Cloud Build can notify you of updates to your build status by sending you notifications to selected channels. In addition to notifiers maintained by Cloud Build such as Slack or SMTP, you can also use the provided library in the cloud-build-notifiers repository to create your own notifier.
This page explains how you can create your own notifier.
Before you begin
-
Enable the Cloud Build, Cloud Run, Pub/Sub, and Secret Manager APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
Install the Go programming language.
Install the Google Cloud CLI.
Setting up
Open a terminal window on your machine.
Clone and navigate into the cloud-build-notifiers repository:
git clone https://github.com/GoogleCloudPlatform/cloud-build-notifiers.git && cd cloud-build-notifiersAdd a directory for your own notifier and navigate into it, where DIRECTORY_NAME is the name of your directory:
mkdir DIRECTORY_NAME && cd DIRECTORY_NAMEInitialize go modules in your new directory, where DIRECTORY_NAME is the name of your new directory:
go mod init github.com/GoogleCloudPlatform/cloud-build-notifiers/DIRECTORY_NAMEYou should now see a
go.modfile in your directory.Add the following line to your
go.modfile to ensure you are using the latest version of notifiers:replace github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers => ../
Your dependencies are now set up and you are ready to create your own notifier.
Creating your own notifier
The cloud-build-notifiers contains a lib/notifiers directory. In the
lib/notifiers directory, you will see a file named notifier.go. This
file contains the framework you can use to create your own notifier.
You will need to define two methods to create a notifier in your main file.
In the new directory, create a file named
main.go.In
main.go, import the notifiers library framework and any other dependencies:Define a main method for your notifier. In this example,
loggeris the name of the notifier:The
mainmethod uses theMainmethod defined in thenotifier.gofile, which is used to set up notifier binaries.Define a struct for your notifier, where you will define the variables for your interface. In this example,
loggeris the name of the notifier:Add the notifier functionality. The notifier interface is defined by two methods:
SetUp: TheSetUpmethod accepts a configuration, fetches secrets, and pulls specified filters out of the configuration and stores them as a Common Expression Language predicate that can be used to send notifications. To learn more about CEL, see thecel-specrepository.SendNotification: TheSendNotificationmethod is what is used to send notifications to your selected channel or service.The definition of the notifier is available in
notifier.goand in the Go documentation.In the following example, the notifier interface is defined using the
SetUpand theSendNotificationmethod to print build logs, withloggeras the name of your notifier:Your final
main.gofile should look similar to the following file. In this example,loggeris used as the name of the notifier.
Next, configure your notifier.
Configure notifications
Write a notifier configuration file to configure your notifier and filter on build events:
In the following example notifier configuration file, the
filterfield uses CEL with the available variable,build, to filter build events with aSUCCESSstatus:Where:
logging-sampleis the name of the notifier.
For additional fields you can filter by, see the Build resource. For additional filtering examples, see Using CEL to filter build events.
Upload your notifier configuration file to a Cloud Storage bucket:
If you don't have a Cloud Storage bucket, then run the following command to create a bucket, where BUCKET_NAME is the name you want to give your bucket, subject to naming requirements.
gcloud storage buckets create gs://BUCKET_NAME/Upload the notifier configuration file to your bucket:
gcloud storage cp CONFIG_FILE_NAME gs://BUCKET_NAME/CONFIG_FILE_NAMEWhere:
BUCKET_NAMEis the name of your bucket.CONFIG_FILE_NAMEis the name of your configuration file.
Build and deploy your notifier:
Create a Dockerfile for the
logging-sample:Build and deploy the notifier using the following
cloudbuild.yamlfile.Where:
_CONFIG_PATHis the path to your notifier configuration, such asgs://BUCKET_NAME/CONFIG_FILE_NAME.yaml.
To run the
cloudbuild.yaml, pass in the notifier path as a substitution variable.gcloud builds submit . --substitutions=_CONFIG_PATH=gs://BUCKET_NAME/CONFIG_FILE_NAMEGrant Pub/Sub permissions to create authentication tokens in your project:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountTokenCreatorWhere:
PROJECT_IDis the ID of your Google Cloud project.PROJECT_NUMBERis your Google Cloud project number.
Create a service account to represent your Pub/Sub subscription identity:
gcloud iam service-accounts create cloud-run-pubsub-invoker \ --display-name "Cloud Run Pub/Sub Invoker"You can use
cloud-run-pubsub-invokeror use a name unique within your Google Cloud project.Give the
cloud-run-pubsub-invokerservice account the Cloud RunInvokerpermission:gcloud run services add-iam-policy-binding SERVICE_NAME \ --member=serviceAccount:cloud-run-pubsub-invoker@PROJECT_ID.iam.gserviceaccount.com \ --role=roles/run.invokerWhere:
SERVICE_NAMEis the name of the Cloud Run service to which you're deploying the image.PROJECT_IDis the ID of your Google Cloud project.
Create the
cloud-buildstopic to receive build update messages for your notifier:gcloud pubsub topics create cloud-buildsYou 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-nameFor more information, see Pub/Sub topics for build notifications.
Create a Pub/Sub push subscriber for your notifier:
gcloud pubsub subscriptions create subscriber-id \ --topic=cloud-builds \ --push-endpoint=service-url \ --push-auth-service-account=cloud-run-pubsub-invoker@project-id.iam.gserviceaccount.comWhere:
subscriber-idis the name you want to give your subscription.service-urlis the Cloud Run-generated URL for your new service.project-idis the ID of your Google Cloud project.
Notifications for your Cloud Build project are now set up. The next time you invoke a build, you will receive a notification in your channel if the build matches the filter you've configured.
Test notifications
To test notifications for the example used in this guide, you can
invoke a build by running the gcloud builds submit command.
In the following example, we specify success.yaml as the configuration
path. Running this command should result in a minimal successful build. You
should also be able to see an output of your build logs.
gcloud builds submit --no-source --config=success.yaml
Where success.yaml is:
steps:
- name: busybox
args: ["true"]
In the following example, we specify failure.yaml as the configuration
path. Running this command should result in a failed build. Instead of
seeing an output of your build logs, you will see an output informing
you that there was no match for the CEL filters you specified in your source.
gcloud builds submit --no-source --config=failure.yaml
Where failure.yaml is:
steps:
- name: busybox
args: ["false"]
If you created a notifier that's configured to perform another task
other than logging output to Cloud Run service logs, you
can also run the gcloud builds submit command to test your notifications.
To examine errors associated with your build, check the
Cloud Run logs for your service. To learn more, see
Viewing logs in Cloud Run.
What's next
- Learn about Cloud Build notifiers.
- Learn how to subscribe to build notifications.