Sending build notifications

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. You can use a push or pull model for your Pub/Sub subscriptions.

The Pub/Sub topic to which Cloud Build publishes these build update messages is called cloud-builds. When you enable the Pub/Sub API, the cloud-builds topic will automatically be created for you. 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.

Cloud Build Service Agent

The Cloud Build Service Agent service account enables you to receive notifications via Pub/Sub. The service account is automatically added to your project when you enable the Cloud Build API and is granted the role Cloud Build Service Agent.

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 do not see the Cloud Build Service Agent service account on your Cloud IAM page or are not able to receive notifications via Pub/Sub, follow these steps to add the Cloud Build Service Agent service account to your project:

  1. Open the Cloud IAM page in the Google Cloud Console:

    Open the IAM page

  2. Click Add.

  3. Add the following member, where [PROJECT_NUMBER] is your project number:

     service-[PROJECT_NUMBER]@gcp-sa-cloudbuild.iam.gserviceaccount.com
    
  4. Select Service Management > Cloud Build Service Agent as your role.

  5. Click Save.

Push

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

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"
      }
    }
  ]
}

How do I subscribe to build update notifications?

You have a variety of 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.

If you want to learn how to use Cloud Functions and Pub/Sub to send build updates to Slack and other third-party applications, see Configuring Notifications for Third-Party Services.

Next steps