How to Integrate Jenkins with Slack Using a Shared Library: A Step-by-Step Guide
In today’s world of continuous integration and delivery, keeping your team updated on build statuses is crucial. Integrating Jenkins with Slack allows you to send build notifications directly to your Slack channel, keeping everyone in the loop. In this guide, we’ll walk through how to integrate Jenkins with Slack using a shared library. It’s easy, flexible, and keeps your team informed without constantly checking Jenkins. Let’s get started!
Prerequisites
Before diving into the setup, make sure you have the following:
- Jenkins installed and running.
- A Slack workspace where you want to send notifications.
- Administrator access to Jenkins to configure plugins and settings.
- Slack Notification Plugin installed in Jenkins (don’t worry, we’ll cover this below).
- A basic understanding of pipelines in Jenkins and Git.
Why Use a Shared Library?
Using a shared library in Jenkins allows you to write once and reuse your code across multiple pipelines. It’s a cleaner way to manage your Slack notifications without duplicating the same script in every pipeline. Plus, it’s easier to maintain!
Step 1: Setting Up Slack for Jenkins
First, you’ll need to set up a Slack app and give it permissions to send messages to your workspace.
1.1 Create a Slack App
- Go to the Slack API and click Create New App.
- Choose From scratch, then name your app (e.g., "Jenkins Notifier") and pick your workspace.
- Click Create App to get started.
1.2 Configure Slack App Permissions
- In your Slack app dashboard, go to OAuth & Permissions.
- Under Bot Token Scopes, add
chat:write
so your app can send messages to channels. - Once done, click Install App to Workspace. Slack will prompt you to authorize the app for your workspace.
- Copy the OAuth Access Token – you’ll need this for Jenkins.
1.3 Find Your Slack Channel ID
- Open Slack and navigate to the channel where you want Jenkins to send notifications.
- Click the channel name and find the Channel ID in the URL (it looks like
C12345678
). You’ll need this later!
Step 2: Setting Up Jenkins to Talk to Slack
Now that your Slack app is ready, let’s configure Jenkins to send messages to Slack.
2.1 Install the Slack Notification Plugin
- In Jenkins, go to Manage Jenkins > Manage Plugins.
- Under the Available tab, search for Slack Notification Plugin.
- Install the plugin and restart Jenkins to activate it.
2.2 Configure Slack in Jenkins
- Go to Manage Jenkins > Configure System.
- Scroll down to the Slack section and fill in the details:
- Workspace: Your Slack workspace URL (e.g., teamdevsecopsgroupe ).
- Credential: Add a new secret and paste must be generated on slack before (token slack generated ).
- Default channel: Enter the default Slack channel name where notifications should go.
Click Test Connection to make sure everything works, then hit Save.
Step 3: Creating the Jenkins Shared Library for Slack Notifications
To avoid repetitive code, you’ll create a shared library that handles sending Slack notifications from Jenkins pipelines.
3.1 Setting Up the Shared Library
Create a new Git repository for your shared library. The directory structure should look like this:
3.2 Writing the sendNotification.groovy
Script
Here’s a simple Groovy script to send Slack notifications from Jenkins:
def call(String buildStatus = 'STARTED') {
// Build status of null means success.
buildStatus = buildStatus ?: 'SUCCESS'
def colorCode = '#FF0000'
if (buildStatus == 'SUCCESS') {
colorCode = '#36A64F'
} else if (buildStatus == 'UNSTABLE') {
colorCode = '#FFFF00'
}
// Slack message
def message = "*${buildStatus}:* Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})"
slackSend(color: colorCode, message: message, channel: "#${env.SLACK_CHANNEL}")
}
3.3 Adding the Shared Library to Jenkins
- In Jenkins, go to Manage Jenkins > Configure System.
- Scroll down to Global Pipeline Libraries and add a new library:
- Name:
slack
- Default Version: Specify a branch (e.g.,
main
). - Source Code Management: Select Git and enter the URL of the repository containing your shared library.
- Click Save.
Step 4: Using the Shared Library in Your Jenkinsfile
Now that the shared library is set up, you can use it in your pipelines.
Here’s an example Jenkinsfile
:
@Library('slack') _
pipeline {
agent any
environment {
SLACK_CHANNEL = 'teamDevsecops' // Slack channel to send notifications
}
stages {
stage('Build') {
steps {
script {
sendNotification('STARTED')
}
// Your build steps here
}
}
stage('Test') {
steps {
// Your test steps here
}
}
stage('Deploy') {
steps {
// Your deployment steps here
}
}
}
post {
success {
script {
sendNotification('SUCCESS')
}
}
failure {
script {
sendNotification('FAILURE')
}
}
unstable {
script {
sendNotification('UNSTABLE')
}
}
}
}
4.1 Explanation:
@Library('slack')
: This imports your shared library namedslack
.sendNotification('STATUS')
: This is the shared library function that sends the notification to Slack.
- Post-build actions: Automatically send notifications after each build based on success, failure, or instability.
Conclusion
And there you have it! You’ve successfully integrated Jenkins with Slack using a shared library. This setup is both reusable and flexible, allowing you to send notifications to different channels based on your pipeline’s status.
Using a shared library in Jenkins not only keeps your code DRY (Don’t Repeat Yourself) but also makes it easier to manage and maintain across different projects.
Feel free to experiment further and customize the Slack messages as per your needs. Happy building and notifying!