2

I am having difficult time connecting GitHub Repository with GCP Cloud build using Terraform.

In one of the projects I have connected it manually trough the console, without Terraform but that is not helping me, trying to automate the process. Below I am attaching the code that I am trying to use.

In order to use the GitHub repo, I first had to manually connect the repository through Cloud Build | Triggers | CONNECT REPOSITORY. Once the repository is linked, my Terraform issue described here went away. Connecting process opens a window to GitHub repo search to link individual repos. I'm not sure yet how to get around this.

resource "google_cloudbuild_trigger" "my_cloudbuild_trigger" {
  name        = "my-trigger"
  description = "Trigger on push to ${var.branch_name} branch"
  filename    = "cloudbuild.yaml"
  project     = var.project_id
  github {
    owner =  # replace with your organization name
    name  =           # replace with your repository name
    push {
      branch = var.branch_name
    }
  }
}
Error creating Trigger: googleapi: Error 400: Repository mapping does not exist. Please visit




Tried everything I found on the internet so far, expecting to connect to the Repo but nothing worked so far.

Kristian
  • 33
  • 3

2 Answers2

3

It seems there is currently no way of connecting a github repository via terraform, you must do it manually:

https://cloud.google.com/architecture/managing-infrastructure-as-code#directly_connecting_cloud_build_to_your_github_repository

An approach is outlined in this answer:

https://stackoverflow.com/a/71093605

which will help reduce the number of connections you need to manually maintain.

Here's a feature request for google to add this functionality via their cloudbuild API:

https://issuetracker.google.com/issues/142550612?pli=1

The above also contains alternative approaches using e.g. Github workflows.

Thereafter, this feature could be added to terraform:

https://github.com/hashicorp/terraform-provider-google/issues/8961

Also relevant, discussion herein:

https://github.com/hashicorp/terraform-provider-google/issues/1950

lewchinho
  • 31
  • 1
1

There is now a way to connect to Github Host programatically and add repositories using the CLI or Terraform. https://cloud.google.com/build/docs/automating-builds/github/connect-repo-github?generation=2nd-gen#connecting_a_github_host_programmatically

eduartua
  • 475
  • 1
  • 3
  • 11