Today we will learn how to send success, error notification to slack through the Jenkins CI job and from the Jenkinsfile(Declarative Pipeline).
While working on Jenkins sometimes, we need to send notifications to the Slack channel when the Jenkins CI Job run successfully or there was some error, so the team member can be known without checking the CI Job on the Jenkins UI.
it is very helpful to check the Jenkins CI staus on the slack channel.
For sending the notification to the Slack channel, we will use Slack Notification plugin.
Installation of Slack Notification Plugin
Install Instructions for the plugin
- 1. In your Jenkins dashboard, click on Manage Jenkins from the left navigation.
- 2. Navigate to Manage Plugins
- 3. Click on the tab name as Available
- 4. Search for Slack Notification
- 5. Check the box next to install
Jenkins CI App installation in Slack
- 1. Go to Slack App Directory and search for Jenkins CI
- 2. Click on the Add to Slack button
- 3. Now select the channel and click on the Add Jenkins CI Integration
- 4. Copy the Token from the Jenkins CI Page and Click on the Save Setting button at the bottom
We have installed the Jenkins CI App on Slack and got the Token
Now we will configure the Token into the Slack Notification plugin
Slack notification plugin configuration in Jenkins
In the first step, we have installed the Slack notification plugin. now time to configure the plugin.
Please follow the below steps
- Click on Manage Jenkins again in the left navigation and then click to Configure system.
-
- Find the Global Slack notifier settings section and add the following values
- a. Team subdomain:
- b. Integration token credential ID:
- c. The other fields are optional. You can click on the question mark icons next to them for more information.
- d. Press Save once you’ve finished.
- Click on the Test connection button to test the notification in the slack channel. Below is the sample notification
Configuration for project
There are 2 ways to configure in project
1. Through the UI panel
-
a. For each project that you want to receive notifications for, choose Configure from the project’s menu.
b. Then you’ll need to add Slack notifications to the Post-build actions for this project.
- c. In the Slack notifications section, choose the events you want to be notified about.
2. Send notification through Jenkinsfile file
If you are using Jenkinsfile then you can use this code to send the notification to slack channel
slackSend "Build Started - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
You can customize the message and can add more configurations like notify to a particular user, colour etc
-
attachments
(optional)-
Type:
Object
-
Type:
-
baseUrl
(optional)Allows overriding the Slack compatible app URL specified in the global configuration.
-
Type:
String
-
Type:
-
blocks
(optional)-
Type:
Object
-
Type:
-
botUser
(optional)Bot user option indicates the token belongs to a custom Slack app bot user in Slack.
If the notification will be sent to a user via direct message, the default integration sends it via @slackbot, use this option if you want to send messages via a bot user.
-
Type:
boolean
-
Type:
-
channel
(optional)Allows overriding the Slack Plugin channel specified in the global configuration. Multiple channels may be provided as a comma, semicolon, or space-delimited string.
slackSend channel: "#channel-name", message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"
-
Type:
String
-
Type:
-
color
(optional)An optional value that can either be one of good, warning, danger, or any hex color code (eg. #439FE0). This value is used to color the border along the left side of the message attachment.
slackSend color: "#439FE0", message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"`
-
Type:
String
-
Type:
-
failOnError
(optional)If set to true, then the step will abort the Workflow run if there is an error sending message.
slackSend failOnError: true, message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"`
-
Type:
boolean
-
Type:
-
iconEmoji
(optional)Choose a custom emoji to use as the bot's icon in Slack, requires using a bot user
-
Type:
String
-
Type:
-
message
(optional)This is the main text in a message attachment and can contain standard message markup. The content will automatically collapse if it contains 700+ characters or 5+ linebreaks, and will display a "Show more..." link to expand the content. The message may include global variables, for example, environment and current build variables:
slackSend "started ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"`
-
Type:
String
-
Type:
-
notifyCommitters
(optional)Notify committers via direct message in addition to specified channels, requires using a bot user
-
Type:
boolean
-
Type:
-
replyBroadcast
(optional)-
Type:
boolean
-
Type:
-
sendAsText
(optional)-
Type:
boolean
-
Type:
-
teamDomain
(optional)Allows overriding the Slack Plugin Integration Team Domain specified in the global configuration.
-
Type:
String
-
Type:
-
timestamp
(optional)Allows updating an existing message instead of posting a new one.
-
Type:
String
-
Type:
-
token
(optional)Allows overriding the Slack Plugin Integration Token specified in the global configuration.
-
Type:
String
-
Type:
-
tokenCredentialId
(optional)The ID for the integration token from the Credentials plugin to be used to send notifications to Slack. The "Kind" of the credential must be "Secret text." If both "Integration Token" and "Integration Token Credential ID" are set, the "Integration Token Credential ID" will take precedence for security reasons.
This overrides the global setting.
-
Type:
String
-
Type:
-
username
(optional)Choose a custom username to use as the bot's name, requires using a bot user
-
Type:
String
-
Type:
Conclusion
We learnt how to send notifications to the slack channel through the Jenkins CI, Jenkinsfile (Pipeline). Please give it a try and I will be happy to answer your queries on my Twitter handle Twitter
Top comments (0)