Sending slack notification via pipeline job

1. Introduction to Jenkins and Slack Integration

In modern DevOps workflows, real-time communication is essential for faster feedback and collaboration. Slack, being a widely used communication tool, helps teams stay informed about their CI/CD pipeline status. By integrating Jenkins with Slack, developers can receive instant notifications on build successes or failures, enabling swift response times and collaboration.

  • Overview: Explain the benefits of integrating Slack with Jenkins, including real-time notifications of build statuses.
Why Integrate Slack with Jenkins?
  • Immediate Feedback: Get notified as soon as builds succeed or fail, allowing developers to react quickly.
  • Team Collaboration: Build notifications keep the entire team updated on the pipeline status without having to check Jenkins manually.
  • Improved Visibility: Helps keep track of the CI/CD pipeline progress, making it easier to monitor multiple pipelines

Prerequisites:

  • Jenkins: Installed and running.
  • Slack Account: Ensure you have a workspace and a dedicated channel for notifications.
  • Jenkins Slack Plugin: Installed and configured in Jenkins.
  • Webhook URL: From Slack to enable Jenkins to send messages.

2. Installing and Configuring the Slack Plugin in Jenkins

Step-by-Step Guide:

  1. Install Slack Plugin:
    • Log in to Jenkins.Go to Manage Jenkins > Manage Plugins.In the Available tab, search for “Slack Notification”.Select the plugin and click Install without restart.
    • After installation, make sure the plugin is enabled.

  1. Configure Slack Plugin in Jenkins
    • Go to Manage JenkinsConfigure System.
    • Scroll down to the “Slack” section and add your workspace.
    • Under Credential ID, use the credential ID that holds your Slack token (e.g., jen-slack-pwd).
    • In the Default Channel field, specify the channel where you want to post alerts (e.g., #build-notifications).
    • Test the connection by clicking Test Connection to ensure the setup is correct.Click Save to apply the changes.

3. Generate a Slack API Token
  1. Go to your Slack workspace and create a new Slack app via the Slack API.
  2. Once the app is created, go to OAuth & Permissions.
  3. Scroll down to OAuth Tokens for Your Workspace and click Install App to Workspace.
  4. After installation, you’ll get a Bot User OAuth Token. Copy this token.
  5. Go back to Jenkins, and in Manage Jenkins > Manage Credentials, create a new secret text credential with this token. Note the ID of this credential (e.g., jen-slack-pwd).

3. Setting Up Slack Notification in Pipeline Jobs

  • Creating a Basic Pipeline Job:
  • Use the slackSend function in your pipeline script to send notifications.
  • Pipeline script is as follows:
pipeline {
agent any
environment {
SLACK_CREDENTIALS = credentials('b3ee302b-e782-4d8e-ba83-7fa591d43205')
}
stages {
stage('Notify') {
steps {
script {
slackSend (
baseUrl: 'https://slack.com/api/',

channel: '#builds',
color: 'good',
message: 'Test notification from Jenkins',
tokenCredentialId: 'b3ee302b-e782-4d8e-ba83-7fa591d43205'
)
}
}
}
}
}
  • Explanation: Highlight how this script sends notifications to the specified Slack channel upon success or failure of the pipeline stages.
The complete tutorial to the Send slack notification using jenkins pipeline.
Mahesh Wabale

Leave a Comment