Repository navigation
85 lines (78 loc) · 4.05 KB
/
Copy pathnotify-google-chat.yml
File metadata and controls
85 lines (78 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
name: Notify Google Chat
# Posts a message to a Google Chat space when one of the workflows listed
# below ends with "failure" or "timed_out" (not "cancelled").
#
# - To get notified about another workflow, add its `name:` to the list.
# - The incoming webhook URL is the repository secret
# GOOGLE_CHAT_WEBHOOK_URL. Without it the job does nothing, so forks
# that did not set it are not affected.
# - Only runs on saltstack/salt-nightlies (see the `if:` below). The file
# lives in saltstack/salt because the salt-nightlies mirror sync deletes
# files that do not exist here (see mirror-from-saltstack-salt.yaml).
# - `workflow_run` only starts workflows from the default branch, so this
# file has to be on the default branch to have any effect.
# - "Run workflow" (workflow_dispatch) sends a test message, to check the
# secret and the Chat space without needing a failing run.
on:
workflow_dispatch: {}
workflow_run:
workflows:
- Nightly Stress Test
types:
- completed
permissions:
actions: read # list the failed jobs and steps of the run
jobs:
notify:
name: Notify on failure
if: >-
github.repository == 'saltstack/salt-nightlies' &&
(github.event_name == 'workflow_dispatch' ||
contains(fromJSON('["failure", "timed_out"]'), github.event.workflow_run.conclusion))
runs-on: ubuntu-latest
env:
GOOGLE_CHAT_WEBHOOK_URL: ${{ secrets.GOOGLE_CHAT_WEBHOOK_URL }}
steps:
- name: Post to Google Chat
if: env.GOOGLE_CHAT_WEBHOOK_URL != ''
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
# The fallbacks (after ||) are for the manual test run, where
# there is no workflow_run event.
RUN_ID: ${{ github.event.workflow_run.id || github.run_id }}
RUN_URL: ${{ github.event.workflow_run.html_url || format('{0}/{1}/actions/runs/{2}', github.server_url, github.repository, github.run_id) }}
WORKFLOW: ${{ github.event.workflow_run.name || 'Test message - the Google Chat webhook works' }}
BRANCH: ${{ github.event.workflow_run.head_branch || github.ref_name }}
EVENT: ${{ github.event.workflow_run.event || github.event_name }}
CONCLUSION: ${{ github.event.workflow_run.conclusion || 'test' }}
run: |
# Branch and job names are not trusted text: drop the characters
# Google Chat reads as links and mentions.
clean() { tr -d '<>`*~' ; }
# "<job> (<failed step>, ...)" for each failed job, comma separated.
# The message is still useful without it, so ignore API errors.
# No pipe into clean(): a pipeline's exit status is that of its last
# command, so a failed gh call would not reach the || fallback.
# shellcheck disable=SC2016 # $s is a jq variable, not a shell one
jobs=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/jobs?per_page=100" \
--jq '[.jobs[] | select(.conclusion == "failure" or .conclusion == "timed_out") | .name + (([.steps[] | select(.conclusion == "failure" or .conclusion == "timed_out") | .name] | join(", ")) as $s | if $s == "" then "" else " (" + $s + ")" end)] | join(", ")' \
2>/dev/null) || jobs=""
jobs=$(printf '%s' "${jobs}" | clean)
name=$(printf '%s' "${WORKFLOW}" | clean)
repo_name="Salt Nightly"
if [ "${CONCLUSION}" = "test" ]; then
text="🧪 ${name} 🧪"
else
text="🚨 ${name} needs attention 🚨"
if [ -n "${jobs}" ]; then
text="${text}"$'\n'"❌ Jobs needing attention: ${jobs}"
fi
fi
text="${text}"$'\n'"📦 Repo: ${repo_name}"
text="${text}"$'\n'"🌿 Branch: $(printf '%s' "${BRANCH}" | clean)"
text="${text}"$'\n'"🔗 <${RUN_URL}|View run>"
jq -n --arg text "${text}" '{text: $text}' \
| curl --fail --silent --show-error --max-time 30 \
--header 'Content-Type: application/json; charset=UTF-8' \
--data @- "${GOOGLE_CHAT_WEBHOOK_URL}"