I´m trying to dockerize my Angular application for Cloud Run and then conditionally build it with the production or the development configuration based on an environment variable.
cloud build command:
gcloud builds submit --tag gcr.io/project-id/image-id --timeout=1200
Dockerfile:
FROM node:14
WORKDIR usr/src/app
COPY package*.json ./
RUN npm install
# Copy local code to the container
COPY . .
# Build app
RUN if [ "$ENV" = "development" ] ; then npm run build-dev:ssr ; else npm run build-prod:ssr ; fi
CMD ["npm", "run", "serve:ssr"]
At the moment, the $ENV doesn´t exist, but is there a way to pass this with the gcloud build submit command?
You can't pass build argument as you do with docker build command. To achieve this, you need to create a simple Cloud Build file
steps:
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-t', 'gcr.io/project-id/image-id', '--build-arg=ENV=$_MY_VARIABLE', '.' ]
# push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/project-id/image-id']
images:
- 'gcr.io/project-id/image-id'
timeout: 1200s
substitutions:
_MY_VARIABLE: default value if not passed in the build command
And run it like this
gcloud builds submit --substitutions=_MY_VARIABLE=specific_value
Adding on Guillaume Blaquiere's answer
Do not forget to add the ARG VAR_NAME in the Dockerfile (it is like a placeholder waiting for the --build-arg input).
Adding only the '--build-arg=ENV=$_MY_VARIABLE' argument in the Cloud Build YAML file will not be enough for you to access the variable from the Dockerfile itself since you are giving an input to a placeholder that does not exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With