Certified Jenkins Engineer
Jenkins Administration and Monitoring Part 1
Demo Send Slack Notification
In this guide, you’ll enhance the Jenkinsfile
in the solar-system repository to send Slack notifications based on build outcomes. By the end, you’ll have a reusable Groovy helper and a DRY pipeline that alerts your team of successes, instabilities, and failures.
1. Create a Feature Branch
Start by branching off from main
:
git checkout -b feature/enabling-slack
Switched to a new branch 'feature/enabling-slack'
2. Generate the Slack Step
Open Pipeline Syntax in Jenkins:
- Select Slack Send.
- Configure your channel (e.g.,
#dasher-notifications
). - Set a default message and a hex color for “Build Started.”
You can pick a hex code to reflect status:
Pipeline syntax will produce:
slackSend botUser: true,
channel: '#dasher-notifications',
color: '#439FE0',
message: "Build Started: ${env.JOB_NAME} ${env.BUILD_NUMBER}"
Note
Ensure you have configured your Slack App and credentials in Jenkins Manage Credentials before using slackSend
.
3. Why Reuse Is Better
Adding identical slackSend
calls under every stage’s post
block quickly becomes unwieldy:
pipeline {
/* ... */
stages {
stage('Upload - AWS S3') {
steps { /* ... */ }
post {
success {
slackSend botUser: true,
channel: '#dasher-notifications',
color: '#47ec05',
message: "Upload S3 succeeded: ${env.BUILD_NUMBER}"
}
failure {
slackSend botUser: true,
channel: '#dasher-notifications',
color: '#ec2805',
message: "Upload S3 failed: ${env.BUILD_NUMBER}"
}
}
}
// more stages...
}
}
4. Define a Reusable Slack Method
At the top of your Jenkinsfile
, add:
def slackNotification(String buildStatus = 'STARTED') {
buildStatus = buildStatus ?: 'SUCCESS'
// Map status to color
def color = buildStatus == 'SUCCESS' ? '#47ec05' :
buildStatus == 'UNSTABLE' ? '#d5ee0d' :
'#ec2805'
// Construct message
def msg = "${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}\n${env.BUILD_URL}"
slackSend color: color, message: msg
}
Build Status Color Mapping
Build Status | Hex Color |
---|---|
SUCCESS | #47ec05 |
UNSTABLE | #d5ee0d |
FAILURE | #ec2805 |
5. Update the Pipeline
Use a single post { always { ... } }
block:
pipeline {
agent any
tools {
// e.g., nodejs '12.x'
}
stages {
stage('Installing Dependencies') {
steps {
sh 'npm install --no-audit'
}
}
stage('Code Coverage') {
steps {
catchError(buildResult: 'SUCCESS', message: 'Coverage step failed but allowed') {
sh 'npm run coverage'
}
}
}
// additional stages...
}
post {
always {
slackNotification(currentBuild.result)
}
cleanup {
sh 'rm -rf solar-system-gitops-argocd'
}
junit allowEmptyResults: true, testResults: 'test-results.xml'
}
}
Commit and push:
git add Jenkinsfile
git commit -m "feat: enable Slack notifications"
git push -u origin feature/enabling-slack
Trigger a build to see the consolidated Slack notification:
6. Demonstrate Failure
Add a failing step to confirm a red alert:
pipeline {
agent any
stages {
stage('Fail Fast') {
steps {
sh 'echo "Simulating failure"; exit 1'
}
}
}
post {
always {
slackNotification(currentBuild.result)
}
}
}
Push and rebuild. The Slack notification color should switch to red on failure.
Warning
Never hardcode sensitive tokens in your Jenkinsfile
. Use Jenkins Credentials Binding for secure handling.
7. Next Steps
To reuse slackNotification
across multiple repositories, extract it into a Shared Library. This centralizes common pipeline logic and keeps your Jenkinsfile
lean.
Links and References
Watch Video
Watch video content
Practice Lab
Practice lab