Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
JohJonker committed Feb 3, 2021
1 parent 9229e7d commit 3006fd6
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Entle Web Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
# action-pagerduty-alert
# PagerDuty Alert GitHub Action
Sends a critical PagerDuty alert, e.g. on action failure.

## Prerequisites

1. Create a service integration in PagerDuty:
1. Go to PagerDuty > "Services" > Pick your service > "Integrations" > "Add a new integration"
2. Choose a name (e.g. "Your GitHub CI/CD") and "Use our API directly" with "Events API v2"
3. Copy the integration key
2. Set up a secret in your GitHub repo to store the integration key, e.g. "PAGERDUTY_INTEGRATION_KEY"

## Inputs

`pagerduty-integration-key`

**Required:** the integration key for your PagerDuty service

## Example usage

In your `steps`:

```yaml
- name: Send PagerDuty alert on failure
if: ${{ failure() }}
uses: Entle/[email protected]
with:
pagerduty-integration-key: '${{ secrets.PAGERDUTY_INTEGRATION_KEY }}'
```
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: 'PagerDuty Alert'
description: 'GitHub Action to send a critical PagerDuty alert, e.g. on action failure.'
inputs:
pagerduty-integration-key:
description: 'The integration key for your PagerDuty service'
required: true
runs:
using: 'node12'
main: 'index.js'
branding:
icon: 'alert-triangle'
color: 'red'
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const core = require('@actions/core');
const { context } = require('@actions/github');
const axios = require('axios');

// Trigger the PagerDuty webhook with a given alert
async function sendAlert(alert) {
try {
const response = await axios.post('https://events.pagerduty.com/v2/enqueue', alert);

if (response.status === 202) {
console.log(`Successfully sent PagerDuty alert. Response: ${JSON.stringify(response.data)}`);
} else {
core.setFailed(
`PagerDuty API returned status code ${response.status} - ${JSON.stringify(response.data)}`
);
}
} catch (error) {
core.setFailed(error.message);
}
}

// Run the action
try {
const integrationKey = core.getInput('pagerduty-integration-key');

sendAlert({
payload: {
summary: `${context.repo.repo}: Error in "${context.workflow}" run by @${context.actor}`,
timestamp: new Date().toISOString(),
source: 'GitHub Actions',
severity: 'critical',
custom_details: {
run_details: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
related_commits: context.payload.commits
? context.payload.commits.map((commit) => `${commit.message}: ${commit.url}`).join(', ')
: 'No related commits',
},
},
routing_key: integrationKey,
event_action: 'trigger',
});
} catch (error) {
core.setFailed(error.message);
}
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "action-pagerduty-alert",
"version": "0.1.0",
"description": "GitHub Action to send a critical PagerDuty alert",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Entle/action-pagerduty-alert.git"
},
"keywords": [
"PagerDuty",
"Actions"
],
"author": "Johannes Jonker <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/Entle/action-pagerduty-alert/issues"
},
"homepage": "https://github.com/Entle/action-pagerduty-alert#readme",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"axios": "^0.21.1"
}
}

0 comments on commit 3006fd6

Please sign in to comment.