Search code examples
google-cloud-platformterraformgoogle-cloud-buildterraform-provider-gcp

Creating Cloud Build trigger from Terraform


I am trying to create a Cloud Build trigger from Terraform but I am getting "Blocks of type "github" are not expected here." error.

My code:

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "3.5.0"
    }
  }
}

provider "google" {
  credentials = file("key-file.json")

  project = var.project_id
}

resource "google_cloudbuild_trigger" "github-trigger" {
  name = "github-trigger"
  filename = "cloudbuild.yaml"
  location = "europe-west1"
  github {
    owner = <owner>
    name = <name>
    push {
      branch = <branch>
    }
  }
    
} 

When I am not including github block and location, the code is working and the trigger is being created.

My encoding is UTF-8 and I am running the code in VS Code.

Any idea why I am getting this error?


Solution

  • Since you are using provider version that is three years old, it will not work that way. You can either update to a newer version that supports github argument or make sure you are using the correct syntax for the provider version you have. My guess is that the closest to what you want is using trigger_template block [1] where you can define similar arguments:

    • project_id
    • repo_name
    • dir
    • branch_name
    • tag_name
    • commit_sha

    However, I strongly suggest updating to a newer provider version.


    [1] https://registry.terraform.io/providers/hashicorp/google/3.5.0/docs/resources/cloudbuild_trigger#trigger_template