DEV Community

Cover image for Send notification to slack from the Jenkins CI Job and Jenkinsfile | e Avni Tech
eavnitech
eavnitech

Posted on • Originally published at eavnitech.com

Send notification to slack from the Jenkins CI Job and Jenkinsfile | e Avni Tech

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. manage-jenkins
  • 2. Navigate to Manage Plugins
  • 3. Click on the tab name as Available
  • 4. Search for Slack Notification slack-notification-plugin-manager-search
  • 5. Check the box next to install

Jenkins CI App installation in Slack

slack-notification-plugin-manager-search

  • 3. Now select the channel and click on the Add Jenkins CI Integration slack-notification-plugin-manager-search
  • 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.
    1. 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. jenkins-ci-configuration
    1. Click on the Test connection button to test the notification in the slack channel. Below is the sample notification jenkins-with-slack-notification-example

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

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

jenkins-ci-configuration-in-project

  • c. In the Slack notifications section, choose the events you want to be notified about. jenkins-ci-configuration-in-project

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>)"
Enter fullscreen mode Exit fullscreen mode

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.

     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.

    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.

    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:

    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

Top comments (0)