Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Cloud Build pubblica messaggi su un argomento Google Pub/Sub quando cambia lo stato della build, ad esempio quando viene creata, quando passa a uno stato di funzionamento e quando viene completata. Ogni messaggio contiene una rappresentazione in stringa JSON base64 della risorsa di compilazione nell'attributo message.data. L'ID univoco della compilazione e il relativo
stato sono disponibili
nel campo message.attributes.
Per impostazione predefinita, i messaggi vengono pubblicati nell'argomento cloud-builds. Puoi anche specificare un nome di argomento personalizzato nel campo options.pubsubTopic del file di configurazione di compilazione. Per ulteriori informazioni sulla configurazione dei nomi degli argomenti nel file di configurazione della build, consulta Argomenti Pub/Sub per le notifiche di build.
Quando attivi l'API Cloud Build, l'account di servizio Agente servizio Cloud Build viene aggiunto automaticamente al progetto. L'account di servizio ti consente di ricevere notifiche di compilazione da Pub/Sub.
L'account di servizio ha il seguente formato, dove
project-number è il numero del progetto:
Se non vedi l'account di servizio Agente di servizio Cloud Build nella pagina IAM o non riesci a ricevere notifiche da Pub/Sub, segui questi passaggi per aggiungere l'account di servizio Agente di servizio Cloud Build al tuo progetto:
Puoi anche definire un nome di argomento personalizzato nel file di configurazione della compilazione
in modo che i messaggi vengano inviati all'argomento personalizzato. In questo caso, dovresti creare un argomento con lo stesso nome dell'argomento personalizzato:
Le iscrizioni pull inviano il messaggio quando vengono sottoposte a polling dall'applicazione a cui hai effettuato l'iscrizione. I messaggi vengono recapitati quando viene eseguito il polling dell'abbonamento.
I messaggi inviati dalle iscrizioni pull hanno il seguente aspetto:
Per scoprire come configurare le sottoscrizioni Pub/Sub per gli aggiornamenti delle build, consulta la Guida per i sottoscrittori Pub/Sub.
Puoi anche scoprire di più sulle
librerie client Pub/Sub,
che semplificano lo sviluppo di applicazioni di abbonamento.
Per scoprire come utilizzare Pub/Sub per inviare aggiornamenti di compilazione via email o a servizi come Slack, consulta Avvisi di Cloud Build.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eCloud Build sends build update messages to the \u003ccode\u003ecloud-builds\u003c/code\u003e Pub/Sub topic when a build's state changes.\u003c/p\u003e\n"],["\u003cp\u003eEach message includes a base64 JSON representation of the Build resource in \u003ccode\u003emessage.data\u003c/code\u003e and the build's ID and status in \u003ccode\u003emessage.attributes\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou can use either a push or pull model for your Pub/Sub subscriptions to receive these build notifications.\u003c/p\u003e\n"],["\u003cp\u003eTo receive notifications, you must enable both the Cloud Build API and the Pub/Sub API, and also add the Cloud Build Service Agent to your project.\u003c/p\u003e\n"],["\u003cp\u003eTo access information on how to set up Pub/Sub subscriptions for build updates, refer to the Pub/Sub Subscriber Guide, and Cloud Build notifiers for sending build updates to other services.\u003c/p\u003e\n"]]],[],null,["# Subscribe to build notifications\n\nCloud Build publishes messages on a\n[Google Pub/Sub](/pubsub) topic when your build's state changes,\nsuch as when your build is created, when your build transitions to a working\nstate, and when your build completes. Each message contains a base64 JSON string\nrepresentation of your\n[Build resource](/build/docs/api/reference/rest/v1/projects.builds) in the\n`message.data` attribute. The build's unique ID and the build's\n[status](/build/docs/api/reference/rest/v1/projects.builds#status) can be found\nin the `message.attributes` field.\n\nBy default, messages are published to the `cloud-builds` topic; you can also\nspecify a custom topic name in the\n[`options.pubsubTopic`](/build/docs/build-config-file-schema#pubsub-topic)\nfield of your build config file. For more information about configuring topic\nnames in your build config file, see\n[Pub/Sub topics for build notifications](/build/docs/configuring-notifications/notifiers#pubsub-topic-config).\n\nYou can use a [push or pull model](/pubsub/docs/subscriber#push_pull) for your\nPub/Sub subscriptions.\n\nReceive build notifications\n---------------------------\n\nTo receive build notifications:\n\n1. Enable the Cloud Build API:\n\n [Enable the Cloud Build API](https://console.cloud.google.com/apis/library/cloudbuild.googleapis.com)\n\n When you enable the Cloud Build API, the **Cloud Build Service\n Agent** service account is automatically added to your project. The\n service account lets you receive build notifications from\n Pub/Sub.\n\n The service account has the following format, where\n \u003cvar translate=\"no\"\u003eproject-number\u003c/var\u003e is your project number: \n\n service-\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eproject\u003c/span\u003e\u003cspan class=\"devsite-syntax-o\"\u003e-\u003c/span\u003e\u003cspan class=\"devsite-syntax-n\"\u003enumber\u003c/span\u003e\u003c/var\u003e@gcp-sa-cloudbuild.iam.gserviceaccount.com\n\n If you don't see the **Cloud Build Service Agent** service account on your\n IAM page, or you can't receive notifications from\n Pub/Sub, then follow these steps to add the\n **Cloud Build Service Agent** service account to your project:\n 1. Open the **IAM** page in the Google Cloud console:\n\n [Open the IAM page](https://console.cloud.google.com/iam-admin/iam)\n 2. Click **Grant access**.\n\n 3. Add the following principal, where \u003cvar translate=\"no\"\u003eproject-number\u003c/var\u003e\n is your project number:\n\n service-\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eproject\u003c/span\u003e\u003cspan class=\"devsite-syntax-o\"\u003e-\u003c/span\u003e\u003cspan class=\"devsite-syntax-n\"\u003enumber\u003c/span\u003e\u003c/var\u003e@gcp-sa-cloudbuild.iam.gserviceaccount.com\n\n 4. Select **Service Agents** \\\u003e **Cloud Build Service Agent** as your role.\n\n 5. Click **Save**.\n\n2. Enable the Pub/Sub API:\n\n [Enable the Pub/Sub API](https://console.cloud.google.com/apis/library/pubsub.googleapis.com)\n3. Create the `cloud-builds` topic:\n\n gcloud pubsub topics create cloud-builds\n\n You can also define a custom topic name in your build config file\n so that messages are sent to the custom topic instead. In this case, you\n would create a topic with the same custom topic name: \n\n gcloud pubsub topics create \u003cvar translate=\"no\"\u003etopic-name\u003c/var\u003e\n\n For more information, see\n [Pub/Sub topics for build notifications](/build/docs/configuring-notifications/notifiers#pubsub-topic-config).\n\n To learn more about managing Pub/Sub topics, see\n [Managing topics and subscriptions](/../pubsub/docs/admin).\n\n| **Note:** Pub/Sub notifications generated by Cloud Build are subject to the [Pub/Sub Service Level Agreement (SLA)](/../pubsub/sla).\n\nPush subscriptions\n------------------\n\n[Push subscriptions](/pubsub/docs/push) deliver messages to an HTTP endpoint\nthat you define. Messages are delivered as soon as they are published to the\ntopic.\n\nMessages sent from push subscriptions look like this: \n\n {\n \"message\": {\n \"attributes\": {\n \"buildId\": \"abcd-efgh...\",\n \"status\": \"SUCCESS\"\n },\n \"data\": \"SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==\",\n \"message_id\": \"136969346945\"\n },\n \"subscription\": \"projects/myproject/subscriptions/mysubscription\"\n }\n\nPull subscriptions\n------------------\n\n[Pull subscriptions](/pubsub/docs/pull) deliver message when polled by the\nsubscribed application. Messages are delivered when the subscription is polled.\n\nMessages sent from pull subscriptions look like this: \n\n {\n \"receivedMessages\": [\n {\n \"ackId\": \"dQNNHlAbEGEIBERNK0EPKVgUWQYyODM2LwgRHFEZDDsLRk1SK...\",\n \"message\": {\n \"attributes\": {\n \"buildId\": \"abcd-efgh-...\",\n \"status\": \"SUCCESS\"\n },\n \"data\": \"SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==\",\n \"messageId\": \"19917247034\"\n }\n }\n ]\n }\n\nSubscribe to build update notifications\n---------------------------------------\n\nYou have several options for subscribing to build update notifications.\nFor example, you can\n[push messages to an endpoint](/pubsub/docs/push#configuring-http-endpoints),\nor\n[write a Python app for polling your subscription](/pubsub/docs/pull#pubsub-pull-python).\n\nTo learn how to set up Pub/Sub subscriptions for build updates,\nread the [Pub/Sub Subscriber Guide](/pubsub/docs/subscriber).\nYou can also learn about\n[Pub/Sub Client Libraries](/pubsub/docs/reference/libraries),\nwhich make developing subscriber applications easier.\n\nTo learn how to use Pub/Sub to send build updates to\nemail or to services such as Slack, see\n[Cloud Build notifiers](/build/docs/configuring-notifications/notifiers)."]]