Skip to content

Commit 38f8710

Browse files
authored
Add more info to Notification and notify for additional workflows (#226)
1 parent 8f39e76 commit 38f8710

File tree

2 files changed

+69
-55
lines changed

2 files changed

+69
-55
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -122,58 +122,3 @@ jobs:
122122
build-args: |
123123
VERSION=${{ steps.meta.outputs.version }}
124124
GIT_COMMIT=${{ github.sha }}
125-
126-
notify:
127-
name: Notify
128-
runs-on: ubuntu-20.04
129-
needs: build-docker
130-
if: always() && github.ref == 'refs/heads/master'
131-
steps:
132-
- name: Workflow Status
133-
id: check
134-
uses: martialonline/workflow-status@v2
135-
- name: Output Variables
136-
id: commit
137-
run: |
138-
echo "::set-output name=sha::$(echo ${GITHUB_SHA} | cut -c1-7)"
139-
echo "::set-output name=repo::$(echo ${GITHUB_REPOSITORY} | cut -d '/' -f 2)"
140-
- name: Send Notification
141-
uses: 8398a7/action-slack@v3
142-
if:
143-
steps.check.outputs.status == 'failure' ||
144-
steps.check.outputs.status == 'cancelled'
145-
with:
146-
status: custom
147-
custom_payload: |
148-
{
149-
username: 'Github',
150-
icon_emoji: ':octocat:',
151-
mention: 'channel',
152-
attachments: [{
153-
title: '[${{ steps.commit.outputs.repo }}] ${{ github.workflow }} pipeline has failed',
154-
color: '${{ steps.check.outputs.status }}' == 'failure' ? 'danger' : 'warning',
155-
fields: [{
156-
title: 'Commit Hash',
157-
value: '${{ steps.commit.outputs.sha }}',
158-
short: true
159-
},
160-
{
161-
title: 'Author',
162-
value: '${{ github.actor }}',
163-
short: true
164-
},
165-
{
166-
title: 'Commit Message',
167-
value: `${{ github.event.head_commit.message }}`,
168-
short: false
169-
},
170-
{
171-
title: 'Pipeline URL',
172-
value: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}',
173-
short: false
174-
}]
175-
}]
176-
}
177-
env:
178-
GITHUB_TOKEN: ${{ github.token }}
179-
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

.github/workflows/notifications.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Notification
2+
3+
on:
4+
workflow_run:
5+
branches: [master]
6+
workflows:
7+
- "Continuous Integration"
8+
- "CodeQL"
9+
- "Fossa"
10+
- "Lint"
11+
types:
12+
- completed
13+
14+
jobs:
15+
on-failure:
16+
runs-on: ubuntu-20.04
17+
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
18+
steps:
19+
- name: Output Variables
20+
continue-on-error: true
21+
id: vars
22+
run: |
23+
suite_id=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} | jq '.check_suite_id')
24+
event=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} | jq -r '.event')
25+
name=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/check-suites/$suite_id/check-runs | jq -r '[.check_runs[] | select(.conclusion=="failure")][0].name')
26+
url=$(curl -sS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/${{ github.repository }}/check-suites/$suite_id/check-runs | jq -r '[.check_runs[] | select(.conclusion=="failure")][0].html_url')
27+
message_sanitized=$(awk '{gsub("&","&amp;");gsub("<","\\&lt;");gsub(">","\\&gt;");print}' <<<'${{ github.event.workflow_run.head_commit.message }}')
28+
29+
echo "::set-output name=name::$name"
30+
echo "::set-output name=url::$url"
31+
echo "::set-output name=event::$event"
32+
echo "::set-output name=message::$message_sanitized"
33+
34+
- name: Send Notification
35+
uses: 8398a7/action-slack@v3
36+
with:
37+
status: custom
38+
custom_payload: |
39+
{
40+
username: 'Github',
41+
icon_emoji: ':github:',
42+
mention: 'channel',
43+
attachments: [{
44+
title: '[${{ github.event.repository.full_name }}] ${{ github.event.workflow.name }} pipeline has failed (${{ steps.vars.outputs.event }})',
45+
color: 'danger',
46+
fields: [{
47+
title: 'Commit',
48+
value: `<https://github.com/${{ github.repository }}/commit/${{ github.event.workflow_run.head_commit.id }}|${{ steps.vars.outputs.message }}>`,
49+
short: true
50+
},
51+
{
52+
title: 'Failed Job',
53+
value: `<${{ steps.vars.outputs.url }}|${{ steps.vars.outputs.name }}>`,
54+
short: true
55+
},
56+
{
57+
title: 'Author',
58+
value: `${{ github.event.workflow_run.head_commit.author.name }}`,
59+
short: true
60+
},
61+
{
62+
title: 'Pipeline URL',
63+
value: `<https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}|${{ github.event.workflow_run.id }}>`,
64+
short: true
65+
}]
66+
}]
67+
}
68+
env:
69+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

0 commit comments

Comments
 (0)