Jenkins jobs are spitting out build results to Slack channels.
// Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
// Simulate a failed test
script {
error("Test failed!")
}
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
post {
success {
slackSend channel: '#build-success',
color: 'good',
message: "SUCCESS: *${env.JOB_NAME}* build `${env.BUILD_NUMBER}` completed successfully. <${env.BUILD_URL}|Open>"
}
failure {
slackSend channel: '#build-failures',
color: 'danger',
message: "FAILURE: *${env.JOB_NAME}* build `${env.BUILD_NUMBER}` failed. <${env.BUILD_URL}|Open>"
}
unstable {
slackSend channel: '#build-unstable',
color: 'warning',
message: "UNSTABLE: *${env.JOB_NAME}* build `${env.BUILD_NUMBER}` is unstable. <${env.BUILD_URL}|Open>"
}
}
}
This pipeline defines stages for building, testing, and deploying. The post section is where the magic happens for Slack notifications. It triggers based on the build’s final status: success, failure, or unstable.
Inside slackSend, you specify:
channel: The Slack channel name (e.g.,#build-success).color: A hex code or common color name (good,warning,danger) that colors the Slack message attachment.message: The actual content of the message. You can use Groovy string interpolation to include Jenkins environment variables like${env.JOB_NAME},${env.BUILD_NUMBER}, and${env.BUILD_URL}.
To make this work, you need the Slack Notification plugin installed in Jenkins. Go to Manage Jenkins -> Manage Plugins -> Available and search for "Slack Notification". Install it, and restart Jenkins if prompted.
Once the plugin is installed, you need to configure the Slack integration globally. Go to Manage Jenkins -> Configure System. Scroll down to the "Slack" section. Here, you’ll enter your Slack integration details. The most common method is using an "Incoming Webhook URL".
To get this URL:
- In Slack, go to
Administration->Manage apps. - Search for "Incoming WebHooks" and add it to your workspace.
- Choose the default channel where you want messages to post (this can be overridden in the pipeline script).
- Click "Add Incoming WebHooks integration".
- Copy the "Webhook URL". It will look something like
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX. - Paste this URL into the "Credential" field in Jenkins’ Slack configuration. You’ll likely need to create a "Secret text" credential in Jenkins first (
Manage Jenkins->Manage Credentials->System->Global credentials->Add Credentials) and then select that credential here.
The color parameter in slackSend maps to the color field in Slack’s attachment API. good typically becomes green, warning becomes yellow, and danger becomes red.
You can also send messages to specific users or groups by mentioning them in the message text, like @here or @username.
The real power comes from the post section. You can define actions for always, changed, fixed, regression, broken, repaired, and deleted in addition to success, failure, and unstable. For example, to notify a specific team only when a build breaks:
pipeline {
agent any
stages {
// ... stages here ...
}
post {
// ... other post conditions ...
broken {
slackSend channel: '#dev-alerts',
message: "BUILD BROKEN: *${env.JOB_NAME}* build `${env.BUILD_NUMBER}` failed after a previous success. <${env.BUILD_URL}|Open>"
}
}
}
The Jenkinsfile itself is the primary mechanism for controlling what gets sent and when. The global Slack configuration in Jenkins handles the how – connecting Jenkins to your Slack workspace.
The message field supports Slack’s message formatting. You can use *bold*, _italic_, ~strike~, and `code`. Links are created using <URL|Text>.
The most surprising thing about using Slack notifications is how easily they can become noise if not carefully managed. The default behavior might be to send a message for every single build, but the real value is in conditional notifications – only alerting when something changes or requires attention. This means leveraging the post conditions like changed, fixed, broken, and regression is far more effective than just success and failure for every run.
The next logical step is to explore more advanced Slack message formatting, including buttons and interactive elements, using the Slack API directly or through other Jenkins plugins.