CircleCI→PagerDuty Change Notifications
An important thing when you are setting up PagerDuty is to make sure that that the responder has as much context as possible to recent changes of the system. At PlushCare we use CircleCI heavily and making sure that a responder to an incident can attribute to a recent change is extremely important in making sure that our uptime and SLOs are met.
We currently use CircleCI for all deploys to production from a branch named Prod
. In the code below you will see that we only send change events to PagerDuty when we deploy to production as only production currently sends notifications through PagerDuty.
-
Go to your Service Directory
-
Click “+ Add Change Integration”
-
Click “+ New Change Integration”
-
Select “PagerDuty Integrations” and from the drop down select “Custom Change Event Transformer”
-
Enter a name for your Integration. Something like “CircleCI” is a good one.
-
Select the service you will be adding this to
-
Click “Next”
-
Click “View Integration”
-
Click “Edit Integration”
-
Take the code below and replace the boilerplate PagerDuty gave you to start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
var data = PD.inputRequest.body; if (data.type == 'workflow-completed') { if (data.pipeline.vcs.branch == 'Prod') { var payload = { summary: data.project.name + " deploy " + data.workflow.status + ": " + data.pipeline.vcs.commit.subject, source: data.webhook.id, custom_details: { "Subject": data.pipeline.vcs.commit.subject, "Body": data.pipeline.vcs.commit.body, "Author": data.pipeline.vcs.commit.author, "Revision": data.pipeline.vcs.revision, "Pipeline Number": data.pipeline.number, }, }; var changeEvent = { payload: payload, links: [{ href: data.workflow.url, text: "View Workflow" }] }; PD.emitChangeEvents([changeEvent]); } }
-
Click “Save changes”
-
Copy the Integration URL. It should look something like:
https://events.pagerduty.com/integration/RANDOM_TEXT/change/enqueue
-
Go to the CircleCI Project settings for the project you would like to setup
-
Go to Webhooks in the sidebar
-
Click “Add Webhook”
-
Below is my recommended configuration:
- Name: “PagerDuty Change Events”
- Webhook URL: Paste the URL you copied earlier
- Secret Token: Leave blank
- Certificate verification: Enabled
- Events:
- Workflow Completed
-
Click “Add Webhook”
-
After a successful workflow go back to the PagerDuty service page and you should see the event in Recent Changes.
Congratulations you should now receive change notifications from CircleCI to your PagerDuty Service.