How to Setup Slack Integration for Google Cloud Build using Cloud Functions
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:
- 120 free builds minutes daily.
- Up to 10 (or more) parallel builds available to you.
- No authentication required if your applications already run on Google Cloud Infrastructure.
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.
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
- Prepare GCP Project
- Setup Slack App with Webhook
- Create and Deploy Cloud Function
Ready? Let's dive into it :)
Prepare GCP Project
- 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
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.
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] 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]
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.
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
index.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
>> gcloud functions deploy subscribe --stage-bucket [BUCKET_NAME] \
[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.
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.
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…