Thursday, August 26, 2021

How to send approval notification from Jenkins Pipeline to Slack | Trigger Jenkins Job using Webhooks from Slack

How to integrate Slack with Jenkins using webhooks? How to send approval notification from Jenkins to Slack and approve and trigger job from Slack to Jenkins. We need to create webhooks in Slack to send messages to Slack channel.



Pre-requistes:
1. Jenkins is up and running
2. Slack account is is created and channel is created.

Steps to integrate Slack with Jenkins using Webhooks

Create Webhooks in Slack
2. Click on Create an app

3. Select from scratch


4. Name the app and choose the workspace

Now click on incoming webhooks

Enable it by clicking on


Now select the channel where you like to post messages

Now copy the Webhook url which we will be using in Jenkins pipeline:



Now go to Jenkins and create this pipeline:

import groovy.json.JsonOutput

pipeline {
    agent any
    tools {
        maven 'Maven3'
    }
    environment {
        jenkins_server_url = "http://ec2-3-129-59-179.us-east-2.compute.amazonaws.com:8090"
        notification_channel = 'aug-2021-weekday-batch'
        slack_url = 'https://hooks.slack.com/services/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'
        
    }

   stages {
       
    stage('Cloning Git') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/main']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '8b429648-98dd-446a-80c6-120f9eab44fe', url: 'https://github.com/akannan1087/myAug21WeekendRepo']]])     
            }
        }
    stage ('Build') {
      steps {
      sh 'mvn clean install -f MyWebApp/pom.xml'
      }
    }

    stage ('DEV Deploy') {
      steps {
      echo "deploying to DEV Env "
      deploy adapters: [tomcat9(credentialsId: '341bc648-7df9-4b41-b520-9872cd2d8097', path: '', url: 'http://ec2-18-222-124-177.us-east-2.compute.amazonaws.com:8080/')], contextPath: null, war: '**/*.war'
      }
    }
    stage('QA approve') {
        steps {
          notifySlack("Do you approve QA deployment? $jenkins_server_url/job/$JOB_NAME", notification_channel, [])
            input 'Do you approve QA deployment?'
            }
        }
        
    stage ('QA Deploy') {
      steps {
      echo "deploying to QA Env "
      deploy adapters: [tomcat9(credentialsId: '341bc648-7df9-4b41-b520-9872cd2d8097', path: '', url: 'http://ec2-18-222-124-177.us-east-2.compute.amazonaws.com:8080/')], contextPath: null, war: '**/*.war'
      }
    }
    
    }
}

def notifySlack(text, channel, attachments) {

    def payload = JsonOutput.toJson([text: text,
        channel: channel,
        attachments: attachments
    ])

    sh "curl -X POST --data-urlencode \'payload=${payload}\' ${slack_url}"
}


No comments:

Post a Comment

How to setup monitoring on Kubernetes Cluster using Prometheus and Grafana | Setup monitoring on EKS Cluster using Prometheus and Grafana

What is Prometheus? Prometheus is an open source monitoring tool Provides out-of-the-box monitoring capabilities for the Kubernetes containe...