Customizing CircleCI notifications in Slack
@mention me in Slack when I break the build. Why is that so hard to do?
My fellow engineers are very productive, so code is always being pushed and deployed. We use CircleCI and its GitHub integration to manage our continuous deployment process. CircleCI, via a Slack webhook, posts every successful and failed build into our #build channel.
But I don’t need to know about every successful and failed build. I want the #build channel to only ping me for my successful and failed builds. Actually, only the failed builds. (I really care about those!) Unfortunately, CircleCI’s Slack integration can’t help: it knows who wrote the commit, but it doesn’t know who they are on Slack. It makes sense why this doesn’t exist out of the box; how would CircleCI learn such a mapping? Where would it be stored? Even if CircleCI provided some sort of lookup method, what if it still needed to be tailored to how my team handles build notifications?
My CircleCI Router app acts as a bridge between CircleCI and Slack. It replaces the GitHub username in the message sent from CircleCI with the corresponding Slack ID. Engineers who have authenticated with my app can mute the #build channel and only get notified for their own builds, for which the message will @mention them.
Here’s how it works
My teammates sign in with Slack and add their GitHub credentials. They can choose notifications for all builds or only for failures, via Transposit’s user settings.
Next, I replaced CircleCI’s Slack Webhook URL with my own, created in Transposit.
When CircleCI hits the webhook, my operation pulls the GitHub username from the message (it’s always the second word) and replaces it with the user’s Slack ID. I’ve created the mapping between identifiers using the application stash, which is a built-in k/v store. Depending on the state of the build (and that user’s preferences) we’ll @ them in the #build channel using the Slack connector.
Everything is wrapped in a try/catch so the original message will always be sent if there was an error in the code.
Fork my app to customize your team’s build notifications!