Cloud Build can notify you of updates to your build status by sending you notifications to desired 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.
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-notifiers
Add a directory for your own notifier and navigate into it, where DIRECTORY_NAME is the name of your directory:
mkdir DIRECTORY_NAME && cd DIRECTORY_NAME
Initialize 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_NAME
You should now see a
go.mod
file in your directory.Add the following line to your
go.mod
file 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,
logger
is the name of the notifier:The
main
method uses theMain
method defined in thenotifier.go
file, 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,
logger
is the name of the notifier. E.g.
Next, you will add the notifier functionality. The notifier interface is defined by two methods:
SetUp
: TheSetUp
method 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-spec
repository.SendNotification
: TheSendNotification
method is what is used to send notifications to your desired channel or service.The definition of the notifier is available in
notifier.go
and in the Go documentation.In the example below, the notifier interface is defined using the
SetUp
and theSendNotification
method to print build logs, withlogger
as the name of your notifier:Your final
main.go
file should look similar to the following file. In this example,logger
is used as the name of the notifier.Now that you have your notifier defined, you can follow the following steps below to configure your notifier.
Configuring notifications
Write a notifier configuration file to configure your notifier and filter on build events:
In the following example notifier configuration file, the
filter
field uses CEL with the available variable,build
, to filter build events with aSUCCESS
status:Where:
logging-sample
is 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 do not have a Cloud Storage bucket, 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_NAME
Where:
BUCKET_NAME
is the name of your bucket.CONFIG_FILE_NAME
is 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.yaml
file.Where:
_CONFIG_PATH
is 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_NAME
Grant 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.serviceAccountTokenCreator
Where:
PROJECT_ID
is the ID of your Google Cloud project.PROJECT_NUMBER
is 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-invoker
or use a name unique within your Google Cloud project.Give the
cloud-run-pubsub-invoker
service account the Cloud RunInvoker
permission:gcloud run services add-iam-policy-binding SERVICE_NAME \ --member=serviceAccount:cloud-run-pubsub-invoker@PROJECT_ID.iam.gserviceaccount.com \ --role=roles/run.invoker
Where:
SERVICE_NAME
is the name of the Cloud Run service to which you're deploying the image.PROJECT_ID
is the ID of your Google Cloud project.
Create the
cloud-builds
topic to receive build update messages for your notifier:gcloud pubsub topics create cloud-builds
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.com
Where:
subscriber-id
is the name you want to give your subscription.service-url
is the Cloud Run-generated URL for your new service.project-id
is 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.
Testing notifications
To test notification functionality 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 for notification
functionality. 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.