DEV Community

Cover image for Integrate Slack with Jenkins to receive CICD pipeline notification
Rohit
Rohit

Posted on

Integrate Slack with Jenkins to receive CICD pipeline notification

Integrating Slack with Jenkins allows you to receive build notifications directly in your Slack channels, which helps in monitoring the status of your CI/CD pipeline. Here’s a step-by-step guide to set up this integration:

1) Create Slack Account - First create a Slack account and then subsequently the slack workspace and channel:-

Image description

  • Give your workspace name and some more details:-

Image description

Image description

  • Select your team mates to include them. If you are individual then you can skip this. Select the free trial:-

Image description

  • Once you selected, Dashboard will look like:-

Image description

  • Create a channel (I have given name as 'cicd-implementation')

Image description

2) Create Token - Now we need to create a token for Jenkins to authenticate slack and for that we need to add an app into our slack account. Refer below steps:-

  • Do a google search 'slack app'

Image description

  • Open the first link ‘Add app to Slack’ and search for Jenkins:-

Image description

Image description

  • Click on ‘Add to Slack’

Image description

  • Select the channel and ‘Add Jenkins to CI Integration’. For me it would be 'cicd-implementation'

Image description

Image description

  • In the same page, refer the steps to integrate slack with Jenkins. Also copy the token given in step 3 and save it somewhere. Now come down and click on ‘Save Settings’

Image description

3) Integrate Jenkins & Slack - Now we will go to Jenkins and integrate the slack by using plugins and slack token.

  • Go to Jenkins (Manage Jenkins-> Plugins-> Available Plugins) and look for slack notification plugins and install it.

Image description

  • Now, lets integrate the slack with Jenkins. Go to Manage Jenkins-> Systems and look out for Slack plugin.

Image description

  • Get your Slack workspace name correctly as this could cause issue when you are going to test connection in Jenkins. Refer below to get your slack workspace name:-

Image description

  • Provide workspace name based on slack setup and click add credential

Image description

  • Select 'Secret text', add the token which you have copied earlier and then give some ID & description. Later click on Add button.

Image description

Image description

  • Select the 'slackpass', give the channel id and then click on test connection.

Image description

  • A 'success' message should show over here to make sure there is proper integration between Jenkins and Slack.

Note:- It might be possible that you may get Failure message here, this could be because of some setup or plugins issue. So you can just logout from your slack account, login back and do the whole setup again. You should be able to see success message.

Image description

  • Lets add these things in our pipeline code. Add below set of codes to your pipelines. Here define a function to do color mapping in slack. In slack 'good' means green color and 'danger' means red.

Image description

Image description

  • Its time to create a new pipeline and paste your Pipeline as a code to Jenkins and test.

Image description

Image description

Image description

  • Go to Slack channel and see if there any message coming in with 'Success'?

Image description

Image description

  • If the pipeline failed then you will see the red color in your slack channel with 'Failed' message

Image description

Conclusion!

This configuration sends notifications to Slack on build success and failure with links to the Jenkins build.

By following these steps, you should be able to integrate Slack with Jenkins and receive notifications in your specified Slack channels.

Happy Learnings!

Top comments (0)