0
0
Jenkinsdevops~5 mins

Slack notifications in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sending Slack notifications from Jenkins helps teams stay updated on build and deployment status without checking Jenkins manually. It solves the problem of missing important alerts by delivering messages directly to Slack channels.
When you want to notify your team immediately after a build succeeds or fails
When you want to send deployment status updates to a Slack channel
When you want to alert developers about test failures during continuous integration
When you want to keep stakeholders informed about pipeline progress
When you want to automate reminders or warnings related to Jenkins jobs
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
      }
    }
  }
  post {
    success {
      slackSend(channel: '#jenkins-notifications', color: 'good', message: "Build Successful: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (<${env.BUILD_URL}|Open>)")
    }
    failure {
      slackSend(channel: '#jenkins-notifications', color: 'danger', message: "Build Failed: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (<${env.BUILD_URL}|Open>)")
    }
  }
}

This Jenkinsfile defines a simple pipeline with build and test stages.

The post section sends Slack notifications after the job finishes.

slackSend sends messages to the #jenkins-notifications Slack channel.

The color parameter sets the message color: green for success, red for failure.

The message includes the job name, build number, and a clickable link to the build.

Commands
Installs the Slack plugin in Jenkins to enable Slack notifications.
Terminal
jenkins-plugin-cli --plugins slack
Expected OutputExpected
Installing plugin: slack Downloading plugin: slack Installing plugin slack Plugin slack installed successfully
Triggers the Jenkins pipeline named 'example-pipeline' to run the build and test stages.
Terminal
jenkins-cli build example-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building... [Pipeline] echo Testing... [Pipeline] End of Pipeline Finished: SUCCESS
Sends a test message to Slack using an Incoming Webhook URL to verify Slack integration.
Terminal
curl -X POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX -d '{"text":"Test Slack notification from Jenkins"}' -H 'Content-type: application/json'
Expected OutputExpected
ok
Key Concept

If you remember nothing else from this pattern, remember: configure Jenkins to send Slack messages automatically after builds to keep your team informed in real time.

Common Mistakes
Not installing the Slack plugin before using slackSend in Jenkinsfile
The slackSend step will fail because Jenkins does not recognize the command without the plugin.
Install the Slack plugin using Jenkins plugin manager or CLI before using slackSend.
Using incorrect Slack channel name or missing permissions
Slack messages will not be delivered if the channel does not exist or Jenkins bot lacks permission.
Verify the Slack channel name and ensure Jenkins has permission to post messages there.
Hardcoding sensitive Slack webhook URLs directly in Jenkinsfile
Exposes secrets in source control and risks security breaches.
Use Jenkins credentials store to securely manage Slack tokens or webhook URLs.
Summary
Install the Slack plugin in Jenkins to enable Slack notifications.
Add slackSend steps in the Jenkinsfile post section to send messages on build success or failure.
Test Slack integration by sending a message using a webhook or triggering a Jenkins build.