PagerDuty - Create Incident

Octopus.Script exported 2024-12-20 by twerthi belongs to ‘PagerDuty’ category.

Creates an Incident against a PagerDuty Service.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Token

PagerDuty.API.AuthorizationToken =

Please supply the API token of your PagerDuty instance.

Found here: https://mydomain.pagerduty.com/api_keys

Title

PagerDuty.Incident.Title =

Please enter a title for this incident.

Details

PagerDuty.Body.Details =

Please enter the details of the Incident.

Service Id

PagerDuty.Service.Id =

Please enter the Service Id for the Incident.

Priority

PagerDuty.Priority.Code =

Please select a Priority level for the Incident.

Urgency

PagerDuty.Urgency.Code =

Please enter an Urgency for the Incident.

Escalation Policy Id

PagerDuty.EscalationPolicy.Id =

Please enter an Escalation Policy Id, leave blank if you don’t have one.

Script body

Steps based on this template will execute the following PowerShell script.

# Gather Octopus variables
$pagerDutyToken = $OctopusParameters['PagerDuty.API.AuthorizationToken']
$incidentTitle = $OctopusParameters['PagerDuty.Incident.Title']
$serviceId = $OctopusParameters['PagerDuty.Service.Id']
$incidentPriority = $OctopusParameters['PagerDuty.Priority.Code']
$incidentUrgency = $OctopusParameters['PagerDuty.Urgency.Code']
$escalationPolicyId = $OctopusParameters['PagerDuty.EscalationPolicy.Id']
$incidentDetails = $OctopusParameters['PagerDuty.Body.Details']
$pagerDutyFrom = "Octopus Deploy Project: $($OctopusParameters['Octopus.Project.Name']) Environment $($OctopusParameters['Octopus.Environment.Name'])"

# Configure request headers
$headers = @{
    "Authorization" = "Token token=$pagerDutyToken"
    "Content-Type" = "application/json"
    "Accept" = "application/json"
    "From" = "$pagerDutyFrom"
}

# Build Incident Object
$incidentPayload = @{
  incident = @{
    type = "incident"
    title = $incidentTitle
    service = @{
      id = $serviceId
      type = "service_reference"
    }
  
    urgency = $incidentUrgency
    body = @{
      type = "incident_body"
      details = $incidentDetails
    }
  }
}

# Check to see if an escalation id was specified
if (![string]::IsNullOrWhitespace($escalationPolicyId))
{
  $policyDetails = @{
    type = "escalation_policy_reference"
    id = $escalationPolicyId
  }

  $incidentPayload.incident.Add("escalation_policy", $policyDetails)
}


# Get Priority
$priorities = (Invoke-RestMethod -Method Get -Uri "https://api.pagerduty.com/priorities" -Headers $headers)
$priority = ($priorities.priorities | Where-Object {$_.Name -eq $incidentPriority})

# Add priority to body
$priorityBody = @{
  id = "$($priority.id)"
  type = "priority_reference"
}
$incidentPayload.incident.Add("priority", $priorityBody)

# Submit incident
try
{
  $responseResult = Invoke-RestMethod -Method Post -Uri "https://api.pagerduty.com/incidents" -Body ($incidentPayload | ConvertTo-Json -Depth 10) -Headers $headers
  Write-Host "Successfully created incident."
  $responseResult.incident
}
catch [System.Exception] {
        Write-Host $_.Exception.Message
        
        $ResponseStream = $_.Exception.Response.GetResponseStream()
        $Reader = New-Object System.IO.StreamReader($ResponseStream)
        $Reader.ReadToEnd() | Write-Error
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "38a1ab46-b8dd-48b4-ab21-73068c737a43",
  "Name": "PagerDuty - Create Incident",
  "Description": "Creates an Incident against a PagerDuty Service.",
  "Version": 1,
  "ExportedAt": "2024-12-20T22:23:43.782Z",
  "ActionType": "Octopus.Script",
  "Author": "twerthi",
  "Packages": [],
  "Parameters": [
    {
      "Id": "39ed5ced-1b7e-4d69-af4a-dbac5a4bf7df",
      "Name": "PagerDuty.API.AuthorizationToken",
      "Label": "Token",
      "HelpText": "\n\nPlease supply the API token of your PagerDuty instance.\n\nFound here: https://mydomain.pagerduty.com/api_keys",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "c092e897-15df-4561-8505-f76650cdec01",
      "Name": "PagerDuty.Incident.Title",
      "Label": "Title",
      "HelpText": "Please enter a title for this incident.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "9d048098-25cb-4736-8523-97eed8e15f3a",
      "Name": "PagerDuty.Body.Details",
      "Label": "Details",
      "HelpText": "Please enter the details of the Incident.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "MultiLineText"
      }
    },
    {
      "Id": "cdc36097-7255-4f58-bf3b-afb4ae1b80c6",
      "Name": "PagerDuty.Service.Id",
      "Label": "Service Id",
      "HelpText": "Please enter the Service Id for the Incident.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "888bd996-ef6a-4d06-b8a0-7afd7dc888b7",
      "Name": "PagerDuty.Priority.Code",
      "Label": "Priority",
      "HelpText": "Please select a Priority level for the Incident.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "None|None\nP1|P1\nP2|P2\nP3|P3\nP4|P4\nP5|P5"
      }
    },
    {
      "Id": "dcf56174-8588-448d-8ab0-1d7285f6b5e2",
      "Name": "PagerDuty.Urgency.Code",
      "Label": "Urgency",
      "HelpText": "Please enter an Urgency for the Incident.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "high|High\nlow|Low"
      }
    },
    {
      "Id": "45206809-1211-454b-a724-fe0e924ed11c",
      "Name": "PagerDuty.EscalationPolicy.Id",
      "Label": "Escalation Policy Id",
      "HelpText": "Please enter an Escalation Policy Id, leave blank if you don't have one.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "# Gather Octopus variables\n$pagerDutyToken = $OctopusParameters['PagerDuty.API.AuthorizationToken']\n$incidentTitle = $OctopusParameters['PagerDuty.Incident.Title']\n$serviceId = $OctopusParameters['PagerDuty.Service.Id']\n$incidentPriority = $OctopusParameters['PagerDuty.Priority.Code']\n$incidentUrgency = $OctopusParameters['PagerDuty.Urgency.Code']\n$escalationPolicyId = $OctopusParameters['PagerDuty.EscalationPolicy.Id']\n$incidentDetails = $OctopusParameters['PagerDuty.Body.Details']\n$pagerDutyFrom = \"Octopus Deploy Project: $($OctopusParameters['Octopus.Project.Name']) Environment $($OctopusParameters['Octopus.Environment.Name'])\"\n\n# Configure request headers\n$headers = @{\n    \"Authorization\" = \"Token token=$pagerDutyToken\"\n    \"Content-Type\" = \"application/json\"\n    \"Accept\" = \"application/json\"\n    \"From\" = \"$pagerDutyFrom\"\n}\n\n# Build Incident Object\n$incidentPayload = @{\n  incident = @{\n    type = \"incident\"\n    title = $incidentTitle\n    service = @{\n      id = $serviceId\n      type = \"service_reference\"\n    }\n  \n    urgency = $incidentUrgency\n    body = @{\n      type = \"incident_body\"\n      details = $incidentDetails\n    }\n  }\n}\n\n# Check to see if an escalation id was specified\nif (![string]::IsNullOrWhitespace($escalationPolicyId))\n{\n  $policyDetails = @{\n    type = \"escalation_policy_reference\"\n    id = $escalationPolicyId\n  }\n\n  $incidentPayload.incident.Add(\"escalation_policy\", $policyDetails)\n}\n\n\n# Get Priority\n$priorities = (Invoke-RestMethod -Method Get -Uri \"https://api.pagerduty.com/priorities\" -Headers $headers)\n$priority = ($priorities.priorities | Where-Object {$_.Name -eq $incidentPriority})\n\n# Add priority to body\n$priorityBody = @{\n  id = \"$($priority.id)\"\n  type = \"priority_reference\"\n}\n$incidentPayload.incident.Add(\"priority\", $priorityBody)\n\n# Submit incident\ntry\n{\n  $responseResult = Invoke-RestMethod -Method Post -Uri \"https://api.pagerduty.com/incidents\" -Body ($incidentPayload | ConvertTo-Json -Depth 10) -Headers $headers\n  Write-Host \"Successfully created incident.\"\n  $responseResult.incident\n}\ncatch [System.Exception] {\n        Write-Host $_.Exception.Message\n        \n        $ResponseStream = $_.Exception.Response.GetResponseStream()\n        $Reader = New-Object System.IO.StreamReader($ResponseStream)\n        $Reader.ReadToEnd() | Write-Error\n}"
  },
  "Category": "PagerDuty",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/pagerduty-create-incident.json",
  "Website": "/step-templates/38a1ab46-b8dd-48b4-ab21-73068c737a43",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABhQTFRF////QbwAveatVcMrmtp9N7oA1+/NcMxCz2uBkgAAAsJJREFUeNrsm9ua6iAMRktIwvu/8WzHK20NR1vds9bXa+jqD4ht2DYAAAAAAAAAAAAAAAAA+Au4PZK/VUQePVL53kTSA4gggggiiCCCCCKIIIIIIogggggiiCDyN0VcSoyK97aopUpOK0Vcy+115Q17caXfN5qlVca9ZLu3aeGVFopoSa38u7MsDRq7B93MuIgU6+2rlkpJ4wyLaOr1SBaH4jMeoyKebaQ3C7oTs/NFfP1Q1jRHOdfjZYeSLhCZG8x62GS6QmTK43gZzleIzD69vH6CjIkUm+vSfNEaOCmi6/tcEEi/yNxMv+PLZ8iAiMyPAtPlTQ6I6OGWMOCo1/oqaFVmRcp+B1UkQnOuTPe9SKXJ32ZnRfJunHj/RkDCNatlwz/9D/H5pkxHtjQl2p1Y03+wWRF5uqfsI+PxoVe1cAa9SWRsaD6vSzlosinj9SJjveYgkY8W2XpE5AoRe0MiQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiKLEylflIj3fLH6pkSib4iNTcoliWhUEahWrR5aN1nnEtl9SS/RLTV9nn6utWockJMPooS9ykBR9e7LvfmsiNcQrZRw7AsjbhUcYYtqlfKWfpGUaxzVB8eBVZs9aHJJcdAnlDnJJSIS/+6P1En6FSL7UkBb3uQZIvuVcrrqt3vNWpNIfX1eUhT9bpGjn67ZSPwKkcOnJ+cuWStEXqwvZd1qfo7Iy1M95dQ8pkWC00mja/BQHpMiOTpl5TpSkJ3H8pgSsVqn/cfPkvp2soil0lAgLKXn3ELW0ThGRcxy6/FQ0ZIaTo3Zrf7bt22diDUUsfcdcnXX+2wJjoX2H5td9PLhA/lfRRKJkAiJkAiJkAiJkAiJkAiJkAiJkAiJkAiJkMhXiDy9D/1aEXeRh2sDAAAAAAAAAAAAAAAAgIAfAQYAXo0zRlgxYJcAAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, December 20, 2024