Andrew Edwards
Andrew Edwards

Reputation: 1025

Google AppEngine ENV variables from Google Cloud Build Dockerfile

So I have a CloudBuild trigger that builds my cloudbuild.yaml file and this is all fine and dandy. I also use the gcloud builder to run docker commands to pass ENV variables to my Dockerfile. for example:

steps:
- name: 'gcr.io/$PROJECT_ID/swift:4.2'
  args: ['test']
  id: 'Running unit tests'

- name: 'gcr.io/cloud-builders/docker'
  args: ['build','--build-arg', 'PROJECT=$PROJECT_ID','-t', 'us.gcr.io/$PROJECT_ID/$BRANCH_NAME:$SHORT_SHA', '.']
  id: 'Building docker image'

- name: 'gcr.io/cloud-builders/docker'
  args: ["push", "us.gcr.io/$PROJECT_ID/$BRANCH_NAME:$SHORT_SHA"]
  id: 'Pushing built image to registry'

- name: 'gcr.io/cloud-builders/gcloud'
  args: ['app', 'deploy']
  id: 'Deploying to AppEngine'
timeout: 1800s # 30 minute timeout

As you can see I, I'm using the ENV variables that all GCP resources have by default.($PROJECT_ID for example). And in the docker command I'm passing it as an argument so I can use the ARG command in the dockerfile:

ARG PROJECT
FROM gcr.io/${PROJECT}/swift:4.2 as builder
WORKDIR /App
#Other commands....

Now all of this works fine and I'm able to build my image etc. now I want to deploy to app engine in the final step. Only problem is that I'm using the same Dockerfile to uses the swift:4.2 base image that's only located in my GoogleContainerRegistry so I need the $PROJECT_ID for my project to pull that.

My question is: Is there any way to have AppEngine build environment pass arguments to the docker build that builds my image when deploying? I have an app.yaml file and I know there's an env_variables: property and I know I'd be able to use the docker ARG or ENV command (can't remember which one) to get my $PROJECT_ID inside my Dockerfile. But the only problem is AppEngine doesn't have that Property defined as far as I know. The only other thing I can think of is to echo the $PROJECT_ID from Cloud Builder step to the end of the app.yaml file. But if there's a cleaner approach I'd love to hear about it. Thanks!

Upvotes: 2

Views: 1766

Answers (1)

Andrew Edwards
Andrew Edwards

Reputation: 1025

I think I've found a solution for my needs.

gcloud app deploy has a flag image-url that can specify an already built image rather than rebuilding the Dockerfile. So I went with this as my final cloudbuild.yaml

 - name: 'gcr.io/cloud-builders/gcloud'
   args: ['app', 'deploy', '--image-url', 'gcr.io/$PROJECT_ID/$BRANCH_NAME:$SHORT_SHA']

Basically point to the image I just built and pushed to my container registry.

Upvotes: 5

Related Questions