|
8 | 8 | branches:
|
9 | 9 | - main
|
10 | 10 |
|
| 11 | +permissions: |
| 12 | + actions: read |
| 13 | + |
11 | 14 | jobs:
|
12 |
| - notify-slack: |
| 15 | + on-failure: |
13 | 16 | runs-on: ubuntu-latest
|
14 |
| - if: ${{ github.event.workflow_run.conclusion == 'failure' }} |
| 17 | + if: | |
| 18 | + (github.event.workflow_run.conclusion == 'failure' || |
| 19 | + github.event.workflow_run.conclusion == 'timed_out') && |
| 20 | + github.event.workflow_run.name != 'Slack Notifications for Failed GitHub Actions' |
| 21 | + timeout-minutes: 10 |
15 | 22 | steps:
|
16 |
| - - name: Check out repository |
17 |
| - uses: actions/checkout@v4 |
18 |
| - |
19 |
| - - name: Send Slack notification |
| 23 | + - name: Send Slack Notification |
| 24 | + uses: ravsamhq/notify-slack-action@v2 |
| 25 | + with: |
| 26 | + status: ${{ github.event.workflow_run.conclusion }} |
| 27 | + notification_title: " ${{github.event.workflow_run.name}} - ${{github.event.workflow_run.conclusion}} on ${{github.event.workflow_run.head_branch}} - <${{github.server_url}}/${{github.repository}}/actions/runs/${{github.event.workflow_run.id}}|View Failure>" |
| 28 | + message_format: ":fire: *${{github.event.workflow_run.name}}* ${{github.event.workflow_run.conclusion}} in <${{github.server_url}}/${{github.repository}}/${{github.event.workflow_run.head_branch}}|${{github.repository}}>" |
| 29 | + footer: "Linked Repo <${{github.server_url}}/${{github.repository}}|${{github.repository}}> | <${{github.server_url}}/${{github.repository}}/actions/runs/${{github.event.workflow_run.id}}|View Failure>" |
| 30 | + env: |
| 31 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_WORKFLOW_NOTIF }} |
| 32 | + |
| 33 | + - name: Check Slack Notification Status |
20 | 34 | if: failure()
|
21 | 35 | run: |
|
22 |
| - const fetch = require('node-fetch'); |
23 |
| -
|
24 |
| - const SLACK_WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL; |
25 |
| - const context = JSON.parse(process.env.GITHUB_CONTEXT); |
26 |
| -
|
27 |
| - const slackMessage = { |
28 |
| - channel: '#github', |
29 |
| - issue: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`, |
30 |
| - status: context.jobStatus, |
31 |
| - text: `GitHub Actions workflow failed: ${context.workflow}` |
32 |
| - }; |
33 |
| -
|
34 |
| - await fetch(SLACK_WEBHOOK_URL, { |
35 |
| - method: 'POST', |
36 |
| - headers: { |
37 |
| - 'Content-Type': 'application/json', |
38 |
| - }, |
39 |
| - body: JSON.stringify(slackMessage), |
40 |
| - }); |
41 |
| - env: |
42 |
| - SLACK_WEBHOOK_URL: ${{ vars.SLACK_WEBHOOK_FAIL_ACTIONS }} |
43 |
| - GITHUB_CONTEXT: ${{ toJson(github) }} |
| 36 | + echo "Failed to send Slack notification" |
| 37 | + echo "Workflow Run ID: ${{ github.event.workflow_run.id }}" |
0 commit comments