Reputation: 31485
I have a backend service that I'll need to deploy to Google Cloud Run
.
From Google's tutorial on Cloud Run, we get that:
First you need to build your image and send it to Cloud Build.
gcloud builds submit --tag gcr.io/PROJECT-ID/helloworld
Only then you deploy it to Cloud Run:
gcloud run deploy --image gcr.io/PROJECT-ID/helloworld --platform managed
I get the sequence above. But I'll be deploying this service to 2 different environments: TEST
and PROD
.
So I need an SERVER_ENV
variable, that should be "PROD"
on my production environment, and of course it should be "TEST"
on my test environment. This is so my server (express server that will be run from the container) knows which database to connect to.
But the problem is that I only have a single Dockerfile
:
FROM node:12-slim
ENV SERVER_ENV=PROD
WORKDIR /
COPY ./package.json ./package.json
COPY ./distApp ./distApp
COPY ./distService ./distService
COPY ./public ./public
RUN npm install
ENTRYPOINT npm start
So how can I set different ENV
variables while following the build & deploy sequence above? Is there an option in the gcloud builds submit
comment that I can maybe override something? Or use a different Dockerfile
? Anybody got other ideas?
AN IDEA:
Maybe use the Cloud Build configuration file?
cloudbuild.yaml
Upvotes: 1
Views: 2850
Reputation: 692
I think that what you are trying to achieve is possible using the ARG
instruction in the Dockerfile.
I would set it to be test
and then use the arg parameter according to the environment you are building in right now.
More docs:
how to use it with Docker Compose - https://docs.docker.com/compose/compose-file/#args
additional documentation - https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
Upvotes: 0
Reputation: 75970
You can't achieve this without a cloudbuild.yaml
file. The command gcloud builds submit --tag ...
doesn't accept extra docker parameter.
Here an example of configuration
FROM node:12-slim
ARG SERVER_CONF=PROD
ENV SERVER_ENV=$SERVER_CONF
WORKDIR /
COPY ./package.json ./package.json
COPY ./distApp ./distApp
COPY ./distService ./distService
COPY ./public ./public
RUN npm install
ENTRYPOINT npm start
I created a build argument SERVER_CONF
. Your ENV
will take this value at build time. The default value is PROD
Now your cloudbuild.yaml
file
step:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '--tag=gcr.io/PROJECT-ID/helloworld', '--build-arg="SERVER_CONF=$_SERVER_CONF"', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/PROJECT-ID/helloworld']
substitutions:
_SERVER_CONFPROD: PROD
Use substitution variables to change the environment. Not that here you can also set a default value, that override your Dockerfile value. Take care of this!
You can also set the tag as substitution variable if you want
Eventually, how to call your Cloud Build
# With default server conf (no substitution variables, the the file default)
gcloud builds submit
# With defined server conf
gcloud builds submit --substitutions=_SERVER_CONF=TEST
Upvotes: 3