Subscribing to 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.

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.

You can use a push or pull model for your Pub/Sub subscriptions.

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 Pub/Sub to send build updates to Slack or email, see Configuring Slack notifications or Configuring SMTP notifications.