Jenkins pipeline slack notification

Jenkins pipeline slack notification | complete tutorial in 2022

Jenkins is a CICD tool that can be used to implement CICD technology in our project. This article will learn about the Jenkins pipeline slack notification. We will learn how to integrate slack to send notifications to the slack channel using Jenkins. So let’s get started.

Why setting up the notification in Jenkins is required?

Setting up the Jenkins job itself is not sufficient as the user needs to be get notified if anything goes wrong. So it is important to integrate some kind of notification in your Jenkins job, it can be slack, email notification, or the pager duty notification.

You can follow the below link to set up the email notification in your Jenkins job as per your need.

Jenkins setup email notification

You can follow the below tutorial to setup pagerduty notifications to your Jenkins job

https://plugins.jenkins.io/pagerduty/

Integrate Jenkins with slack

To send slack notifications via Jenkins users need to perform the below steps

Create a slack account and create a slack channel where notification needs to be sent.

Install Jenkins CI of the slack channel

Install the slack notification plugin

To install a plugin go to your Jenkins instance > manage Jenkins > manage plugins and click on available plugins. search and download the “slack notification plugin”

Jenkins pipeline slack notification example

Now follow the below steps to create a Jenkins pipeline with slack notification enabled.

clink on the new item on Jenkins home page and click on create a pipeline job.

click on pipeline and provide the job name and click on the OK button.

Now go to the pipeline session and paste the below code.

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo "Hello world"
                    }
            }
        }
    post{
        always{
            slackSend( channel: "#Jenkins", token: "slack_webhook token", color: "good", message: "Test Email")
        }
    }
}

click on the save button to save the Jenkins pipeline. Now trigger the job and check in the slack channel for the notification.

Jenkins pipeline slack notification on failure

So far we have understood how to send slack messages using the Jenkins job, but in most of the use cases, users need to get notified only if there is some issue.

There any many post actions operations available on Jenkins and based on such conditions alerts will get triggered from Jenkins. Some of the post actions available in Jenkins are:-

  • always
  • changed
  • failure
  • success
  • unstable
  • etc.

So, if a user wants to get notified only when the Jenkins job failed then he has to choose action block as a failure.

So the Jenkins pipeline slack notification on a failure job looks like below

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo "Hello world"
                    }
            }
        }
    post{
        failure{
            slackSend( channel: "#Jenkins", token: "slack_webhook token", color: "good", message: "Test Email")
        }
    }
}

Jenkins slack notification custom message

In this session, we will understand how to write a custom message in the Jenkins job and add that message to the slack notification. So let’s get started.

In Jenkins Users also have the flexibility to define a function and write their custom message on it and pass that function to the message block on the slack plugin. The general format of the Jenkins job with a custom message will look like this below

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo "Hello world"
                    }
            }
        }
    post{
        failure{
            slackSend( channel: "#Jenkins", token: "slack_webhook token", color: "good", message: "${custom_msg()}")
        }
    }
}


def custom_msg()
{
  def JENKINS_URL= "localhost:8888"
  def JOB_NAME = env.JOB_NAME
  def BUILD_ID= env.BUILD_ID

  def JENKINS_LOG= " FAILED: Job [${env.JOB_NAME}] Logs path: ${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/consoleText"
  return JENKINS_LOG

}

Jenkins slacksend formatting

Instead of sending the plain message through Jenkins, users also have the flexibility to format the message based on their needs. Below are the formattings a user can do using slack plugging via Jenkins

  • Format the color of the messages.
  • Send an attachment along with the message
  • Adds blocks to send custom messages
  • user can send a message and create a thread on that message
  • update the content of a previously sent message
  • Add an emoji reaction to a previously-sent message.

Most of the above features required the bot user. More information about all these operations can be found here

Conclusion

More to Read

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top