forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (168 loc) · 7.72 KB
/
slack-notifications.yml
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
##
# A reusable workflow for posting messages to the Making WordPress
# Core Slack Instance by submitting data to Slack webhook URLs
# received by Slack Workflows.
##
name: Slack Notifications
on:
workflow_run:
workflows:
- Code Coverage Report
- Coding Standards
- End-to-end Tests
- JavaScript Tests
- PHP Compatibility
- PHPUnit Tests
- Test NPM
- Test old branches
types:
- completed
branches:
- '[3-4].[0-9]'
- '5.[0-8]'
workflow_call:
inputs:
calling_status:
description: 'The status of the calling workflow'
type: string
required: true
secrets:
SLACK_GHA_SUCCESS_WEBHOOK:
description: 'The Slack webhook URL for a successful build.'
required: true
SLACK_GHA_CANCELLED_WEBHOOK:
description: 'The Slack webhook URL for a cancelled build.'
required: true
SLACK_GHA_FIXED_WEBHOOK:
description: 'The Slack webhook URL for a fixed build.'
required: true
SLACK_GHA_FAILURE_WEBHOOK:
description: 'The Slack webhook URL for a failed build.'
required: true
env:
CURRENT_BRANCH: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_branch || github.ref_name }}
jobs:
# Gathers the details needed for Slack notifications.
#
# These details are passed as outputs to the subsequent, dependant jobs that
# submit data to Slack webhook URLs configured to post messages.
#
# Performs the following steps:
# - Retrieves the workflow ID (if necessary).
# - Retrieves the workflow URL (if necessary).
# - Retrieves the previous workflow run and stores its conclusion.
# - Sets the previous conclusion as an output.
# - Prepares the commit message.
# - Constructs and stores a message payload as an output.
prepare:
name: Prepare notifications
runs-on: ubuntu-latest
if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event.workflow_run.event != 'pull_request' }}
outputs:
previous_conclusion: ${{ steps.previous-conclusion.outputs.previous_conclusion }}
payload: ${{ steps.create-payload.outputs.payload }}
steps:
- name: Get the workflow ID
id: current-workflow-id
if: ${{ github.event_name == 'push' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
uses: actions/github-script@441359b1a30438de65712c2fbca0abe4816fa667 # v5.0.0
with:
script: |
const workflow_run = await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.run_id }},
});
return workflow_run.data.workflow_id;
- name: Get details about the previous workflow run
id: previous-result
uses: actions/github-script@441359b1a30438de65712c2fbca0abe4816fa667 # v5.0.0
with:
script: |
const previous_runs = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.workflow_id || steps.current-workflow-id.outputs.result }},
branch: '${{ env.CURRENT_BRANCH }}',
per_page: 1,
page: 2,
});
return previous_runs.data.workflow_runs[0].conclusion;
- name: Store previous conclusion as an output
id: previous-conclusion
run: echo "::set-output name=previous_conclusion::${{ steps.previous-result.outputs.result }}"
- name: Get the commit message
id: current-commit-message
uses: actions/github-script@441359b1a30438de65712c2fbca0abe4816fa667 # v5.0.0
if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }}
with:
script: |
const commit_details = await github.rest.repos.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref: '${{ github.sha }}'
});
return commit_details.data.commit.message;
- name: Prepare commit message.
id: commit-message
run: |
COMMIT_MESSAGE=$(cat <<'EOF' | awk 'NR==1' | sed 's/`/\\`/g' | sed 's/\"/\\\\\\"/g' | sed 's/\$/\\$/g'
${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_commit.message || ( github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' ) && fromJson( steps.current-commit-message.outputs.result ) || github.event.head_commit.message }}
EOF
)
echo "::set-output name=commit_message_escaped::${COMMIT_MESSAGE}"
- name: Construct payload and store as an output
id: create-payload
run: echo "::set-output name=payload::{\"workflow_name\":\"${{ github.event_name == 'workflow_run' && github.event.workflow_run.name || github.workflow }}\",\"ref_name\":\"${{ env.CURRENT_BRANCH }}\",\"run_url\":\"https://github.com/WordPress/wordpress-develop/actions/runs/${{ github.event_name == 'workflow_run' && github.event.workflow_run.id || github.run_id }}\",\"commit_message\":\"${{ steps.commit-message.outputs.commit_message_escaped }}\"}"
# Posts notifications when a workflow fails.
failure:
name: Failure notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'failure' || inputs.calling_status == 'failure' || failure() }}
steps:
- name: Post failure notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_FAILURE_WEBHOOK }}
# Posts notifications the first time a workflow run succeeds after previously failing.
fixed:
name: Fixed notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ needs.prepare.outputs.previous_conclusion == 'failure' && ( github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' || inputs.calling_status == 'success' ) && success() }}
steps:
- name: Post failure notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_FIXED_WEBHOOK }}
# Posts notifications when a workflow is successful.
success:
name: Success notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' || inputs.calling_status == 'success' && success() }}
steps:
- name: Post success notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_SUCCESS_WEBHOOK }}
# Posts notifications when a workflow is cancelled.
cancelled:
name: Cancelled notifications
runs-on: ubuntu-latest
needs: [ prepare ]
if: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'cancelled' || inputs.calling_status == 'cancelled' || cancelled() }}
steps:
- name: Post cancelled notifications to Slack
uses: slackapi/slack-github-action@d5d276d7ae0f38f29322b80da9baf985cc80f8b1 # v1.15.0
with:
payload: ${{ needs.prepare.outputs.payload }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GHA_CANCELLED_WEBHOOK }}