Building without environment variables causes errors
Answered
Standard Chinchilla posted this in #help-forum
Standard ChinchillaOP
So i have a CI pipeline which builds my nextjs app, now in here I don't have any environment variables which should be the case and the whole app builds without it now when I run my container I inject my variables but
NEXT_PUBLIC_ ones don't get inlined? Is there something I can do about thisAnswered by joulev
so you must configure your workflow somehow so that at least, all
NEXT_PUBLIC_ variables are available at build53 Replies
@Standard Chinchilla So i have a CI pipeline which builds my nextjs app, now in here I don't have any environment variables which should be the case and the whole app builds without it now when I run my container I inject my variables but `NEXT_PUBLIC_` ones don't get inlined? Is there something I can do about this
NEXT_PUBLIC_ env vars are inlined during build. once the build completes, env var changes only take effect for things that run on the serverso you must configure your workflow somehow so that at least, all
NEXT_PUBLIC_ variables are available at buildAnswer
@joulev so you must configure your workflow somehow so that at least, all `NEXT_PUBLIC_` variables are available at build
Standard ChinchillaOP
any idea how?
🙂
Right now I am exposing them directly in my repo
but dk if thats right
@Standard Chinchilla any idea how?
well i dont know what your workflow looks like, so...
Standard ChinchillaOP
pushing -> building in CI
you probably want to populate the env vars on the machine used to build too
yes so add the env vars to the CI
if that is github actions, then use github action secrets
@joulev if that is github actions, then use github action secrets
Standard ChinchillaOP
Ohk thank you
Standard ChinchillaOP
ENV HOSTNAME "0.0.0.0"
ENV NEXT_PUBLIC_STRIPE_PUBLIC_KEY "somekey"
ENV NEXT_PUBLIC_SERVICES_BASE_URI "http://svc.cluster.local" I added this to my dockerfile for now^But it still isn't able to inline them
useEffect(() => {
const stripeENV = env("NEXT_PUBLIC_STRIPE_PUBLIC_KEY");
console.log(process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY);
console.log(stripeENV);
setStripe(loadStripe(stripeENV!));
}, []);(ignore the env for now)
Hmm I don’t use docker so can’t help here, sorry. Though many people use docker so someone will probably come help soon
@Ray You need to set the env on the build step
Standard ChinchillaOP
I already do that 🙂
But it doesnt build the container with those vars for obvious reasons
the public ones
@Standard Chinchilla I already do that 🙂
Could you share your dockerfile
Standard ChinchillaOP
the one in examples
Standard ChinchillaOP
While building? no
thats the whole point 😉
Then where do you set the variables?
Standard ChinchillaOP
when running the container
@Standard Chinchilla https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
Put your variables to the .env file if you are using this
@Standard Chinchilla when running the container
This doesnt work
Like i said the variables need to set on build step
Standard ChinchillaOP
But inside CI pipeline why would I do that mhm
Do I add every single variable I have in github secret

and then referenced each single variable one ny one
Doesnt make sense right there should be some betteer way
regardless of that
I hardcoded my value
Standard ChinchillaOP
It is at build time 🙂
where?
where is the build?
Standard ChinchillaOP
Github action.
Ah nvm I passed that in after the build completed my bad
Maybe I should just use my client vars in the github secret would help