React quickly when a Jenkins Pipeline job fails

Michael Wittig – 06 Nov 2017 (updated 21 Jan 2021)

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

Consultant focusing on Amazon Web Services (AWS). Entrepreneur building marbot.io. Author of Amazon Web Services in Action, Rapid Docker on AWS, and cloudonaut.io.

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

Published on and updated on

marbot teaser

Chatbot for AWS Monitoring

Configure monitoring for Amazon Web Services: CloudWatch, EC2, RDS, EB, Lambda, and more. Receive and manage alerts via Slack. Solve incidents as a team.

Slack
Add to Slack
Microsoft Teams
Add to Teams