Notify on Workflow Failure and Fix #213
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: Notify on Workflow Failure and Fix | |
on: | |
workflow_run: | |
workflows: ["*"] # This listens to all workflows | |
types: | |
- completed | |
jobs: | |
notify: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check Previous Run Status | |
id: previous_run | |
run: | | |
echo "Checking previous run status..." | |
PREVIOUS_STATUS=$(gh run list --limit 2 --branch main --workflow "${{ github.event.workflow_run.name }}" --json conclusion --jq '.[1].conclusion') | |
echo "previous_status=$PREVIOUS_STATUS" >> $GITHUB_ENV | |
- name: Send Discord Notification on Failure | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
env: | |
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} | |
run: | | |
curl -H "Content-Type: application/json" \ | |
-X POST \ | |
-d '{ | |
"content": "**Workflow Failure Detected on Main Branch** :x:", | |
"embeds": [ | |
{ | |
"author": { | |
"name": "GitHub Actions Bot", | |
"icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png", | |
"url": "https://github.com/${{ github.repository }}" | |
}, | |
"title": "Workflow: '${{ github.event.workflow_run.name }}'", | |
"description": "The workflow **${{ github.event.workflow_run.name }}** has failed on the main branch.", | |
"url": "${{ github.event.workflow_run.html_url }}", | |
"color": 15158332 | |
} | |
] | |
}' \ | |
$DISCORD_WEBHOOK_URL | |
- name: Send Discord Notification on Fix | |
if: ${{ github.event.workflow_run.conclusion == 'success' && env.previous_status == 'failure' }} | |
env: | |
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} | |
run: | | |
curl -H "Content-Type: application/json" \ | |
-X POST \ | |
-d '{ | |
"content": "**Workflow Fixed on Main Branch** :white_check_mark:", | |
"embeds": [ | |
{ | |
"author": { | |
"name": "GitHub Actions Bot", | |
"icon_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png", | |
"url": "https://github.com/${{ github.repository }}" | |
}, | |
"title": "Workflow: '${{ github.event.workflow_run.name }}' Fixed", | |
"description": "The workflow **${{ github.event.workflow_run.name }}** has been fixed on the main branch.", | |
"url": "${{ github.event.workflow_run.html_url }}", | |
"color": 3066993 | |
} | |
] | |
}' \ | |
$DISCORD_WEBHOOK_URL |