Jenkins & Slack Integration on AWS Platform

Varun Kumar Manik
5 min readAug 27, 2022

--

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:

  1. 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

  1. Log into the Slack-compatible application.
  2. Create a Webhook (it may need to be enabled in the system console) by visiting Integrations.
  3. You should now have a URL with a token. Something like https://mydomain.com/hooks/xxxx where xxxx is the integration token and https://mydomain.com/hooks/ is the Slack compatible app URL.
  4. Install this plugin on your Jenkins server.
  5. Follow the pipeline instructions for the slack installation instruction

Step 1: Download the slack plugin

  1. Get a Slack account: https://slack.com/
  2. Configure the Jenkins integration: https://my.slack.com/services/new/jenkins-ci
  3. Install this plugin on your Jenkins server:
  4. From the Jenkins, homepage navigate to Manage Jenkins
  5. Navigate to Manage Plugins,
  1. Change the tab to Available,
  2. Search for slack,
  3. 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.

  1. Go to https://api.slack.com/apps and click “Create New App”.
  1. Pick an app name, i.e. “Jenkins” and a workspace that you’ll be installing it to.
  2. Click “Create App”. This will leave you on the “Basic Information” screen for your new app.

Step 4:

  1. Scroll down to “Display Information” and fill it out. You can get the Jenkins logo from: https://jenkins.io/artwork/.
  2. Scroll back up to “Add features and functionality”.
  3. Click “Permissions” to navigate to the “OAuth & Permissions” page.
  4. Scroll down to “Scopes”. Under “Bot Token Scopes”
  5. Add chat:write Scope.
  6. (optional) Add files:write Scope if you will be uploading files.
  7. (optional) Add chat:write.customize Scope if you will be sending messages with a custom username and/or avatar.
  8. (optional) Add reactions:write Scope if you will be adding reactions.
  9. (optional) Add users:read and users:read.email Scope if you will be looking users up by email.

Step 5:

  1. (optional) Click “App Home” in the sidebar
  2. (optional) Edit the slack display name for the bot.
  3. Return to the “OAuth & Permissions” page.
  4. At the top of the page, click “Install App to Workspace”. This will generate a “Bot User OAuth Access Token”.

Step 6:

  1. Copy the “Bot User OAuth Access Token”.
  2. On Jenkins: Find the Slack configuration in “Manage Jenkins → Configure System”.
  3. On Jenkins: Click “Add” to create a new “Secret text” Credential with that token.

Step 7:

  1. On Jenkins: Select the new “Secret text” in the dropdown.
  2. On Jenkins: Make note of the “Default channel / member id”.
  3. On Jenkins: Tick the “Custom slack app bot user” option.
  4. Invite the Jenkins bot user into the Slack channel(s) you wish to be notified in.
  5. On Jenkins: Click test connection. A message will be sent to the default channel / default member.

Step 8: Pipeline Code

pipeline {
agent any
stages {
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:

https://plugins.jenkins.io/slack/

--

--

Varun Kumar Manik
Varun Kumar Manik

Written by Varun Kumar Manik

AWS APN Ambassador | SME of DevOps DevSecOps | Cloud Architect & Trainer | Blogger | Youtuber |Chef

No responses yet