DEV Community

joy
joy

Posted on

Send notifications to Slack using Jenkins

Introduction

Jenkins 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.
  2. A Slack workspace.
  3. Slack bot application created from scratch within your Slack workspace.
  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 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

Step 2: Go to the plugins section and click on available plugins. Install Slack notifications and restart Jenkins once done with installation.

Jenkins plugin

Step 3: Go to https://api.slack.comcreate an (slackbot)application from scratch and add your workspace.

Slack API

(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

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

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

Secret text

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

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

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

Step 9: Go to the advanced project options; add a pipeline script. You can modify the text message in the slackSend value.

Pipeline script

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

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.

Top comments (0)