Integrate Slack with Jenkins to receive CICD pipeline notification
Published: 2024-06-01 08:44:02

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:

**<u>1) Create Slack Account</u>** - First create a Slack account and then subsequently the slack workspace and channel:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo528xxhnjm2izhfljqe.png)

- Give your workspace name and some more details:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot7ngn7upczd8c9r1bca.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wda5icrov5lh2ap8s3w.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8robu0uplj3zphvsi6ec.png)

- Once you selected, Dashboard will look like:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thbf1e0f9gl7ty32mdxr.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73m3by1t3igirz2xsjei.png)


**<u>2) Create Token</u>** - 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkh4lvk2bxdekttxn1kv.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0xjbshz6y3kzserb30t.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tb4mjxcllxl8gf6mr4v.png)

- Click on ‘Add to Slack’

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttaix6tbd5txjo05ehy3.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enzuivfvjtvklisq6ujd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjlq1vamijhucs46l5ss.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvbt4pnca1ctdgxuxuua.png)

**<u>3) Integrate Jenkins & Slack</u>** - 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6ypdp0xwiqquwy7cgbc.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0o80apt4x23mv8a0xaq.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/er8h9yypqqwsq91ys4cx.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ulz924lb8vplqc3zo8l.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiom9ue6k43bsuooj9n4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09wzjx1t58jmu4y8cdql.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syb4bdzgt3dy4w6n4o4i.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0fvz055gy20tglfn32e.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo6kjxav81kdm4qtrvo9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idv10qkgo17pje61siim.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8urrn0byt20ufiulvq9f.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02xiz14jkc865rj1exh5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rn3a9cvxiavw0jccqv1k.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxm2dnd576csr78mrtgp.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8ws2v7igyn9ohcs2z3o.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cb7lroa425dpexxq8ur.png)

**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!**