Is it possible to generate tags dynamically in Google Cloud Build?

Viewed 3414

First of all: I am somewhat new to cloud build. Compared to previously used methods, I find it a wrenching, unripe and fairly annoying framework. Endless time is spend getting builders to work that supposedly work out of the box (like the helm builder for example), and it's limitations are astonishing and frustrating. Perhaps the following problem is a good example:

I would like to build and push a docker image. According to the documentation, the images to be pushed to the docker repository at the end (I'm using GCR for this) reside in the following configuration section in my cloudbuild.yaml file:

images:
  - 'eu.gcr.io/$PROJECT_ID/my-project:${_TAG}'
  - 'eu.gcr.io/$PROJECT_ID/my-project:latest'

I can set the _TAG substitution manually by using the section:

substitutions:
  _TAG: x.y.z

but that means I have to manually fix the version number in this file every time. Worse still: if I branch out, I need to maintain the version number all the time. I have a python project in this case and it uses setuptools, the version is naturally contained in the setup.py file and I can parse it out with no problem. Attempts to parse the number into a specific file and use $(cat VERSION) in the images section fail, because the system claims it can't substitute the $(cat VERSION) part. So how can I overwrite the _TAG variable inside of another build step such, that it appears correct in the 'images' section?

1 Answers

If you are using triggered builds from Cloud Source Repositories, GitHub, or Bitbucket you can tag your commit and use the $TAG_NAME default substitution variable.

 images:
      - 'eu.gcr.io/$PROJECT_ID/my-project:$TAG_NAME'
      - 'eu.gcr.io/$PROJECT_ID/my-project:latest'

On the other hand if you are using the Cloud SDK to submit the Cloud Build build you can provide values with the --substitutions argument:

gcloud builds submit [SOURCE] --config config.yaml --substitutions _TAG=x.y.z

Also I believe you would find this GitOps-style continuous delivery with Cloud Build tutorial very helpful. It explains how to create a continuous integration and delivery (CI/CD) pipeline on Google Cloud Platform using Cloud Build.

Related