Slack Notifications for Failed GitHub Actions #74
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Slack Notifications for Failed GitHub Actions | |
on: | |
workflow_run: | |
workflows: ["*"] | |
types: | |
- completed | |
branches: | |
- main | |
jobs: | |
notify-slack: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v4 | |
- name: Send Slack notification | |
if: failure() | |
run: | | |
const fetch = require('node-fetch'); | |
const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL; | |
const context = JSON.parse(process.env.GITHUB_CONTEXT); | |
const slackMessage = { | |
channel: '#github', | |
issue: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, | |
status: context.jobStatus, | |
text: `GitHub Actions workflow failed: ${context.workflow}` | |
}; | |
await fetch(SLACK_WEBHOOK_URL, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(slackMessage), | |
}); | |
env: | |
SLACK_WEBHOOK_URL: ${{ vars.SLACK_WEBHOOK_FAIL_ACTIONS }} | |
GITHUB_CONTEXT: ${{ toJson(github) }} |