How to Setup Slack Integration for Google Cloud Build using Cloud Functions

Learn how to easily setup slack integrations for your GCB CI/CD apps

How to Setup Slack Integration for Google Cloud Build using Cloud Functions
Google Cloud Build Slack Notification Flow Diagram
Note: We will abbreviate Google Cloud Build as GCB for the rest of this post to keep it clean.

If you are reading this, then you probably fall in one of the 3 categories below:

  • You already use GCB as your CI/CD platform of choice for one or more of your applications.
  • You Intend to use GCB for an app you are building or intend to build in the future
  • You have heard a bit about GCB but haven't used it before or don’t even know what GCB is.

For the sake of those that fall in the third category, here’s a brief explanation of what GCB is.

Google Cloud Build is a serverless CI/CD platform offering by Google that allows you to build, test, and deploy your applications in the cloud. Similar to tools like CircleCI, TravisCI, or Jenkins.

Why you may want to use Cloud Build instead of CircleCI and the likes could be their generosity as regards to the following:

  1. 120 free builds minutes daily.
  2. Up to 10 (or more) parallel builds available to you.
  3. No authentication required if your applications already run on Google Cloud Infrastructure.

The Problem

GCB doesn’t have an out-of-the-box one-click Slack integration like you have available for most other CI/CD platforms mentioned earlier (reasons best known to Google). However, some scripts exist to enable you to set this up on Google Cloud Run. Nevertheless, none of these scripts worked for me, besides the fact that they are overly stressful for something as basic as this.

The Solution

In this post, I will show you how to easily set this up using Google Cloud Functions in 3 simple steps.

A Little Background

GCB sends all build event updates along with its build metadata to Pub/Sub on the cloud-builds topic. We will configure a cloud function to listen to that topic, filter out messages it receives, and sends out notifications to our Slack channel using Webhooks.

TLDR: You can find the code for this in this GitHub here: GCB-Slack-Notifier

Steps

  1. Prepare GCP Project
  2. Setup Slack App with Webhook
  3. Create and Deploy Cloud Function

Ready? Let's dive into it :)

Prepare GCP Project

Assumptions

  • You have a google cloud project setup and you are logged in to your account. Otherwise, click here to set one up
  • You have billing enabled for the selected project

Enable APIs

To proceed, we need to enable the Cloud Functions and Pub/Sub APIs from the Google Cloud console. You can enable the APIs directly using this link if not yet enabled on your project.

CloudSDK or CloudShell
Depending on your preference, you can either setup CloudSDK to connect to your GCP Project or connect to Cloud Shell directly from the GCP console.

Setup Slack App and Webhook

If you don’t already have a Slack webhook ready, please create one using the instructions here Incoming Webhooks for Slack

A slack webhook URL is how we will be sending messages to our Slack channel.

Now we are ready for the fun part :)

Create and Deploy Cloud Function

To achieve this, we first create a Cloud Storage bucket to stage our cloud function. All bucket names must be globally unique, so ensure you choose a good name for your bucket. A good suggestion for naming your cloud bucket could be [PROJECT-ID]_cloudbuild where [PROJECT-ID] is the id of your GCP project.

Creating the staging Bucket
Using the Cloud Shell or Cloud SDK, run the following command replacing [BUCKET_NAME] with your chosen bucket name.

gsutil mb gs://[BUCKET_NAME]

Setup Code
Clone the GCB Slack repository here using the following command

>> git clone https://github.com/wayneskillz/GCB-Slack-Notifier
>> cd GCB-Slack-Notifier

>> ls

You should now have the following 3 files in your current directory.

index.js
package.json
README.md

The package.json contains dependencies for our Cloud function while index.js contains the actual Cloud subscriber function.

Update Webhook URL
Using vim, nano, or your favorite text editor of choice, set the SLACK_WEBHOOK_URL inindex.jsto be the webhook URL you created on Slack. The webhook URL typically starts with the form https://hooks.slack.com/const SLACK_WEBHOOK_URL = ''; // Enter Your Slack Webhook URL here

Finally, we deploy our Cloud Function

Deploying the Cloud Function

Run the following command from the GCB-Slack-Notifier/ directory.

>> gcloud functions deploy subscribe --stage-bucket [BUCKET_NAME] \ 
    --trigger-topic cloud-builds

where [BUCKET_NAME] is the name of our Cloud Storage bucket we created earlier. Your function will now be created.

This creates our cloud subscriber function that listens to new messages on the cloud-builds topic where cloud build sends all build event updates.

Once you see the status of READY , it means your cloud function is now set for use.

Congratulations!

If you trigger a new build of your app from Cloud build, you should now see Slack notifications for a ’SUCCESS’, ‘FAILURE’, ‘INTERNAL_ERROR’, ‘TIMEOUT’, ‘QUEUED’, or ‘CANCELLED’ status of our builds such as the one below.

Sample Google Cloud Build Slack Notification

You can customize the slack app to have whatever icon and name you prefer.

And that's it!

Please leave a comment if you have found this useful or have any questions.

Till next time…