Reputation: 610
as described in title, some frameworks like Next.js or Nuxt.js require some env vars defined in app.yaml
(only accessible at runtime) to be accessible during build step, mainly npm run build
the workaround I'm using at the moment is to define the same env vars in 2 different places app.yaml
and cloud build trigger env vars: It is not ideal at all
Upvotes: 0
Views: 619
Reputation: 75745
Agree, to maintain the same value in 2 places is the best way to lost the consistency and to create bugs. Thus, the best solution is to define only once these variables.
Put them where you want:
But only once! Then create a step in your Cloud Build job that parse the configuration variable file and extract the values to put them at the correct location before continuing the build process.
grep
, cut
and sed
works well for this. You can provide file examples in you need to have code example for this substitution.
Edit 1
According to your comment, there is few things to know. Cloud Build is great!! But Cloud Build is also boring...
The env var management is a perfect example. The short answer is: it's not possible to reuse an env var define in the step N in the step N+x.
To solve this, you need to do ugly things, like this
steps:
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- -c
- |
MY_ENV_VAR="cool"
echo $${MY_ENV_VAR} > env-var-save.file
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- -c
- |
MY_ENV_VAR=$$(cat env-var-save.file)
echo $${MY_ENV_VAR}
Because only the /workspace
directory is common between each step, you have to save the env vars in file and to read them afterwards in the step that you want.
About the app.yaml
file, you can do something like this
app.yaml
file content example
runtime: go114
service: go-serverless-oracle
env_variables:
ORACLE_IP: "##ORACLE_IP##"
Example of Cloud Build step, get the value from substituions variable in Cloud Build.
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- -c
- |
sed -i -e 's/##ORACLE_IP##/$_ORACLE_IP/g' app.yaml
cat app.yaml
substitutions:
_ORACLE_IP: 127.0.0.1
Upvotes: 2