typescriptgoogle-cloud-platformnext.jsdeploymenti18next

Use process.env variables inside next.config.js


I have an application with NextJS deployed on GCP. Right now I'm setting up the CD for the application and i have three CDs because there's three different deploys. Let's call it cd-one.yaml, cd-two.yaml and cd-three.yaml. For each CD I have an app.yml (app-one.yml, app-two.yml and app-three.yml).

this is the code for an app.yml for example:

app.yml

the only thing that changes for each app.yml is the service and GCP_SERVICE, for example: one, two and three. The CDs are working and being deployed successfully. But I'm having a problem when I try to use this variables inside next.config.js. For example this is my next-i18next.config.js

next-i18next.config.js

I'm using the variable from app.yml and it works just fine. I'm getting the behavior that I wanted. But when I try to do the same for my next.config.js it looks like it's not receiving the variables there because it will always fall on the last condition that is the default. This is my next.config.js

next-config.js

I don't know what to try anymore. Is there any other way to achieve this?


Solution

  • next-i18next.config.js is a runtime configuration and next.config.js is a build-time configuration. If your build process occurs in an environment where the app.yaml is not used (like a CI/CD pipeline that doesn't apply these configurations), then the environment variables will not be present. The app.yaml file is specific to Google App Engine and is only applied when the app is being deployed, not during the build.

    So you need to add the env variables in Cloud Build Trigger: enter image description here