React quickly when a Jenkins Pipeline job fails

Michael Wittig – 06 Nov 2017

Jenkins is the leading open source automation server. Jenkins provides hundreds of plugins to support building, deploying and automating any project.

With marbot, you get alerts in Slack for each Jenkins Pipeline job failure. To do so, Jenkins sends a web request to marbot in case of a failed Pipeline build.

Set up instructions

  1. Install Jenkins plugin http_request
  2. In your Jenkinsfile, go to the post section, and handle the failure condition. Replace $endpoint-ID with the ID of your endpoint. You can get this value by asking @marbot for it on your Slack channel.
    pipeline {
    agent any
    stages {
    [...]
    }
    post {
    failure {
    httpRequest url: 'https://api.marbot.io/v1/endpoint/$endpoint-ID', contentType: 'APPLICATION_JSON', httpMode: 'POST', customHeaders: [[name: 'X-Alert-Key', value: "jenkins-${env.JOB_NAME}"]], requestBody: "{\"buildResult\": \"${currentBuild.currentResult}\", \"build\": \"${env.JOB_NAME}:${currentBuild.number}\", \"buildDuration\": \"${currentBuild.durationString}\", \"buildUrl\": \"${currentBuild.absoluteUrl}\"}"
    }
    }
    }

Sample Alert

To simulate an alert, you can create a new item (type Jenkins Pipeline) with the following pipeline definition. Replace $endpoint-ID with the ID of your endpoint.

pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'exit 1'
}
}
}
post {
failure {
httpRequest url: 'https://api.marbot.io/v1/endpoint/$endpoint-ID', contentType: 'APPLICATION_JSON', httpMode: 'POST', customHeaders: [[name: 'X-Alert-Key', value: "jenkins-${env.JOB_NAME}"]], requestBody: "{\"buildResult\": \"${currentBuild.currentResult}\", \"build\": \"${env.JOB_NAME}:${currentBuild.number}\", \"buildDuration\": \"${currentBuild.durationString}\", \"buildUrl\": \"${currentBuild.absoluteUrl}\"}"
}
}
}

Click on the Build Now link to start the pipeline: I promise, it will fail!

The following alert will be triggered by marbot soon.

Jenkins Alert

Michael Wittig

Michael Wittig

I’m the author of Amazon Web Services in Action. I work as a software engineer, and independent consultant focused on AWS and DevOps.

You can contact me via Email, Twitter, and LinkedIn.

Briefcase icon
Hire me
marbot teaser

Incident Management for Slack

Team up to solve incidents with marbot. Never miss a critical alert. Escalate alerts from your AWS infrastructure among your team members. Strong integrations with all parts of your AWS infrastructure: CloudWatch, Elastic Beanstalk, RDS, EC2, ...

Slack icon
Try for free