Send Slack Notifications with Jenkins
?? Saral Saxena ??????
?11K+ Followers | Linkedin Top Voice || Associate Director || 14+ Years in Java, Microservices, Kafka, Spring Boot, Cloud Technologies (AWS, GCP) | Agile , K8s ,DevOps & CI/CD Expert
How automation build environment to run build every single push happens on the source repository we need to broadcast the status of the build to the team members will ensure code base sanity
Jenkins is an open-source CI server that allows you to easily set up pipelines for the automation of testing, building, and deploying. At certain stages of the pipeline, you may decide you want to send out a Slack notification to you and your team to notify you of the build status.
Install Jenkins app for Slack
Go to your Slack app on your desktop.
On the bottom of your left side navigation bar, you will see Recent Apps.
Click Add more apps.
Search for “Jenkins CI” and click Add.
You will be sent to the Jenkins app page in your browser. Click Install.
Select the Slack channel you want to post to and click Add Jenkins CI integration. (You can also create a new channel by selecting the bottom right link where it says create a new channel).
On the next page, scroll down to Integration Settings to find all the settings and info you need to customize your Slack notification in Jenkins.
We will need the auto-generated Token later for configuration in Jenkins.
Install Slack Notifications plug-in for Jenkins
Now, go to your Jenkins admin portal in your local browser.
On the top left corner, go to Jenkins → Manage Jenkins → Manage Plugins
Under the Available tab, filter and search for “slack notification.”
Select Slack Notification, then Install without restart on the bottom left.
It will take you to the installation page, and it should only take a few seconds to finish installing.
Once the “Success” message appears, go to back to the main page by clicking Go back to the top page
Connect Jenkins to your Slack
Before we set up a new pipeline job, we need to configure Jenkin’s settings to connect to your Slack.
We will do this in Manage Jenkins → Configure System
Scroll all the way down on the Configure System page until you see the settings for Slack
- Workspace: Your team’s workspace name. If you sign in to slack at https://example.slack.com/, your workspace is ‘example’.
- Credential:
- - Click Add → Jenkins and a pop-up will appear.
- - Under the Kind dropdown, select secret text.
Copy and paste your Slack Token into Secret, and click Add.
If you forgot to save/copy your token, go to:
Slack app directory (https://<YOURWORKSPACE>.slack.com/apps/manage) → Jenkins CI → Configurations → Edit Configuration → Integration Settings
Optional: I added SlackToken for Description to keep track of my credentials/tokens
so that it’s readable in the drop-down after you’ve added it. You can choose any name you want.
Make sure to select the newly added token credential from the drop-down.
- Default channel/member id: Enter the channel name (i.e. #channel) or user ID (i.e. UUU123UU4) you want the notification to be sent to.
Once everything is filled out, click Save.
Create a new job to test the Slack notification.
Click create new jobs.
Name your new job, select Pipeline, then click OK on the bottom left.
Scroll all the way down to Pipeline. We will write or copy our JenkinsFile script into the Script text box.
Copy and paste the following code into the pipeline script. This is the bare bones you need for sending a Slack message
Click Save.
Click Build Now. Wait a second…
Click into your newest build which should appear flashing under Build History.
Click on Console Output to view what happens as it would in your terminal or command prompt.
If all goes well, a Slack message has been sent to you by a jolly butler.