manintheit.org

manintheit.org


Slack Notification and Opsgenie Alerting in Jenkins

Alerting/Notification is an essential part of Tech operations to make sure business continuity and mitigating the financial loss. Nevertheless, self-healing platforms(e.g Kubernetes) on the market, there are some cases that human intervention is indispensable.

In the mean time, proper alerting/notification system are crucial. E-mail notification quite outdated, and can be easily ignored by Operators due to sheer of false positive alerts which yields Alert Fatigue.

Slack

Slack is modern cloud-based communication and collaboration platform. Its aim to manage all of your business work in single place, in terms of communication and management. It has plenty of Apps by which, you can integrate it with other platforms(e.g Opsgenie).

Opsgenie

Opsgenie is a cloud based alert and incident management system, it takes alerts and forwards/calls to correct person/team. It is highly intelligent system that aggregates multiple alerts of the same to avoid Alert Fatigue.

Integrating Slack and Opsgenie with other systems quite easy, thanks to their RESTful API system. One example of it with Jenkins, in which I am sharing this blog post.

You can use Jenkins Shared Library in my GitHub repo to get notified/alerted in case of failure in your CI/CD Pipelines or cronjobs in Jenkins.

Example Jenkins Pipeline

@Library('jenkins-shared-lib') _
pipeline {
    agent any
    stages {
        stage ('Example') {
            steps {
                script {
	            // this execution returns non-zero
                    echod "hello"
                }
            }
        }
    }
    
    post {
        failure {
            script{
                slack(message: "Failed Jenkins Job: <${BUILD_URL}|Click here> for details!")
                opsgenie(message: "Jenkins: Opsgenie Test(Please ignore)", description: "Failed Jenkins Job: <${BUILD_URL}|Click here> for details!")
                
                
            }
        }
    }
}

Note: There is Jenkins plugins for Slack and Opsgenie, but I prefer to use this approach as it requires single plugin(HTTP Request Plugin), which is highly important not to face Jenkins dependency hell.

Assumptions:

  • Slack: Incoming Webhook enabled for desired Slack channel to notified from.
  • Opsgenie: Restful API integration created and Enabled.
  • Jenkins: HTTP Request Plugin available in Jenkins instance.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.