PagerDuty with Jenkins Pipelines

Pipeline as a Code is already here and it’s awesome for the most part :D . However certain plugins are still not available for use. One plugin is for PagerDuty, there is already a pending ticket for the feature but still not available during the time of this writing.

Quick work around is to integrate natively via PagerDuty’s API using Curl. Just follow the steps below and you will be done in no time.

1.Go to PagerDuty’s service integrations page:

2. Select “Use our API directly” and “Events API v2”,

3. On Jenkins, Add the “Integration Key” as global credentials.

4. Configure the Pipeline. Below is the bare minimum needed to trigger an alert. Load the integration key in the “environment definitions” and set the trigger at the post build step as seen below. Refer to PagerDuty’s documentation for more information.

// Declaritve pipeline
// Declaritve pipeline
pipeline {
agent {
node {
label 'master'
}
}
environment {
PAGERDUTY_SERVERS_INT_KEY = credentials('PAGERDUTY_SERVERS_INT_KEY')
}
stages {
stage('build docker image') {
steps {
sh """
echo "TEST"
exit 1
"""
}
}
}
post {
failure {
sh """
curl -X POST -H "content-type: application/json" \
-d '{"routing_key":"${env.PAGERDUTY_SERVERS_INT_KEY}","event_action":"trigger","payload":{"summary":"${env.JOB_NAME} job failed ${env.BUILD_URL}","source":"Jenkins","severity":"critical","component":"exploratory-stats","group":"prod-datapipe","class":"deploy"}}' \
https://events.pagerduty.com/v2/enqueue
"""
}
}
}

5. The pipeline example above when executed will purposely fail and trigger the alert. Voila! you’re done!

[Pipeline] node
Running on master in /var/lib/jenkins/jobs/PagerDuty Integration Test/workspace
[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] retry
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build docker image)
[Pipeline] sh
[workspace] Running shell script
+ echo TEST
TEST
+ exit 1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] sh
[workspace] Running shell script
+ curl -X POST -H 'content-type: application/json' -d '{"routing_key":"****","event_action":"trigger","payload":{"summary":"PagerDuty Integration Test job failed http://jenkins.posumeads.com/job/PagerDuty%20Integration%20Test/8/","source":"Jenkins","severity":"critical","component":"exploratory-stats","group":"prod-datapipe","class":"deploy"}}' https://events.pagerduty.com/v2/enqueue
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 413 100 95 100 318 103 346 --:--:-- --:--:-- --:--:-- 346
{"status":"success","message":"Event processed","dedup_key":"60342e1cca664eab869aee35573d4cd8"}[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // retry
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

It is really simple and easy to implement, although a native plugin will be better and cleaner. Till the day it gets released, this should be enough to get us through in monitoring our reoccurring Jenkins jobs.