Triggering a google_cloudbuild_trigger from terraform to create a google_storage_bucket_object

Viewed 149

I have the following setup:

  1. A google_cloudbuild_trigger that runs on the latest github code and builds and uploads the build to a dataflow flex artifact location (on google storage)
  2. A dataflex template job that depends on the artifact being present.

I want to configure terraform so that if the artifact is not present, then automatically trigger the google_cloudbuild_trigger and wait for it to complete. If the artifact is present, then just continue using it.

Is this even possible in terraform ?

Snippets of my terraform script:

The following is the cloudbuild trigger:

resource "google_cloudbuild_trigger" "build_pipeline" {
  name = "build_pipeline"
  github {
    owner = "my-org"
    name  = "my-project"
    push {
      branch = "^my-branch$"
    }
  }

  filename = "path/cloudbuild.yaml"
  substitutions = {
    _PROJECT_ID = var.google_project_id
  }
}

The following is the dataflow flex template job:

resource "google_dataflow_flex_template_job" "dataflow_job" {
  provider                = google-beta
  name                    = "dataflow_job"
  container_spec_gcs_path = "${google_storage_bucket.project_store.url}/path/to/flex/template.json"
  project                 = var.google_project_id
  depends_on              = [google_bigquery_table.tables]
  parameters = { ...  }
}

I have tried creating a simple "data" resource like:

data "google_storage_bucket_object" "picture" {
  name   = "path/to/flex/template.json"
  bucket = google_storage_bucket.project_store.name
}

But I cannot figure out how to change this into something that triggers the google_cloudbuild_trigger.build_pipeline if the data resource doesn't exist.

Something like:

data "google_storage_bucket_object" "picture" {
  name   = "path/to/flex/template.json"
  bucket = google_storage_bucket.project_store.name
  if_does_not_exist_trigger = google_cloudbuild_trigger.build_pipeline
}
0 Answers
Related