Cloud build is managed service in Google Cloud Platform Infrastructure to offer CI/CD feature. As it is quite known, these days CI/CD has become the backbone on DevOps. Having a streamlined process ensures fast flow to production. When building pipeline you want to build, test, deploy your artifact to staging/production environments automatically whenever changes are pushed to your code base.

Cloud Build

Cloud build is CI/CD platform offered by GCP. The best thing it provides a generous 120 min/day of free build time(At the time of writing this article). It’s more than enough for small team to get started. It also provides seamless integration with other GCP products.

The missing feature - Sequential build numbers

However, it does have few restrictions, and one such missing feature is having sequential build numbers, cloud build manages the build numbers using SHA string. As Developers we are accustomed to working with incremental build numbers, they are lot easy to manage as compared hash string. But we can achieve it by storing the build numbers in external location, i.e. Cloud Storage. So lets us build a mechanism to

Generating cloud builder to incrementing build numbers

In order to achieve the goal, first we need to create a cloud builder. For this I am creating a cloud builder, let me call it as cloud-build-number. Then we will push this to our image repository, next step will be to use this builder to generate builds with sequential numbers.

Build number cloud builder

In order to create this builder we need entrypoint script, and a Docker file to build our docker container.

Entry point script

So my first file is the script which will be responsible for updating the build numbers. I found one script, so I took an inspiration and modified it as per my need.

The idea is, we are going to have a cloud storage folder where we will be keeping all the records for our build, each record will store the build number.

This script takes name of the file (required argument). I wanted the generated versions to in the format of MAJOR:MINOR:PATCH, so I added additional optional parameters.

  • -ma or --major: To use the major version.
  • mi or --minor: To use specific version.
  • -e or --env-file: Name of the output file (default filename: .buildenv).

The PATCH number is stored and incremented in cloud storage.

The docker file

The docker file is simple, we take the gcr.io/cloud-builders/gsutil and add an entrypoint to our script.

Build & push docker image

We need to build the docker image locally, tag it with the path to the gcr.io registry in the project my-project, and then push the image. I am assuming you have setup gcp login and have write access to cloud registry. For more details refer gcp docs.

docker build -t gcr.io/my-project/increment_build_number .
docker push gcr.io/my-project/increment_build_number

Now that we have our builder pushed to registry, time to move on to generating build numbers.

Create storage bucket

Before we proceed, we need to create a storage bucket on Cloud Storage.

To create a new storage bucket (e.g. build_numbers), use the gsutil command

gsutil mb -p PROECT_ID -b on gs://BUCKET_NAME

For more details, refer cloud storage docs.

Generating build numbers

Finally, it’s time to use builder in our service cloud build, build file: cloudbuild.yaml looks like.

The first step generates a .buildenv file. In the build step we use source command to export the build number to environment variable $build_num.

So this would create a docker image with tag latest & 0.0.1.

Please do leave comment on what you think.

If you liked this article, you can buy me a coffee

Categories: ,

Updated:

Kumar Rohit
WRITTEN BY

Kumar Rohit

I like long drives, bike trip & good food. I have passion for coding, especially for Clean-Code.

Leave a comment