How to Post a Slack Notification from a Jenkins Pipeline
Jenkins posts to Slack with the Slack Notification plugin's slackSend step, usually from a post block.
Install and configure the Slack Notification plugin (workspace + token), then call slackSend in a post { failure {} } (or always) block so results reach a channel.
Notify Slack on failure
The post block posts a red message to Slack only when the build fails.
Jenkinsfile
pipeline {
agent any
stages {
stage('Test') {
steps { sh 'npm ci && npm test' }
}
}
post {
failure {
slackSend(
channel: '#builds',
color: 'danger',
message: "Build FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER} (${env.BUILD_URL})"
)
}
}
}Gotchas
slackSendneeds the Slack Notification plugin configured globally with a workspace and token credential.- Put notifications in a
postblock so they fire on the final result, not mid-stage. - Use
color: 'danger'/'good'/'warning'(or a hex) - arbitrary status strings are ignored.
Related guides
How to Post a Slack Notification from GitHub ActionsPost a Slack notification from GitHub Actions with the official slackapi action or an incoming webhook, inclu…
How to Require Manual Approval Before Deploy in a Jenkins PipelineRequire manual approval before a Jenkins deploy with the input step - pause for a confirm, restrict who can a…