Skip to content
Available onCommunityPro

Google Cloud Build Integration

Collect Google Cloud Build build notifications (via a Pub/Sub push subscription), transform them to CDEvents.

  • Cloud Build publishes every status change of a build to the cloud-builds Pub/Sub topic.
  • cdviz-collector transforms these updates to pipelineRun CDEvents, and sends them to the database, listeners,...
  • Without a Pub/Sub push endpoint, poll the Cloud Build REST API instead.

From eventCDEvents
cloud-builds topic, status QUEUEDpipelineRun.queued
cloud-builds topic, status WORKINGpipelineRun.started
cloud-builds topic, status SUCCESS/FAILURE/TIMEOUT/CANCELLED/…pipelineRun.finished

How It Works ​

Cloud Build publishes every update of a Build resource to the built-in Pub/Sub topic cloud-builds, in the project where the build runs. A push subscription on that topic sends each update as an HTTPS POST with the standard Pub/Sub push envelope. The cdviz-collector webhook source accepts this envelope as is: you need no Pub/Sub client.

The transformer decodes message.data and maps the status of the build to pipelineRun events:

Build.statusCDEvents emitted
QUEUEDpipelineRun.queued
WORKINGpipelineRun.queued + pipelineRun.started
SUCCESS, FAILURE, INTERNAL_ERROR, TIMEOUT, CANCELLED, EXPIREDpipelineRun.queued + .started + .finished

A build usually triggers several deliveries. Each delivery rebuilds the lifecycle so far from its Build snapshot, with a stable timestamp per phase. So repeated deliveries produce identical events, which the database deduplicates.

Configuration ​

Setting Up cdviz-collector ​

toml
[sources.gcp_cloudbuild_webhook]
enabled = true
transformer_refs = ["gcp_cloudbuild_webhook"]

[sources.gcp_cloudbuild_webhook.extractor]
type = "webhook"
id = "000-gcp-cloudbuild" # used as part of the webhook's url

[remote.transformers-community]
type = "github://cdviz-dev/transformers-community"

[transformers]
gcp_cloudbuild_webhook = { type = "vrl", template_rfile = "transformers-community:///gcp_cloudbuild_webhook/to_v0_5.vrl" }

For more details on remote transformers, see the Transformers documentation.

Setting Up Google Cloud ​

  1. Enable Cloud Build in the project. The first build creates the cloud-builds topic.
  2. Create a push subscription on cloud-builds, with the endpoint https://your-collector-url/webhook/000-gcp-cloudbuild.
  3. If the collector endpoint is behind IAM (for example Cloud Run ingress), allow the Pub/Sub service agent to call it. See push subscription authentication.

Poll the Cloud Build REST API ​

Use polling to backfill past builds, or when Pub/Sub cannot reach the collector. The gcp_cloudbuild_rest_api transformer reads the responses of builds.list. A finished build emits pipelineRun.queued, .started and .finished, each with its own timestamp, so polling the same build again produces identical events.

toml
[sources.gcp_cloudbuild_rest_builds]
enabled = true
transformer_refs = ["gcp_cloudbuild_rest_builds"]

[sources.gcp_cloudbuild_rest_builds.extractor]
type              = "http_polling"
polling_interval  = "5m"
parser            = "json"
driver_vrl = """
project = "myproject"
reqs = []
if .response == null {
  ts_after = to_string(.metadata.ts_after) ?? ""
  filter_expr = if ts_after != "" { "create_time>\\"" + ts_after + "\\"" } else { "" }
  reqs = [{ "url": "https://cloudbuild.googleapis.com/v1/projects/" + project + "/builds", "query": {"pageSize": "100", "filter": filter_expr}, "route": "both" }]
} else {
  next_token = to_string(.response.body.nextPageToken) ?? ""
  if next_token != "" {
    reqs = [{ "url": string!(.request.url), "query": {"pageSize": "100", "pageToken": next_token}, "route": "both" }]
  }
}
.requests = reqs
"""

[sources.gcp_cloudbuild_rest_builds.extractor.headers]
authorization = { type = "secret", value_file = "/secrets/gcp_token", prefix = "Bearer " }

[transformers]
gcp_cloudbuild_rest_builds = { type = "vrl", template_rfile = "transformers-community:///gcp_cloudbuild_rest_api/builds_to_v0_5.vrl" }

Set project to your Google Cloud project. The polling identity needs cloudbuild.builds.list on the project (role roles/cloudbuild.builds.viewer or broader).

Access token refresh

The http_polling source has no built-in Google OAuth2 or service account token refresh. Keep /secrets/gcp_token filled with a valid access token from an external process, for example a cron job or a sidecar that runs gcloud auth print-access-token > /secrets/gcp_token.

See the HTTP Polling Source reference for ts_after, ts_before_limit and the other options.