Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploying from github does not read environment variable #2024

Closed
jhonysouza100 opened this issue Mar 4, 2024 · 3 comments
Closed

Deploying from github does not read environment variable #2024

jhonysouza100 opened this issue Mar 4, 2024 · 3 comments

Comments

@jhonysouza100
Copy link

My problem is the following:
When I deploy from github, my CapRover application runs successfully, but I cannot display the environment variable value entered by the Caprover interface. The app shows a value "undefined"

my captain-definition file:

 {
  "schemaVersion": 2,
  "dockerfileLines": [
    "FROM node:18-alpine",
    "WORKDIR /app",
    "COPY ./package.json /app/",
    "RUN npm install && npm cache clean --force",
    "COPY ./ /app",
    "ENV NODE_ENV production",
    "RUN npm run build",
    "CMD [ \"npm\",\"run\", \"preview\" ]"
  ]
}

my App.jsx file:

export default function App() {

  return (
    <>
      <h1>Vite + React</h1>
      {/* PROBLEM */}
      <h2>{`${import.meta.env.VITE_HELLO_WORLD}`}</h2>
      {/* PROBLEM */}
    </>
)}

my vite.config.js file:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  preview: {
    host: true,
    strictPort: true,
    port: 8080
  }
})
@githubsaturn
Copy link
Collaborator

You're reading your variable in your frontend code. The env vars aren't available in frontend. They are available ONLY during runtime. I'm not familiar with ViteJS - but if I were to guess it probably needs the env var on build time:

Read this section: https://caprover.com/docs/app-configuration.html#environment-variables

@jhonysouza100
Copy link
Author

I understand, you should pass the value of the environment variable inside the container.

captain-definition file:

{
 "schemaVersion": 2,
 "dockerfileLines": [
   "FROM node:18-alpine",
   "WORKDIR /app",
   "COPY ./package.json /app/",
   "RUN npm install && npm cache clean --force",
   "COPY ./ /app",
   "ARG VITE_HELLO_WORLD",
   "ENV VITE_HELLO_WORLD $VITE_HELLO_WORLD",
   "ENV NODE_ENV production",
   "RUN npm run build",
   "CMD [ \"npm\",\"run\", \"preview\" ]"
 ]
} 
  • ARG VITE_HELLO_WORLD: Defines a build argument for the environment variable "VITE_HELLO_WORLD". This argument can be provided during the build process.

  • ENV VITE_HELLO_WORLD $VITE_HELLO_WORLD: Sets the environment variable "VITE_HELLO_WORLD" to the value of the build argument. This allows the environment variable to be customized during container build, for example, when using CapRover.

@githubsaturn
Copy link
Collaborator

Yes, that's correct! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants