Cloud Build trigger via Terraform

A Cloud Build trigger is the bridge between GitHub and Cloud Build. Push to main fires the trigger, which clones the repo, reads cloudbuild.yml, and runs the build. Provisioning the trigger via Terraform keeps it in version control alongside the buildspec.

terraform/cloud_build.tf
hcl
resource "google_cloudbuild_trigger" "deploy_dbt_runner" {
  name        = "deploy-${var.project_short_name}-dbt-runner"
  project     = var.gcp_project_id
  location    = var.region

  github {
    owner = var.github_owner
    name  = var.github_repo
    push { branch = "^main$" }
  }

  filename = "cloud_run_dbt/cloudbuild.yml"

  substitutions = {
    _SERVICE_NAME = var.cloud_run_service_name
    _REGION       = var.region
    _AR_REPO      = "cloud-run"
  }

  service_account = google_service_account.cloud_build.id
}

A Cloud Build trigger that fires on every push to main. The filename points at the cloudbuild.yml living in the repo. Substitution vars override defaults at runtime.

The github { } block requires a one-time Cloud Build GitHub connection setup (in the Cloud Console, install the Cloud Build app on the org). After that, Terraform manages all triggers without manual GitHub steps.

Three triggers, each with included_files filters that limit when each trigger fires. A push that only touches Service A skips the build for Services B and C. Faster CI, cheaper Cloud Build minutes.