Cloud Build publishes messages on a
Google Pub/Sub topic when your build's state changes,
such as when your build is created, when your build transitions to a working
state, and when your build completes. Each message contains a base64 JSON string
representation of your
Build resource in the
message.data
attribute. The build's unique ID and the build's
status can be found
in the message.attributes
field.
By default, messages are published to the cloud-builds
topic; you can also
specify a custom topic name in the
options.pubsubTopic
field of your build config file. For more information about configuring topic
names in your build config file, see
Pub/Sub topics for build notifications.
You can use a push or pull model for your Pub/Sub subscriptions.
Receive build notifications
To receive build notifications:
Enable the Cloud Build API:
When you enable the Cloud Build API, the Cloud Build Service Agent service account is automatically added to your project. The service account lets you receive build notifications from Pub/Sub.
The service account has the following format, where project-number is your project number:
service-project-number@gcp-sa-cloudbuild.iam.gserviceaccount.com
If you don't see the Cloud Build Service Agent service account on your IAM page, or you can't receive notifications from Pub/Sub, then follow these steps to add the Cloud Build Service Agent service account to your project:
Open the IAM page in the Google Cloud console:
Click Grant access.
Add the following principal, where project-number is your project number:
service-project-number@gcp-sa-cloudbuild.iam.gserviceaccount.com
Select Service Agents > Cloud Build Service Agent as your role.
Click Save.
Enable the Pub/Sub API:
Create the
cloud-builds
topic:gcloud pubsub topics create cloud-builds
You can also define a custom topic name in your build config file so that messages are sent to the custom topic instead. In this case, you would create a topic with the same custom topic name:
gcloud pubsub topics create topic-name
For more information, see Pub/Sub topics for build notifications.
To learn more about managing Pub/Sub topics, see Managing topics and subscriptions.
Push subscriptions
Push subscriptions deliver messages to an HTTP endpoint that you define. Messages are delivered as soon as they are published to the topic.
Messages sent from push subscriptions look like this:
{
"message": {
"attributes": {
"buildId": "abcd-efgh...",
"status": "SUCCESS"
},
"data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==",
"message_id": "136969346945"
},
"subscription": "projects/myproject/subscriptions/mysubscription"
}
Pull subscriptions
Pull subscriptions deliver message when polled by the subscribed application. Messages are delivered when the subscription is polled.
Messages sent from pull subscriptions look like this:
{
"receivedMessages": [
{
"ackId": "dQNNHlAbEGEIBERNK0EPKVgUWQYyODM2LwgRHFEZDDsLRk1SK...",
"message": {
"attributes": {
"buildId": "abcd-efgh-...",
"status": "SUCCESS"
},
"data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==",
"messageId": "19917247034"
}
}
]
}
Subscribe to build update notifications
You have several options for subscribing to build update notifications. For example, you can push messages to an endpoint, or write a Python app for polling your subscription.
To learn how to set up Pub/Sub subscriptions for build updates, read the Pub/Sub Subscriber Guide. You can also learn about Pub/Sub Client Libraries, which make developing subscriber applications easier.
To learn how to use Pub/Sub to send build updates to email or to services such as Slack, see Cloud Build notifiers.