React quickly when a Jenkins Pipeline job fails

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.

This integration requires marbot  plus

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 in your Slack channel.
    pipeline {
    agent any
    stages {
    [...]
    }
    post {
    failure {
    httpRequest url: "https://api.marbot.io/v1/endpoint/$endpoint-ID", contentType: "APPLICATION_JSON", httpMode: "POST", requestBody: "{\"buildResult\": \"${currentBuild.currentResult}\", \"${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", requestBody: "{\"buildResult\": \"${currentBuild.currentResult}\", \"${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

Published on