Search code examples
google-cloud-platformgoogle-cloud-build

Environment variables not picked up in build


Dockerfile

FROM node:lts-alpine as build-stage
ENV VUE_APP_BACKEND_SERVER=${_VUE_APP_BACKEND_SERVER}
RUN echo "server env is:"
RUN echo $VUE_APP_BACKEND_SERVER
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run gcpbuild

Cloudbuild config

steps:
  - name: gcr.io/cloud-builders/docker
    args:
      - build
      - '--no-cache'
      - '-t'
      - '$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA'
      - front
      - '-f'
      - front/Dockerfile
      - '--build-arg=ENV=$_VUE_APP_BACKEND_SERVER'
    id: Build
...
...
options:
  substitutionOption: ALLOW_LOOSE
substitutions:
  _VUE_APP_BACKEND_SERVER: 'https://backend.url'

I have also set the variable in the substitutions in the 'Advanced' section. However during the build the echo prints a blank and the variable is not available in the app as expected.


Solution

  • What you need is:

    FROM node:lts-alpine as build-stage
    ARG VUE_APP_BACKEND_SERVER
    ...
    

    Also, fix build-arg line in your cloud build config:

    - '--build-arg',
    - 'VUE_APP_BACKEND_SERVER=${_VUE_APP_BACKEND_SERVER}'
    

    Check out the docs.

    Read more about ARG directive in Dockerfiles.