James Duffy

CircleCI→PagerDuty Change Notifications

// By Updated:

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.

  1. Go to your Service Directory

  2. Click “+ Add Change Integration”

  3. Click “+ New Change Integration”

  4. Select “PagerDuty Integrations” and from the drop down select “Custom Change Event Transformer”

  5. Enter a name for your Integration. Something like “CircleCI” is a good one.

  6. Select the service you will be adding this to

  7. Click “Next”

  8. Click “View Integration”

  9. Click “Edit Integration”

  10. 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]);
      }
    }
    
  11. Click “Save changes”

  12. Copy the Integration URL. It should look something like: https://events.pagerduty.com/integration/RANDOM_TEXT/change/enqueue

  13. Go to the CircleCI Project settings for the project you would like to setup

  14. Go to Webhooks in the sidebar

  15. Click “Add Webhook”

  16. 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
  17. Click “Add Webhook”

  18. 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.