CI/CD pipelines are an essential part of modern software development. They accelerate code delivery and maintain quality by passing all changes through a series of automated stages. Integrating your CI/CD system with tools like Slack gives you immediate notifications when build errors, test failures, and successful releases occur. This enhances your view of CI/CD activity.
In this article, you’ll learn how to use Slack with the popular Jenkins CI server. You’ll use Jenkins’ Slack Notification plugin to send messages and upload files to Slack in five simple steps, plus explore some advanced use cases. We’ll assume you’ve already got access to a Jenkins installation and Slack workspace.
1. Create a Slack App
Begin by configuring Slack. Custom apps are the modern way to extend Slack with your own functionality. Creating your own app lets you set up a bot user that Jenkins can identify as.
Log in to your Slack instance and click the Apps link in the sidebar. If you don’t see the link, try clicking More and then selecting Apps from the dropdown menu. From the Apps screen that appears, click the App Directory link in the top-right:
Click the Build button in the top right of the Slack App Directory, next to your username:
Press the Create an app button on the following screen:
Choose From scratch at the prompt to start a new empty app:
The next step asks you to name your app. Choose any name you like that makes sense for your integration, then select your Slack workspace from the dropdown menu; make sure to check you’ve picked the correct one if you’re logged into several, as your app will only work with the workspace chosen here. Press the green Create App button when you’re done:
2. Configuring Your Slack App
You’ll be taken to your app’s details after the creation process completes. Once you’re there, click the OAuth & Permissions link in the left sidebar to define the capabilities your app can use:
Scroll down the page to the Scopes heading. Click the Add an OAuth Scope button under the Bot Token Scopes heading:
Search for the chat:write scope in the menu. Adding this scope allows Jenkins to send new chat messages to the workspace that the app’s installed in:
To complete the process, scroll back up to the top of the OAuth & Permissions page, and select the Install to Workspace button under the OAuth Tokens for Your Workspace heading:
Press the Allow button at the permissions confirmation prompt:
This will take you back to the OAuth & Permissions page where your app’s Bot User OAuth Token will be displayed. Have this ready to supply to Jenkins in the next section (but make sure to keep this token secure as anyone having this token can send messages to your slack channel):
Finally, head to your Slack workspace and create a channel that Jenkins will send messages to. Once you’ve created a channel, or selected an existing one, post a message that at-mentions your app. Slackbot will prompt you to invite the app into the channel, which will permit the app to send new messages:
3. Configuring Jenkins
Next, head to your Jenkins server. Select the Manage Jenkins link in the left sidebar, then Manage Plugins to reach the plugin administration screen:
Switch to the Available tab at the top of the screen. Search for the Slack Notification plugin, click the checkbox next to its name, and press the Install without restart button at the bottom:
After the plugin’s installed, return to the Manage Jenkins screen and click the Manage Credentials link under the Security heading:
Click the link to the System credential store, then navigate into the Global credentials (unrestricted) domain on the next page. From this screen, press the blue Add Credentials button in the top right to access the credential creation screen:
This is where you configure the Slack Notification plugin with the OAuth token you generated in Slack earlier on. Change the Kind dropdown to Secret text. Paste your OAuth token into the Secret field on the form, then change the ID to slack-token or another label of your choice. Press the Create button at the bottom of the screen to save your secret:
There’s one more step left. Head back to the Manage Jenkins screen again, and click the Configure System link at the top. Scroll down the page until you find the Slack heading. This groups the settings provided by the Slack Notification plugin:
There are three changes to make:
- Select the credential you created above from the Credential dropdown menu.
- Enter the name of the channel you configured earlier into the Default channel / member id field, including a leading # character.
- Check the Custom slack app bot user checkbox to indicate that your token belongs to a custom Slack app that can be used as a bot.
Press the Test Connection button below the form to check everything’s working correctly. Finally, click the dark blue Save button at the bottom to apply your changes. You’re ready to send messages to Slack!
4. Sending Slack Messages as a Post-Build Action
Navigate to your Jenkins homepage, and create a new test job. You can use a freestyle job that performs a simple task like echoing some text in a shell command.
Within your job’s configuration, use the Add post-build action menu to add the Slack Notification plugin to the job:
Next, select one or more checkboxes to set the events that will send a message to your Slack channel:
You can override the plugin’s default configuration by clicking the Advanced button. This lets you change the target Slack channel, post as a different username, or select a different set of credentials to the defaults you’ve configured.
Press the Save button at the bottom of the screen when you’ve finished configuring your job. You’ll then be taken to the job’s details. Click the Build Now link in the left sidebar to run your job:
Now, you should receive a message containing the build’s status in your Slack channel:
5. Sending Slack Messages from a Jenkins Pipeline
The previous example used the plugin to automatically send messages for specific build events. You can also send your own arbitrary messages by including the slackSend snippet in a Jenkins pipeline config file.
Create a new Jenkins pipeline by hovering over the Dashboard link in the top left and choosing New Item from the menu that appears. Select the Pipeline item type, then press OK:
The pipeline’s configuration page will appear. Leave the General tab at its defaults and switch to the Pipeline screen from the menu on the left.
Paste in this simple Groovy script to define your pipeline:
pipeline {
agent any
stages {
stage('Send message') {
steps {
slackSend botUser: true,
message: 'This is a test message sent by Jenkins',
channel: '#jenkins-alerts',
color: 'good'
}
}
}
}
The script defines a pipeline with a single stage that posts a message to a specific channel. If you omit the channel field, then the default channel configured in your plugin’s settings will be used instead.
Running the pipeline sends the message to Slack:
Advanced Example: Using Multiple Slack Instances
The Slack Notification plugin can be used with multiple Slack instances simultaneously. The plugin’s global settings set in Step 3 can be overridden on a per-job basis. Press the Advanced button when you’re configuring a post-build action in the Jenkins job UI, or set extra parameters when you’re posting messages with a slackSend snippet:
slackSend botUser: true,
message: 'This is a test message sent by Jenkins',
color: 'good',
channel: '#demo-channel',
tokenCredentialId: slack-token-2
The example above posts to the demo-channel channel using the OAuth token in the slack-token-2 Jenkins secret. Repeat the procedure described in Steps 1-3 above to create your second Slack app and save its token to the slack-token-2 secret.
Advanced Example: Uploading File Attachments to Slack
The plugin can also upload files to Slack! This can be useful to present important information such as test reports and code coverage artifacts right inside Slack.
Use the slackUploadFile snippet to include this functionality in your pipelines. You must specify the local path to the file and an optional initial comment to display in Slack:
pipeline {
agent any
stages {
stage('Upload file') {
steps {
echo "Demo file" > demo.txt
slackUploadFile filePath: "demo.txt", initialComment: "Demo file uploaded"
}
}
}
}
Add the files:write scope to your Slack app’s bot token before you use this feature. Follow the guidance in Step 2 to add the scope and reinstall the app in your workplace. You can then run the pipeline above to upload a sample file to Slack.
Summary
In this guide, you’ve seen how to connect Jenkins to Slack and send messages for different events in your CI/CD pipelines. This allows your teams to get instant feedback alongside their Slack chats, cutting down time spent switching between tools.
The Slack Notification plugin offers customizable functionality for posting messages, uploading attachments, and amending existing content. Try reading its documentation to learn more about all its capabilities. For a deeper jenkins-slack integration that allows you to also trigger any Jenkins job from Slack with granular permission control and other advanced DevOps integrations, check out Kubiya’s AI-driven virtual assistant for Slack.