Jenkins Integration with Slack & Email Notifications for Build Status
Objective
Automate notifications in Jenkins for build status updates (Success, Failure, Unstable) by integrating it with Slack and Email.
Project Overview
This project configures Jenkins to send Slack messages and email alerts when a build completes.
It ensures developers are immediately notified about build failures or successes, enhancing CI/CD efficiency.
Technologies & Tools Used
- Jenkins (CI/CD automation tool)
- Slack (for team collaboration)
- Email Server (SMTP) (for email notifications)
- Jenkins Plugins:
- Slack Notification Plugin
- Email Extension Plugin
- AWS SES (Optional) – If using AWS for SMTP emails.
- Groovy (for advanced notification scripts)
Implementation Steps
Step 1: Install Required Plugins
- Open Jenkins Dashboard → Navigate to Manage Jenkins → Manage Plugins.
- Under the Available tab, search and install:
- Slack Notification Plugin
- Email Extension Plugin
- Restart Jenkins after installation.
Step 2: Configure Slack Integration
- Create a Slack App:
- Go to Slack API
- Click on "Create an App" → From Scratch.
- Select your workspace and provide a name.
- Navigate to OAuth & Permissions → Add the required scopes:
- chat:write
- chat:write.customize
- chat:write.public
- Generate and Copy Bot Token.
- Set Up a Slack Channel:
- Create a new Slack channel (e.g.,
#jenkins-notifications).
- Invite the bot to this channel.
- Configure Slack in Jenkins:
- Open Jenkins Dashboard → Manage Jenkins → Configure System.
- Scroll to Slack section.
- Enter:
- Workspace name.
- Bot User OAuth Token.
- Channel Name (e.g., #jenkins-notifications).
- Test the connection and save.
Step 3: Configure Email Notifications
- Setup SMTP Server:
- Open Jenkins Dashboard → Manage Jenkins → Configure System.
- Scroll to E-mail Notification section.
- Enter SMTP settings:
- SMTP Server: e.g., smtp.gmail.com (for Gmail)
- Username: Your email address.
- Password: App password (not regular email password).
- SMTP Port: 465 (for SSL) or 587 (for TLS).
- Use SSL/TLS: Enable.
- Test the Email Configuration.
- Add Test email recipients and send a test mail.
Step 4: Add Notification to a Jenkins Job
- Open an existing Jenkins Pipeline job or create a new one.
- Edit the Jenkinsfile to include Slack and Email notifications.
Step 5: Configure Jenkinsfile for Notifications
Modify the Jenkinsfile to include Slack and Email notifications:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "Building application..."
sh 'exit 0' // Simulating a successful build
}
}
}
post {
success {
slackSend channel: '#jenkins-notifications', color: 'good', message: "✅ SUCCESS: Job ${env.JOB_NAME} #${env.BUILD_NUMBER} completed successfully."
emailext subject: "Jenkins Build SUCCESS: ${env.JOB_NAME}",
body: "SUCCESS: Build #${env.BUILD_NUMBER} completed successfully.",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
}
failure {
slackSend channel: '#jenkins-notifications', color: 'danger', message: "❌ FAILURE: Job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."
emailext subject: "Jenkins Build FAILURE: ${env.JOB_NAME}",
body: "FAILURE: Build #${env.BUILD_NUMBER} failed. Check logs.",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
}
}
}
Testing the Integration
- Run the pipeline job in Jenkins.
- Check Slack for a message in
#jenkins-notifications.
- Verify email notifications in the recipient’s inbox.
- Induce a failure (e.g.,
sh 'exit 1') and test failure notifications.
Conclusion
This project successfully integrates Slack and Email notifications into Jenkins for build status updates.
It ensures teams receive immediate feedback on builds, improving DevOps collaboration and efficiency.