Home > front end >  Read substitution var from Google Cloud Build into Dockerfile
Read substitution var from Google Cloud Build into Dockerfile

Time:11-08

I have an angular app that I have deployed on Google Cloud with the following cloudbuild.yaml file:

steps:
  - name: "gcr.io/cloud-builders/docker"
    env:
      - "_ANGULAR_ENV=$_ANGULAR_ENV"
    args: ["build", "-t", "gcr.io/$PROJECT_ID/${_SERVICE_NAME}:${SHORT_SHA}", "--build-arg", "ENV=${_ANGULAR_ENV}", "-f", "Dockerfile", "."]

  - name: "gcr.io/cloud-builders/docker"
    args: ["push", "gcr.io/$PROJECT_ID/${_SERVICE_NAME}:${SHORT_SHA}"]

  - name: "gcr.io/cloud-builders/gcloud"
    args:
      [
        "run",
        "deploy", 
        "${_SERVICE_NAME}",
        "--image",
        "gcr.io/$PROJECT_ID/${_SERVICE_NAME}:${SHORT_SHA}",
        "--region",
        "europe-north1",
        "--platform",
        "managed",
        "--allow-unauthenticated"
      ]

I want to read the value of _ANGULAR_ENV which I provide in the cloud build trigger inside substitution variables section:

enter image description here

and pass this value to the Dockerfile as following:

#Arguments
ARG ENV

.
.
.
## Install dependencies
RUN npm install
## Build the angular app in production mode and store the artifacts in dist folder

RUN echo 'ENVIRONMENT:' $ENV
RUN ng build --configuration=$ENV --output-path=dist
.
.
. 

I am trying to specify if its a development deploy or production one via this variable but when I print the value as above its blank and doesnt get the value from google build. I am not sure as to what am I missing here.

CodePudding user response:

So after spending days on this the issue turned out to be simpler. The place of ARG ENV turned out to be the wrong one. Instead of being at the top, it has to be just a line above its usage as you can see from the working solution:

.
.
.
## Install dependencies
RUN npm install
## Build the angular app in production mode and store the artifacts in dist folder
#Arguments
ARG ENV
RUN echo 'ENVIRONMENT:' $ENV
RUN ng build --configuration=$ENV --output-path=dist
.
.
. 
  • Related