Automatically cancel running builds on the same branch, when a new commit is pushed
CloudBuild is a CI/CD offered by Google. At Workable’s Mobile department we made a proof of concept to check if CloudBuild fits our needs in order to be used for artifact (APK) building.
A complex project requires a lot of custom configuration in order to properly set it up and we were able to find most of them in the great CloudBuilders community repo.
Cancelling a running build
CloudBuild builds though come at a cost and depending on your budget and the machine type you use. So having builds run for multiple commits that are pushed on a branch, means a bigger cost on your monthly bill.
As you might have guessed this is something that would not work in case multiple members of our team were working on the same branch and pushing commits with a high frequency.
We need to find a solution that would let us cancel any previously running builds in favor of the new ones.
Cancelot is the name of the Builder we built for the purpose described above. Initially our team had named it “Ralph” from the movie “Wreck-it Ralph”, but Twitter is so resourceful and came up with the name “Cancelot”.
Cancelot is added as a buildstep on your CloudBuild YAML file and is responsible to check for any builds that are currently running for the same branch.
It makes use of the CloudBuild Go package in order to fetch the running builds, by filtering with the following criteria:
build_id != "[CURRENT_BUILD_ID]" AND
source.repo_source.branch_name = "[BRANCH_NAME]" AND
status = "WORKING" AND
After successfully fetching any previously started (& still running builds) it loops through them and cancels them.
Cancelot’s usage and setup is inspired by the CloudBuilders “slackbot” so it is very easy to use it.
- Deploy the GCR image of Cancelot to your project’s GCR
- Add Cancelot’s buildstep in your CloudBuild YAML file
- name: 'gcr.io/$PROJECT_ID/cancelot'
We wanted to give back to the community, just like we were able to use the great stuff that other members have built. Cancelot currently lives on an open PR in CloudBuilders community repository. You are free to use it at your own will until the PR is either accepted or closed 😄