Send notification to slack from the Jenkins CI Job and Jenkinsfile | e Avni Tech
Tags: jenkins, slack
Published: 2021-08-13 03:45:42

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](https://plugins.jenkins.io/slack/) plugin.

## Installation of Slack Notification Plugin

Install Instructions for the plugin
- 1. In your Jenkins dashboard, click on Manage Jenkins from the left navigation. 
![manage-jenkins](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q82kfrei690i0cbaozmd.png) 
- 2. Navigate to **Manage Plugins**
- 3. Click on the tab name as **Available**
- 4. Search for **Slack Notification** 
![slack-notification-plugin-manager-search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l168pbrj69z034nz6xbn.png)
- 5. Check the box next to install

## Jenkins CI App installation in Slack
- 1. Go to [Slack App Directory](https://eavnitech.slack.com/apps) and search for **Jenkins CI**
- 2. Click on the **Add to Slack** button

![slack-notification-plugin-manager-search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/039wwat9yz4u1o0ykf7h.png)
- 3. Now select the channel and click on the **Add Jenkins CI Integration**
![slack-notification-plugin-manager-search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxg5t9rv2wep4diirzy4.png)
- 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

- 1.  Click on **Manage Jenkins** again in the left navigation and then click to **Configure system**.

- 2.  Find the **Global Slack notifier settings** section and add the following values
    - a. Team subdomain: **<Your team domain>**
    - b. Integration token credential ID:  **<Paste the token which we got from step 2>**
    - 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.
   ![jenkins-ci-configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2aqs54tvokezkcahvgv.png)

- 3. Click on the Test connection button to test the notification in the slack channel. 
   Below is the sample notification 
![jenkins-with-slack-notification-example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anajmwuxhv98mubua5i4.png)



## 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.

    ![jenkins-ci-configuration-in-project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whbuidk9oqh99uwvoaix.png)

   - b. Then you’ll need to add Slack notifications to the Post-build actions for this project.

  ![jenkins-ci-configuration-in-project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9h0h8v3rvjsrvvqt4df.png)

  - c. In the Slack notifications section, choose the events you want to be notified about.    
    ![jenkins-ci-configuration-in-project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynk63fetbwvbogv0mss2.png)

### 2. Send notification through **Jenkinsfile** file
  If you are using **Jenkinsfile** then you can use this code to send the notification to slack channel
```javascript
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`
* `baseUrl` (optional)
    
    Allows overriding the Slack compatible app URL specified in the global configuration.
    
    * **Type:** `String`
* `blocks` (optional)
    * **Type:** `Object`
* `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`
* `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.  

    ```javascript
     slackSend channel: "#channel-name", message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"
    ```
    
    * **Type:** `String`
* `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.  
    ```javascript
    slackSend color: "#439FE0", message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"`
    ```
    
    * **Type:** `String`
* `failOnError` (optional)
    
    If set to true, then the step will abort the Workflow run if there is an error sending message.  
    ```javascript
    slackSend failOnError: true, message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"`
    ```
    
    * **Type:** `boolean`
* `iconEmoji` (optional)
    
    Choose a custom emoji to use as the bot's icon in Slack, requires using a bot user
    
    * **Type:** `String`
* `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:  
    ```javascript
    slackSend "started ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"`
    ```
    
    * **Type:** `String`
* `notifyCommitters` (optional)
    
    Notify committers via direct message in addition to specified channels, requires using a bot user
    
    * **Type:** `boolean`
* `replyBroadcast` (optional)
    * **Type:** `boolean`
* `sendAsText` (optional)
    * **Type:** `boolean`
* `teamDomain` (optional)
    
    Allows overriding the Slack Plugin Integration Team Domain specified in the global configuration.
    
    * **Type:** `String`
* `timestamp` (optional)
    
    Allows updating an existing message instead of posting a new one.
    
    * **Type:** `String`
* `token` (optional)
    
    Allows overriding the Slack Plugin Integration Token specified in the global configuration.
    
    * **Type:** `String`
* `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`
* `username` (optional)
    
    Choose a custom username to use as the bot's name, requires using a bot user
    
    * **Type:** `String`


## 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](https://twitter.com/eavnitech)