-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
144 lines (134 loc) · 5.77 KB
/
action.yaml
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: Send Slack Notification
description: Send a notification to Slack
inputs:
title:
description: Title of the notification
required: true
message:
description: Message to be sent
required: true
channel_id:
description: Channel IDs to send the notification to
required: true
slack_bot_token:
description: Slack bot token
required: true
github_token:
description: Github token
required: true
outputs:
message_ts:
description: Slack message timestamp
value: ${{ steps.deployment_message.outputs.message_ts }}
runs:
using: 'composite'
steps:
- name: Get Commit Message
shell: bash
id: commit
env:
GH_TOKEN: ${{ inputs.github_token }}
run: echo message=$(gh api /repos/${{ github.repository }}/commits/${{ github.sha }} --jq '.commit.message' | head -1 | tr -d '"') >> "$GITHUB_OUTPUT"
# The head -1 is there to ensure multi-line commit messages don't break
# the formatting of the JSON payload as CR/LF control chars are unable
# to be escaped cleanly.
- name: Format Commit Hash
shell: bash
id: hash
run: echo hash=$(echo "${{ github.sha }}" | head -c 7) >> "$GITHUB_OUTPUT"
- name: Get Commit Author
shell: bash
id: author
env:
GH_TOKEN: ${{ inputs.github_token }}
run: |
echo name=$(gh api /repos/${{ github.repository }}/commits/${{ github.sha }} --jq '.author.login') >> "$GITHUB_OUTPUT"
echo url=$(gh api /repos/${{ github.repository }}/commits/${{ github.sha }} --jq '.author.html_url') >> "$GITHUB_OUTPUT"
- name: Get Triggering Actor URL
shell: bash
id: triggering-actor
env:
GH_TOKEN: ${{ inputs.github_token }}
run: |
echo url=$(gh api /users/${{ github.triggering_actor }} --jq '.html_url') >> "$GITHUB_OUTPUT"
- name: Notify Slack
id: deployment_message
uses: slackapi/[email protected]
with:
method: chat.postMessage
token: ${{ inputs.slack_bot_token }}
payload: |
unfurl_links: false
unfurl_media: false
channel: ${{ inputs.channel_id }}
text: "${{ inputs.title }}"
blocks:
- type: header
text:
type: plain_text
text: "${{ inputs.title }}"
emoji: true
- type: section
text:
type: mrkdwn
text: ${{ toJson(inputs.message) }}
- type: divider
- type: section
text:
type: mrkdwn
text: "*Head Commit Message:*\n```${{ steps.commit.outputs.message }}```"
- type: divider
- type: section
fields:
- type: mrkdwn
text: "*Event:*\n${{ github.event_name }}"
- type: mrkdwn
text: "*Triggered By:*\n<${{ steps.triggering-actor.outputs.url }}|${{ github.triggering_actor }}>"
- type: mrkdwn
text: "*Ref:*\n${{ github.ref_type }} <${{ github.server_url }}/${{ github.repository }}/tree/${{ github.ref_name }}|${{ github.ref_name }}>"
- type: mrkdwn
text: "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.workflow }}>"
- type: mrkdwn
text: "*Commit:*\n<${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}|${{ steps.hash.outputs.hash }}>"
- type: mrkdwn
text: "*Commit Author:*\n<${{ steps.author.outputs.url }}|${{ steps.author.outputs.name }}>"
- name: Find Failing Jobs
shell: bash
id: failing_jobs
env:
GH_TOKEN: ${{ inputs.github_token }}
run: |
echo jobs=$(gh run view ${{ github.run_id }} --repo ${{ github.repository }} --json jobs | jq '.jobs[] | select(.conclusion == "failure")' | jq -rsc '[.[] | {"type": "section", "text": {"type": "mrkdwn", "text": "• <\(.url)|\(.name)>"}}]' | sed 's/[][]//g') >> $GITHUB_OUTPUT
# Explanation of this approach:
# 1. Use GitHub CLI to fetch job information for the current workflow run.
# 2. Use jq to filter out only the failed jobs.
# 3. Use jq again to transform each failed job into a Slack block format:
# - Create a "section" block for each job
# - Use "mrkdwn" text type to allow for formatting
# - Format each job as a bullet point with a clickable link
# 4. The -rsc options in jq ensure raw output (-r), slurp input (-s), and compact output (-c).
# 5. Use sed to remove outer square brackets, allowing direct insertion into Slack payload.
#
# This approach was chosen because:
# - It handles formatting and structure in a single step, reducing complexity.
# - It directly creates Slack-compatible block structures, ensuring correct formatting in Slack.
# - It avoids issues with newline characters and JSON escaping.
# - It's more robust and maintainable than trying to format the text after the fact.
- name: Comment Failing Jobs
uses: slackapi/[email protected]
if: steps.failing_jobs.outputs.jobs != ''
with:
method: chat.postMessage
token: ${{ inputs.slack_bot_token }}
payload: |
channel: ${{ inputs.channel_id }}
thread_ts: ${{ steps.deployment_message.outputs.ts }}
unfurl_links: false
unfurl_media: false
text: "Failing Jobs:"
blocks:
- type: header
text:
type: plain_text
text: "Failing Jobs:"
${{ steps.failing_jobs.outputs.jobs }}