Cloud Build with Docker and Dockerfile using dynamic environment variables

Photo by Kelly Sikkema on Unsplash

For using enviroment variable inside of a build process with cloud build can be challenging. Therefore I provided you with an example how the process can look like. The think you need:

  1. Dockerfile on git
  2. cloudbuild.yml on git

If you created both add the following:

  1. cloudbuild.yml
options:
logging: CLOUD_LOGGING_ONLY
steps:
— name: gcr.io/cloud-builders/docker
entrypoint: ‘bash’
args: [‘-c’, ‘docker build — build-arg=”VUE_APP_STRAPI_URL=https://docvox-app-cms.herokuapp.com/" -t gcr.io/$PROJECT_ID/app-frontend:${SHORT_SHA} -t gcr.io/$PROJECT_ID/app-frontend:latest .’]
— name: “gcr.io/cloud-builders/docker”
args: [“push”, “gcr.io/$PROJECT_ID/app-frontend”]
— name: “gcr.io/cloud-builders/gcloud”
args:
[
“run”,
“deploy”,
“app-frontend”,
“ — image”,
“gcr.io/$PROJECT_ID/app-frontend:${SHORT_SHA}”,
“ — region”,
“europe-west4”,
“ — platform”,
“managed”,
“ — allow-unauthenticated”,
“ — memory”,
“1Gi”,
“ — cpu”,
“1”
]
images:
— ‘gcr.io/$PROJECT_ID/app-frontend:$SHORT_SHA’
— ‘gcr.io/$PROJECT_ID/app-frontend:latest’
timeout: 3600s

2. Dockerfile

FROM node:lts-alpineARG VUE_APP_STRAPI_URL
# use the value to set the ENV var default
ENV VUE_APP_STRAPI_URL=$VUE_APP_STRAPI_URL
RUN printenv
RUN echo “the ENV is $VUE_APP_STRAPI_URL”
# install simple http server for serving static content
RUN npm install -g http-server
# make the ‘app’ folder the current working directory
WORKDIR /app
# copy both ‘package.json’ and ‘package-lock.json’ (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. ‘app’ folder)
COPY . .
# build app for production with minification
RUN npm run build
# EXPOSE 8080
CMD [ “node”, “server.js” ]

I hope it helped. Happy coding.

--

--

--

If you like my work or have question contact me.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Install node.js on mac with nodebrew

The matrix unroll

Cover sort hear wide.

Why Developers Should Blog

🎃🎃🎃 Did your application performance haunt you this Halloween? 🎃🎃🎃

MapReduce in MongoDB

useState hook for Functional Components

Fix VBA error code 429 ActiveX component can’t create object

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Walter-Tscharf-Development

Walter-Tscharf-Development

If you like my work or have question contact me.

More from Medium

Storage Space Reduced to One-Tenth of MySQL — The Application of TDengine in Monitoring Scenario…

Application Framework

Google App Engine

A Quick Guide into Using Elasticsearch