Better Jenkins Notifications in Declarative Pipelines

I’ve been using declarative pipelines in Jenkins for a while with the Slack plugin to send build notifications to Slack. The plugin does what it says on the tin but gives you a pretty boring message by default.

E8F315D1-04A6-4DC4-B0D8-1E1E7ED42D08.png

I used the environment variables available in the pipeline to make things a little bit better and link back to the job.

08A97422-A7E2-4AB5-A65B-68EF7B5AE196.png

But I was still always disappointed the notifications didn’t contain more information. Thankfully version 2.3 of the plugin added support for the attachments portion of the Slack message API. I was able to leverage the attachments feature to get better message formatting. Meanwhile, I took some inspiration from this thread to incorporate test result summaries.

I store this in a shared pipeline library to avoid repeating the code in every Jenkinsfile. This way you can simply call it in a post step like this.

post {
always {
notifySlack currentBuild.result
}
}

Here is the code in the pipeline library

The end result is a much more informative message.

Screen Shot 2018-02-09 at 11.22.37 PM.png