1. Overview

When our teams are responsible for DevOps practices, we often need to monitor builds and other automated jobs.

In this tutorial, we’ll see how to configure two popular platforms, Jenkins and Slack, to work together and tell us what’s happening while our CI/CD pipelines are running.

2. Setting up Slack

Let’s start by configuring Slack so Jenkins can send messages to it. To do this, we’ll create a custom Slack app, which requires an Administrator account.

In Slack, we’ll create an application and generate an OAuth token:

  • Visit https://api.slack.com
  • Login to the desired workspace
  • Click the Start Building button
  • Name the application Jenkins and click Create App
  • Click on OAuth & Permissions
  • In the Bot Token Scopes section, add the chat:write scope
  • Click the Install App to Workspace button
  • Click the Accept button

When this is done, we’ll see a summary screen:

jenkins slack token

Now, we need to take note of the OAuth token — we’ll need it later when we configure Jenkins. We should treat these as sensitive credentials and keep them safe..

To complete the Slack setup, we must invite the new Jenkins user into the channels we wish it to use. One easy way to do this is to mention the new user with the @ character inside each channel.

3. Setting up Jenkins

To set up Jenkins, we’ll need an administrator account.

First, let’s start by logging into Jenkins and navigating to Manage Jenkins > Plugin Manager.

Then, on the Available tab, we’ll search for Slack:

jenkins slack install plugin

Let’s select the checkbox for Slack Notification and click Install without restart.

Now, we need to configure new credentials. Let’s navigate to Jenkins > Credentials > System > Global Credentials and add a new Secret text credential:

jenkins slack credential

We’ll put the OAuth token from Slack into the Secret field. We should also give these credentials a meaningful ID and description to help us easily identify them later. The Jenkins credentials store is a safe place to keep this token.

Once we save the credentials, there is one more global configuration to set. Under Jenkins > Manage Jenkins > Configure System, we need to check the Custom slack app bot user checkbox under the Slack section:

jenkins slack settings custom bot checkbox

Now that we’ve completed the Jenkins setup, let’s look at how to configure Jenkins jobs and pipelines to send Slack messages.

4. Configuring a Traditional Jenkins Job

Traditional Jenkins jobs usually execute one or more actions to accomplish their goals. These are configured via the Jenkins user interface.

In order to integrate a traditional job with Slack, we’ll use a post-build action.

Let’s pick any job, or create a new one. When we drop down the Add post-build action menu, we’ll find Slack Notifications:

jenkins slack post build action dropdown

Once selected, there are lots of available inputs to the Slack Notification action. Generally, most of the default values are sufficient. However, there are a few required pieces of information:

  • Which build phases to send messages for (start, success, failure, etc)
  • The name of the credentials to use – the ones we added previously
  • The Slack channel name or member ID to send messages to

We can also specify additional fields if desired, such as commit information used for the Jenkins job, custom messages, custom bot icons, and more:

jenkins slack post build action

When setting things up via the UI, we can use the Test Connection button to ensure that Jenkins can reach Slack. If successful, we’ll see a test message in the Slack channel from the Jenkins user:

jenkins slack test message success

If the message doesn’t show up, the Jenkins log files are useful for troubleshooting. Generally, we need to double-check that the post-build action has all required fields, that the OAuth token was copied correctly, and that the token was granted the proper scopes when we configured Slack.

5. Configuring a Jenkins Pipeline

Jenkins Pipelines differ from traditional jobs. They use a single Groovy script, broken into stages, to define a build. They also don’t have post-build actions, so we use the pipeline script itself to send Slack messages.

The following snippet sends a message to Slack from a Jenkins pipeline:

slackSend botUser: true, 
  channel: 'builds', 
  color: '#00ff00', 
  message: 'Testing Jekins with Slack', 
  tokenCredentialId: 'slack-token'

Just like with the traditional Jenkins job setup, we must still specify a channel name and the name of the credential to use.

Via a Jenkins pipeline, we can also use a variety of additional Slack features, such as file upload, message threads, and more.

One downside to using Jenkins pipelines is that there’s no test button. To test the integration with Slack, we have to execute the whole pipeline.

When first setting things up, we can create a new pipeline that contains only the Slack command while we’re getting things working.

6. Additional Considerations

Now that we’ve got Jenkins and Slack connected, there are some additional considerations.

Firstly, a single Jenkins instance can communicate with multiple Slack workspaces. All we have to do is create a custom application and generate a new token for each workspace. As long as each token is stored as its own credential in Jenkins, different jobs can post to different workspaces.

Along those same lines, a different Jenkins job can post to different Slack channels. This is a per-job setting in the post-build actions we configure. For example, jobs related to software builds could post to a development-only channel. And jobs related to test or production could go to their own dedicated channels.

Finally, while we’ve looked at one of the more popular Slack plugins for Jenkins, which provides fine-grain control over what to send, there are a number of other plugins that serve different purposes. For example, if we want every Jenkins job to send the same notification, there is a Global Slack Notifier plugin that might be better suited for this.

7. Conclusion

In this article, we’ve seen how to integrate Jenkins and Slack to gain feedback on our CI/CD pipelines.

Using a Jenkins plugin, along with a custom Slack application, we were able to send messages from Jenkins to Slack. This allows teams to notice the status of Jenkins jobs and address issues more quickly.

Notify of
Inline Feedbacks
View all comments