Automating Google Community Builders Repository on GCP
Tags: devops, googlecloud, ci, tutorial
Published: 2020-12-25 14:11:24

---
title: Automating Google Community Builders Repository on GCP
published: true
description: Make sure you don't have to download the community builders and submit them manually ever again
tags: devops, googlecloud, ci, tutorial
//cover_image: https://direct_url_to_image.jpg
---

Let's just say, [Google Cloud Build](https://cloud.google.com/cloud-build) has some great capabilities as a CI/CD tool. It's that I always hated cloning their [cloud-builders-community](https://cloud.google.com/cloud-build) repo anytime I wanted to use builders such as [Firebase](https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/firebase) or [Terraform](https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/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.

## :wrench: Requirements

- Have a git repository on Github or Bitbucket
- Mirror the repository or connect via [Cloud Build Github App](https://github.com/apps/google-cloud-build)

> You can connect your repo when creating the trigger later

## :robot: Automating automation

First things first, let's write a nice little `cloudbuild.yaml` that will automate the following:
1. Download the [community builders repository](https://github.com/GoogleCloudPlatform/cloud-builders-community)
2. Submit the builder as a new build with our parameters (that's how we automate automation)

> :bulb: Some builders (e.g. [Terraform](https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/terraform)) can be built with specific versions, that's why we need the parametrization

```yaml
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.

## :red_circle: Finally, the Buttons

Now that our config is ready, we can head to the Google Cloud Console and [create a new Cloud Build Trigger](https://console.cloud.google.com/cloud-build/triggers/add).

![Create trigger displaying Google Cloud Platform Cloud Build](https://dev-to-uploads.s3.amazonaws.com/i/w1aoiynybzxpcsy8b782.png)

> :bulb: Leave this trigger on manual invocation. You will always want to specify the builder and args if needed.

## :rocket: 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.

![Run trigger dialog](https://dev-to-uploads.s3.amazonaws.com/i/e2pqkxmomj2oaivbmden.png)

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.

> :bulb: Builders such as Terraform or Packer support substitutions to specify versions. Append their flags behind the --async in the trigger

## :gift: Bonus: Terraform

In case you are using [Terraform](https://www.terraform.io/), I am leaving the configuration so you don't need to click around in GCP to create your trigger.

```tf
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"
}
```

## :book: 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.