Better Google Cloud Build notifications in Slack

Being a remote-first company, Amperon uses Slack liberally to communicate amongst our team. Recently our engineering team has also started leveraging Slack for chatops, or the ability to monitor and react to what’s happening in our systems within Slack in realtime and plain view of the whole team.

One particularly important piece of our chatops infrastructure are build notifications. Given that we deploy multiple times per day, it’s important to know if there are any issues with new changes. Our CI/CD suite is built on Google Cloud Build, and if you’ve worked with GCB in the past you may have noticed that its integrations with external services are pretty lacking. Today we’re going to focus on improving on on specific integration that is pretty lackluster out of the box: forwarding build failure notifications to Slack.

If you follow Google Cloud’s official article detailing how to set this up, you’ll be left with a notification like this:

That’s… not too helpful. If you have different builds running on different branches, different triggers for different flows (think continuous integration vs continuous deployment), and use something like GitHub to manage your code, this integration leaves a lot to be desired.

Luckily it’s not too hard to spruce this up a bit and end up with a notification that looks like this:

This notification provides quite a bit more insight into what went wrong: you get the commit hash, branch name, and specific step in the build that failed. I’ve also found the links super helpful to quickly jump to information I need in order to figure out what went wrong and how to fix it.

Thankfully this improved notification doesn’t require any additional setup beyond Google’s official documentation for setting up a slack notification, it just requires you to swap out the lackluster cloud function code their tutorial provides with the following:

Just make sure you’ve replaced the GH_ORG_NAME constant with your GitHub organization’s name (ie would need to be stevenleeg) and your Google Cloud Platform project ID in GCP_PROJECT_ID.

Note that this particular implementation only notifies on build failures, not build successful runs. If you’re looking to add in successful build reports you should be able to modify this by removing the BAD_STATUSES check. Feel free to get creative with the resulting slack message too. I’d highly recommend checking out Slack’s block kit builder to make the design process a bit easier. GCB’s Build.BuildStep documentation is also very helpful to know what information you have at your disposal in the build argument of createSlackMessage.

Happy hacking!

No items found.

Related Articles