-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaction.yaml
More file actions
169 lines (161 loc) · 6.81 KB
/
action.yaml
File metadata and controls
169 lines (161 loc) · 6.81 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Post Slack workflow conclusion notifications
description: Post a Slack message with the workflow conclusion via Slack incoming webhook.
inputs:
# REQUIRED INPUTS
slack-webhook-url:
description: The Slack webhook URL.
required: true
status:
description: The status of the workflow. One of 'success', 'failure', 'cancelled', or 'skipped'.
required: true
# OPTIONAL INPUTS
payload:
description: The Slack Block Kit JSON payload to send to Slack. If provided, this input will override all other inputs other than `slack-webhook-url`. https://app.slack.com/block-kit-builder
required: false
default: ''
header:
description: The notification header, as a plain text string. Defaults to the workflow name.
required: false
success-message:
description: The message to display when the workflow is successful. Accepts markdown syntax.
required: false
default: ':large_green_circle: Workflow completed successfully :mario_luigi_dance:'
failure-message:
description: The message to display when the workflow is successful. Accepts markdown syntax.
required: false
default: ':red_circle: Workflow failed :sad-panda:'
cancelled-message:
description: The message to display when the workflow is cancelled. Accepts markdown syntax.
required: false
default: ':black_circle: Workflow cancelled'
skipped-message:
description: The message to display when the workflow is skipped. Accepts markdown syntax.
required: false
default: ':white_circle: Workflow skipped'
tag:
description: |
Override the commit hash displayed in the notification by resolving this tag to its commit hash.
If provided, this will be resolved to the actual commit hash.
If the tag cannot be resolved, the notification will fallback to using the current commit hash.
required: false
default: ''
runs:
using: composite
steps:
- name: Checkout repository
if: ${{ inputs.tag != '' }}
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history to ensure all tags are available
- name: Construct Slack variables
id: slack-variables
shell: bash
run: |
# Git Variables
fallbackBranchName=$(echo "${{ github.ref }}" | cut -c12-)
shortCommitHash=$(echo "${{ github.sha }}" | cut -c1-7)
# Resolve tag to commit hash if provided, otherwise use default commit hash
if [[ -n "${{ inputs.tag }}" ]]; then
if lookupCommitHashForTag=$(git rev-parse "${{ inputs.tag }}" 2>&1); then
shortCommitHash=$(echo "${lookupCommitHashForTag}" | cut -c1-7)
echo "Resolved tag '${{ inputs.tag }}' to commit hash: ${lookupCommitHashForTag}"
else
echo "Error resolving tag '${{ inputs.tag }}': ${lookupCommitHashForTag}"
echo "Falling back to current workflow commit hash: ${{ github.sha }}"
fi
fi
# Determine status message
if [[ "${{ inputs.status }}" == 'success' ]]; then
statusMessage=$(echo "${{ inputs.success-message }}")
elif [[ "${{ inputs.status }}" == 'failure' ]]; then
statusMessage=$(echo "${{ inputs.failure-message }}")
elif [[ "${{ inputs.status }}" == 'cancelled' ]]; then
statusMessage=$(echo "${{ inputs.cancelled-message }}")
elif [[ "${{ inputs.status }}" == 'skipped' ]]; then
statusMessage=$(echo "${{ inputs.skipped-message }}")
else
echo "Invalid workflow status: ${{ inputs.status }}"
exit 1
fi
# Output All Variables
echo "fallback-branch-name=${fallbackBranchName}" >> $GITHUB_OUTPUT
echo "short-commit-hash=${shortCommitHash}" >> $GITHUB_OUTPUT
echo "status-message=${statusMessage}" >> $GITHUB_OUTPUT
# Echo all variables for debugging
echo "fallback-branch-name=${fallbackBranchName}"
echo "short-commit-hash=${shortCommitHash}"
echo "status-message=${statusMessage}"
- name: Construct Slack payload
id: slack-payload
if: ${{ inputs.payload == '' }}
shell: bash
run: |
PAYLOAD=$(cat << EOF
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ inputs.header || github.workflow }}",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${{ steps.slack-variables.outputs.status-message }}"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repository:*\n<${{ github.server_url }}/${{ github.repository }}|${{ github.repository }}>"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n<${{ github.server_url }}/${{ github.repository }}/tree/${{ github.head_ref || steps.slack-variables.outputs.fallback-branch-name }}|${{ github.head_ref || steps.slack-variables.outputs.fallback-branch-name }}>"
},
{
"type": "mrkdwn",
"text": "*Workflow Run:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.run_number }}>"
},
{
"type": "mrkdwn",
"text": "*Commit:*\n<${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}|${{ steps.slack-variables.outputs.short-commit-hash }}>"
}
]
},
{
"type": "actions",
"elements": [
{
"type": "button",
"action_id": "view-workflow-run",
"style": "${{ inputs.status == 'failure' && 'danger' || 'primary' }}",
"url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}",
"text": {
"type": "plain_text",
"emoji": true,
"text": "${{ inputs.status == 'failure' && 'View failed run' || 'View workflow run' }}"
}
}
]
}
]
}
EOF
)
echo "payload<<EOF" >> $GITHUB_OUTPUT
echo "$PAYLOAD" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Send notification
uses: slackapi/slack-github-action@fcfb566f8b0aab22203f066d80ca1d7e4b5d05b3 # v1.27.1
env:
SLACK_WEBHOOK_URL: ${{ inputs.slack-webhook-url }}
SLACK_WEBHOOK_TYPE: 'INCOMING_WEBHOOK'
with:
payload: ${{ inputs.payload != '' && inputs.payload || steps.slack-payload.outputs.payload }}