Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Deploy Cloud build trigger based on Pub/Sub event : Error 400: Request contains an invalid argument.

Hello,

In my GCP project I have 2 repos on artifact registry (Initial and Final). My need is the following: when an image is updated on the Initial repo, I want it to be automatically copied to the Final repo.
To do this, I'd like to set up a Pub/Sub topic that listens on the Initial repo, and a Cloud Build Trigger that is triggered by Pub/Sub as soon as it receives a message.


I build my resources with terraform. According to the official documentation (https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudbuild_trigger.ht...), as my trigger is triggered by a Pub/Sub event, i can't use the filename attribute to specify where my cloud build file is. i have to use the git_file_source attribute.

so I created the following resources with terraform :
- a pub/sub topic

- a cloud source repository

- a cloud build trigger

like this :

 

 

resource "google_project_service" "source_repositories_api" {
  project = "my_project_id"
  service = "sourcerepo.googleapis.com"
}

resource "google_sourcerepo_repository" "my_repo" {
  depends_on = [ google_project_service.source_repositories_api ]
  project = "my_project_id"
  name    = "mi-repo"
}

resource "google_pubsub_topic" "image_updates_topic" {
  name    = "image-updates"
  project = "my_project_id"
}

resource "google_cloudbuild_trigger" "trigger_pubsub" {
  name     = "tri-pubsub"
  location = "europe-west1"

  pubsub_config {
    topic = google_pubsub_topic.image_updates_topic.id
  }

  approval_config {
    approval_required = false
  }

  git_file_source {
    path      = "cloudbuild.yaml"
    repository = google_sourcerepo_repository.my_repo.id
    revision  = "refs/heads/master"
    repo_type = "CLOUD_SOURCE_REPOSITORIES"
  }
}

 

 

Having first uploaded the cloudbuild.yaml file to the repo

As I want to test the simple creation of my trigger, the cloudbuild.yaml file is very simple:

 

 

steps:
  - name: ubuntu
    args:
      - echo
      - hello world
options:
  logging: CLOUD_LOGGING_ONLY

 

 

However, I get the following error:

 

 

╷
│ Error: Error creating Trigger: googleapi: Error 400: Request contains an invalid argument.
│ 
│   with google_cloudbuild_trigger.trigger_pubsub,
│   on cloud_build.tf line 17, in resource "google_cloudbuild_trigger" "trigger_pubsub":
│   17: resource "google_cloudbuild_trigger" "trigger_pubsub" {
│ 
╵

 

 

maybe I need to use a new attribute like source_to_build, but I don't see the point here.

Can you please help me?

 
1 REPLY 1

Hi @aalshikhley2,

Welcome to Google Cloud Community!

It seems the issue originates from the configuration of the git_file_source block. Here are a few actions you can implement to fix the issue:

  1. Change the repository attribute: In your trigger definition, you are using google_sourcerepo_repository.my_repo.id as the value for the repository attribute. This should be the repository name instead of the ID. You can replace it with google_sourcerepo_repository.my_repo.name.
  2. Verify Pub/Sub setup: Ensure that your Pub/Sub topic is correctly set up to receive messages for image updates from the Initial repository.
  3. Review logs: Check Cloud Logging to get more detailed information on what might be going wrong with the trigger creation process.

After making these changes, you can try running the trigger again to see if the issue is resolved. 

I hope the above information is helpful.