Automate builds on repository events

You can configure automated Image Builder pipelines that run whenever you push configuration changes to your Git repository such as pushing to main. Automating builds on repository events ensures that Cloud Build immediately validates, builds, and publishes your custom OS images to Compute Engine and Artifact Registry upon code review or merge.

Before you begin

Required roles

To get the permissions that you need to create repository event triggers, ask your administrator to grant you the following IAM roles on your project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Configure repository automation

You can configure repository event triggers by using either the gcloud CLI or Terraform. Select one of the following tabs to configure repository triggers:

gcloud

You can create an automated repository trigger by using the gcloud builds triggers create command and pointing directly to your connected 2nd-gen repository link (--repository).

gcloud builds triggers create repository \
    --name="TRIGGER_NAME" \
    --repository="projects/PROJECT_ID/locations/REGION/connections/CONNECTION_NAME/repositories/REPO_NAME" \
    --branch-pattern="^main$" \
    --build-config="CLOUDBUILD_YAML_PATH" \
    --service-account="projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_EMAIL" \
    --substitutions="_GCS_WORKDIR=gs://STAGING_BUCKET/workdir/,_IMAGE_BUILDER_CONFIG_PATH=RECIPE_PATH,_SERVICE_ACCOUNT=projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_EMAIL,_IMAGE_OUTPUT_PATH=image-builder/binaryOut" \
    --region=REGION \
    --project=PROJECT_ID

Replace the following:

  • TRIGGER_NAME: the name for your trigger, for example, git-push-custom-os-builder.
  • PROJECT_ID: your project ID.
  • REGION: the region where your repository connection is hosted, for example, us-central1.
  • CONNECTION_NAME: the name of your Developer Connect connection, for example, github-connection.
  • REPO_NAME: the linked Git repository name.
  • CLOUDBUILD_YAML_PATH: the relative path to cloudbuild.yaml in your local directory.
  • SERVICE_ACCOUNT_EMAIL: the exact email address of your build service account.
  • STAGING_BUCKET: your Cloud Storage staging workdir bucket name.
  • RECIPE_PATH: the relative path to imagebuilder.yaml in your repository.

Terraform

To configure an automated repository trigger by using Terraform, complete the following steps:

  1. Add the trigger resource to your main.tf configuration file:

    resource "google_cloudbuild_trigger" "repository_trigger" {
      name        = "TRIGGER_NAME"
      location    = "REGION"
      project     = "PROJECT_ID"
      description = "Triggers custom OS image builds on repository push events"
    
      repository_event_config {
        repository = "projects/PROJECT_ID/locations/REGION/connections/CONNECTION_NAME/repositories/REPO_NAME"
        push {
          branch = "^main$"
        }
      }
    
      filename = "CLOUDBUILD_YAML_PATH"
    
      substitutions = {
        _GCS_WORKDIR               = "gs://STAGING_BUCKET/workdir/"
        _IMAGE_BUILDER_CONFIG_PATH = "RECIPE_PATH"
        _SERVICE_ACCOUNT           = "projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_EMAIL"
      }
    }
    

    Replace the following:

    • TRIGGER_NAME: the name for your trigger, for example, git-push-custom-os-builder.
    • PROJECT_ID: your project ID.
    • REGION: the Google Cloud region where your repository connection is hosted, for example, us-central1.
    • CONNECTION_NAME: the name of your Developer Connect connection, for example, github-connection.
    • REPO_NAME: the linked Git repository name.
    • CLOUDBUILD_YAML_PATH: the relative path to cloudbuild.yaml in your local directory.
    • STAGING_BUCKET: your Cloud Storage staging bucket name.
    • RECIPE_PATH: the relative path to imagebuilder.yaml in your repository.
    • SERVICE_ACCOUNT_EMAIL: the email address of your build service account.
  2. Deploy the updated Terraform configuration:

    terraform plan
    terraform apply
    

    To review the complete multi-file Terraform infrastructure project structure, see Create and manage pipelines using Terraform.

What's next