Send notifications to Slack using Jenkins
Tags: jenkins, jenkinspipeline, slack, devops
Published: 2024-02-26 09:06:30

## Introduction 

[Jenkins](https://www.jenkins.io/) is a tool used by software engineers to  automate  builds, test, and deploy software by providing a centralized platform for managing workflows. 

Imagine you're running a bakery, and you have a team of bakers who work together to bake different types of cakes. Each cake requires several steps, like mixing ingredients, baking, decorating, and packaging.

Now, Jenkins workflows are like having a super efficient system in your bakery that automates and manages all these steps seamlessly.Jenkins workflows streamline the entire process of developing and delivering software, just like your bakery's system ensures that each cake is made to perfection and delivered to the customer with care and efficiency.

This article will explore how Jenkins enables such efficiency by demonstrating how it allows this by sending notifications to Slack by using the Jenkins pipeline and a Slack bot.

#### Prerequisites

To follow along easily with this tutorial, you should have the following:


1. [Jenkins installed on your device.](https://www.jenkins.io/doc/book/installing/linux/)
2.  [A Slack workspace.](https://slack.com/get-started#/createnew)
3. [Slack bot application created from scratch within your Slack workspace.](https://api.slack.com/apps)
4. Jenkins pipeline created.



####Step1 : We start by installing Jenkins and Java from the Jenkins official website.
Once done installing, we configure the admin credentials. We then go to the Jenkins dashboard and click on manage Jenkins.

> **Note** 
We would recommend following the Jenkins documentation [installation guide](https://www.jenkins.io/doc/book/installing/linux/) to have the best installation without having any blunders with Jenkins keyrings. We used the long term support release installation.Make sure to start the Jenkins application with `systemctl start jenkins` after installing. We  access Jenkins on the localhost:8080 port.


![Jenkins dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5k7ik67wrmu91tlitps.png)



####Step 2: Go to the [plugins section](https://plugins.jenkins.io/slack/) and click on available plugins. Install Slack notifications and restart Jenkins once done with installation. 


![Jenkins plugin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6s33gx46mbv0ts6nny3.png)



####Step 3: Go to [https://api.slack.com](https://api.slack.com/)create an (slackbot)application from scratch and add your workspace.


![Slack API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vtg1di80gbi300nfgsvw.png)

 (Attached is a screenshot of my app name and slack workspace. You can name the application whatever you like. We created a slack workspace for testing the application.)


![App name and workspace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ocr709hu5tincajdm3k.png)


#### Step 4: Once the app is created, we click on OAuth and permissions and add go to the bot token scopes.
We add the chat: write, files:write, chat:write.customize, reactions:write, users:read, users:read.email **scopes**.
Once done adding the scopes, we click install workspace which adds Jenkins to the slack workspace.


![Bot scopes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ior3y3gt2l5lci0kq0g.png)

####Step 5: Go to the Jenkins Dashboard and click on manage Jenkins + credentials.Click on the global domains where we add your Slack credentials.
Choose the secret text option; the scope option should be global.
The value of the secret is the OAuth token created in Slack. The ID and description can be anything we wish to add.


![Secret text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aogxhqgunoocknmeum0t.png)


![Secret text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk19h341u35y9bl7ivcc.png)

####Step 6: Click on manage Jenkins then configure systems. Scroll to the bottom where Slack exists and add the Slack workspace name and Slackbot credentials created.
Add your **default channel**. We chose to use random as our default slack channel but you can choose any. Make sure to tick the box with the custom Slack bot user. 


![Adding slack configs to jenkins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvj8xw0wnzu0f6p9af4i.png)

> Note: If we encounter a **crumb error/403 error**, make sure to **enable proxy compatibility** in the Jenkins security tab.

####Step 7: Once the values are added, apply and save the changes and test the connection for the Slack bot. 
If successful we get a success and confirmation message on the selected (random)channel.

![Connection set to slack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03c4am3gtx76gxlyf47l.png)

####Step 8: Go to the Jenkins dashboard and click on a new item; name it what you wish and click the pipeline option.
With that you have created the Jenkins pipeline.


![New pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kob7ux55z6agn7dz4cz.png)

####Step 9: Go to the advanced project options; add a pipeline script. You can modify the text message in the [slackSend](https://gist.github.com/darinpope/7300c9a1b893221f859f2abccc24a0dc) value. 



![Pipeline script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hn6vxw5xh139e0ainpyd.png)



#### Step 10: Save and apply the changes. Once done, you customize and build the pipeline message and it shows up in your selected Slack channel.


![Testing connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5eh145qq3icnkyed6za.png)


#### Conclusion.
In conclusion, we were able to create and send Slack notifications using Jenkins. We were able to achieve this using the Jenkins pipeline and the use of a slackbot.