Jenkins & Slack Integration on AWS Platform
DevOps CICD pipeline Centralized notification
Introduction:
When our teams are responsible for DevOps practices, we often need to monitor builds and other automated jobs.
Automation build environment to run build every single push happens on the source repository we need to broadcast the status of the build to the team members will ensure code base sanity
In this tutorial, we’ll see how to configure two popular platforms, Jenkins and Slack, to work together and tell us what’s happening while our CI/CD pipelines are running.
Pre-Requisite:
- Jenkins must be installed in your VM.
2. Slack must be installed in your VM.
Note: This link will create and install Jenkins via terraform.
https://github.com/manikcloud/simplilearn-projects/blob/main/README.md
Install Instructions for Slack compatible application
- Log into the Slack-compatible application.
- Create a Webhook (it may need to be enabled in the system console) by visiting Integrations.
- You should now have a URL with a token. Something like
https://mydomain.com/hooks/xxxx
wherexxxx
is the integration token andhttps://mydomain.com/hooks/
is theSlack compatible app URL
. - Install this plugin on your Jenkins server.
- Follow the pipeline instructions for the slack installation instruction
Step 1: Download the slack plugin
- Get a Slack account: https://slack.com/
- Configure the Jenkins integration: https://my.slack.com/services/new/jenkins-ci
- Install this plugin on your Jenkins server:
- From the Jenkins, homepage navigate to
Manage Jenkins
- Navigate to
Manage Plugins
,
- Change the tab to
Available
, - Search for
slack
, - Check the box next to install.
Step 3: Creating your app
Note: These docs may become outdated as Slack changes its website, if they do become outdated please send a PR here to update the docs.
- Go to https://api.slack.com/apps and click “Create New App”.
- Pick an app name, i.e. “Jenkins” and a workspace that you’ll be installing it to.
- Click “Create App”. This will leave you on the “Basic Information” screen for your new app.
Step 4:
- Scroll down to “Display Information” and fill it out. You can get the Jenkins logo from: https://jenkins.io/artwork/.
- Scroll back up to “Add features and functionality”.
- Click “Permissions” to navigate to the “OAuth & Permissions” page.
- Scroll down to “Scopes”. Under “Bot Token Scopes”
- Add
chat:write
Scope. - (optional) Add
files:write
Scope if you will be uploading files. - (optional) Add
chat:write.customize
Scope if you will be sending messages with a custom username and/or avatar. - (optional) Add
reactions:write
Scope if you will be adding reactions. - (optional) Add
users:read
andusers:read.email
Scope if you will be looking users up by email.
Step 5:
- (optional) Click “App Home” in the sidebar
- (optional) Edit the slack display name for the bot.
- Return to the “OAuth & Permissions” page.
- At the top of the page, click “Install App to Workspace”. This will generate a “Bot User OAuth Access Token”.
Step 6:
- Copy the “Bot User OAuth Access Token”.
- On Jenkins: Find the Slack configuration in “Manage Jenkins → Configure System”.
- On Jenkins: Click “Add” to create a new “Secret text” Credential with that token.
Step 7:
- On Jenkins: Select the new “Secret text” in the dropdown.
- On Jenkins: Make note of the “Default channel / member id”.
- On Jenkins: Tick the “Custom slack app bot user” option.
- Invite the Jenkins bot user into the Slack channel(s) you wish to be notified in.
- On Jenkins: Click test connection. A message will be sent to the default channel / default member.
Step 8: Pipeline Code
pipeline {
agent anystages {
stage('notification') {
steps {
slackSend message: 'Notification Test message'
}
}
}
}
Step 9: Complete pipeline code
pipeline {
agent any
tools {
maven 'my_mvn'
}
stages {
stage("Checkout") {
steps {
git branch: 'session5.2_pipeline', url: 'https://github.com/manikcloud/Jenkins-cicd.git'
}
}
stage('clean') {
steps {
sh "mvn clean"
}
}
stage('Build') {
steps {
sh "mvn compile"
}
}
stage("Unit test") {
steps {
sh "mvn test"
}
}
stage('notification') {
steps {
slackSend message: 'Notification Test message'
}
}}
}
Step 10: Run Jenkins Job.
Conclusion:
Finally, while we’ve looked at one of the more popular Slack plugins for Jenkins, which provides fine-grain control over what to send, there are a number of other plugins that serve different purposes.
For example, if we want every Jenkins job to send the same notification, there is a Global Slack Notifier plugin that might be better suited for this.
In this article, we’ve seen how to integrate Jenkins and Slack to gain feedback on our CI/CD pipelines.
Using a Jenkins plugin, along with a custom Slack application, we were able to send messages from Jenkins to Slack. This allows teams to notice the status of Jenkins jobs and address issues more quickly.
References: