Let's just say, Google Cloud Build has some great capabilities as a CI/CD tool. It's that I always hated cloning their cloud-builders-community repo anytime I wanted to use builders such as Firebase or Terraform. Always the same - make a project, clone repo, submit whatever you need, and ... never update again? Maybe we could just smash some buttons instead.
🔧 Requirements
- Have a git repository on Github or Bitbucket
- Mirror the repository or connect via Cloud Build Github App
You can connect your repo when creating the trigger later
🤖 Automating automation
First things first, let's write a nice little cloudbuild.yaml
that will automate the following:
- Download the community builders repository
- Submit the builder as a new build with our parameters (that's how we automate automation)
💡 Some builders (e.g. Terraform) can be built with specific versions, that's why we need the parametrization
steps:
- name: 'gcr.io/cloud-builders/git'
args: [
'clone', 'https://github.com/GoogleCloudPlatform/cloud-builders-community.git'
]
- name: 'gcr.io/cloud-builders/gcloud'
dir: 'cloud-builders-community'
entrypoint: 'bash'
args: [
'-c',
'gcloud builds submit --config=$_BUILDER/cloudbuild.yaml $_SUBMIT_ARGS ./$_BUILDER',
]
substitutions:
_BUILDER: terraform
_SUBMIT_ARGS: '--async'
Go ahead and commit this file to your repository.
🔴 Finally, the Buttons
Now that our config is ready, we can head to the Google Cloud Console and create a new Cloud Build Trigger.
💡 Leave this trigger on manual invocation. You will always want to specify the builder and args if needed.
🚀 Building a builder
Want to build a new builder or update an existing one? Click the Run Trigger button next to your trigger and submit the name of the builder with arguments.
This will automatically bake the builder and make it available for you in your project. Once the second build is finished, you are ready to use your builder.
💡 Builders such as Terraform or Packer support substitutions to specify versions. Append their flags behind the --async in the trigger
🎁 Bonus: Terraform
In case you are using Terraform, I am leaving the configuration so you don't need to click around in GCP to create your trigger.
resource "google_cloudbuild_trigger" "build-cloud-builder" {
provider = google-beta
name = "build-cloud-builder"
description = "Builds a specified community cloud builder image"
disabled = true
github {
owner = "<owner>"
name = "<repo>"
push {
branch = "^master$"
}
}
substitutions = {
_BUILDER = "terraform"
_SUBMIT_ARGS = "--async"
}
filename = "cloudbuild.yaml"
}
📖 Last Words
If you liked it and want to know how to update these images on a schedule, please leave me a comment. I'll be happy to write about it next.
Top comments (0)