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
- Install Jenkins plugin
http_request
- In your Jenkinsfile, go to the
post
section, and handle thefailure
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.
Take your AWS monitoring to a new level! Chatbot for AWS Monitoring: Configure monitoring, escalate alerts, solve incidents.