Skip to content

Commit f02bfde

Browse files
committed
Use a reusable action for the slack message
1 parent 7ece8c2 commit f02bfde

File tree

2 files changed

+117
-43
lines changed

2 files changed

+117
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Slack Workflow Notification
2+
description: Send a message to Slack when a workflow completes
3+
4+
inputs:
5+
slack_token:
6+
description: 'Slack token'
7+
required: true
8+
slack_channel:
9+
description: 'Slack channel'
10+
required: true
11+
actor:
12+
description: The user who triggered the workflow
13+
required: true
14+
event:
15+
description: The event that triggered the workflow
16+
required: true
17+
conclusion:
18+
description: The conclusion of the workflow
19+
required: true
20+
env:
21+
description: The environment the workflow is related to
22+
required: true
23+
ref:
24+
description: The artifact reference (commit, tag, etc) of the workflow
25+
required: true
26+
workflow_id:
27+
description: The ID of the workflow that is being notified about
28+
required: true
29+
30+
31+
runs:
32+
using: composite
33+
steps:
34+
- name: Context
35+
id: context
36+
shell: bash
37+
run: |
38+
actor="${{ inputs.actor }}"
39+
event="${{ inputs.event }}"
40+
conclusion="${{ inputs.conclusion }}"
41+
env="${{ inputs.env }}"
42+
ref="${{ inputs.ref }}"
43+
44+
if [[ "$conclusion" == "success" ]]; then
45+
emoji=":white_check_mark:"
46+
else
47+
emoji=":x:"
48+
fi
49+
50+
workflow_id="${{ inputs.workflow_id }}"
51+
repo_url="${{ github.server_url }}/${{ github.repository }}"
52+
workflow_url="$repo_url/actions/runs/$workflow_id"
53+
54+
message="$emoji [$env] $actor *$event* ($ref) completed with *$conclusion* $emoji"
55+
56+
echo "message=${message}" >> $GITHUB_OUTPUT
57+
echo "workflow_id=${workflow_id}" >> $GITHUB_OUTPUT
58+
echo "workflow_url=${workflow_url}" >> $GITHUB_OUTPUT
59+
cat $GITHUB_OUTPUT
60+
61+
- name: Notify Slack
62+
uses: mozilla/addons/.github/actions/slack@main
63+
with:
64+
slack_token: ${{ inputs.slack_token }}
65+
method: chat.postMessage
66+
payload: |
67+
channel: "${{ inputs.slack_channel }}"
68+
text: "${{ steps.context.outputs.message }}"
69+
blocks:
70+
- type: section
71+
text:
72+
type: mrkdwn
73+
text: ${{ steps.context.outputs.message }}
74+
- type: actions
75+
elements:
76+
- type: button
77+
text:
78+
type: plain_text
79+
text: "View Workflow (${{ steps.context.outputs.workflow_id }})"
80+
emoji: true
81+
value: workflow_url
82+
url: ${{ steps.context.outputs.workflow_url }}

.github/workflows/default_completed.yml

+35-43
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,46 @@ jobs:
1010
context:
1111
runs-on: ubuntu-latest
1212

13-
outputs:
14-
is_release_master: true
15-
is_release_tag: true
16-
1713
steps:
1814
- uses: actions/checkout@v4
1915
- uses: ./.github/actions/context
2016

21-
notify_slack:
17+
slack_notification:
18+
needs: context
19+
strategy:
20+
matrix:
21+
include:
22+
- actor: ${{ github.event.workflow_run.actor.login }}
23+
event: 'push'
24+
conclusion: 'success'
25+
env: 'dev'
26+
ref: ${{ github.event.workflow_run.head_commit.id }}
27+
- actor: ${{ github.event.workflow_run.actor.login }}
28+
event: 'push'
29+
conclusion: 'failure'
30+
env: 'dev'
31+
ref: ${{ github.event.workflow_run.head_commit.id }}
32+
- actor: ${{ github.event.workflow_run.actor.login }}
33+
event: 'release'
34+
conclusion: 'success'
35+
env: 'production'
36+
ref: 2025.01.01
37+
- actor: ${{ github.event.workflow_run.actor.login }}
38+
event: 'release'
39+
conclusion: 'failure'
40+
env: 'production'
41+
ref: 2025.01.01
42+
2243
runs-on: ubuntu-latest
23-
needs: [context]
24-
if: ${{ needs.context.outputs.is_release_master || needs.context.outputs.is_release_tag }}
2544
steps:
26-
- name: Notify Slack
27-
uses: mozilla/addons/.github/actions/slack@main
28-
29-
env:
30-
event: ${{ github.event.workflow_run.event }}
31-
conclusion: ${{ github.event.workflow_run.conclusion }}
32-
emoji: ${{ github.event.workflow_run.conclusion == 'success' && ':white_check_mark:' || ':x:' }}
33-
workflow_url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}
34-
workflow_id: ${{ github.event.workflow_run.id }}
45+
- uses: actions/checkout@v4
46+
- uses: ./.github/actions/slack-workflow-notification
3547
with:
3648
slack_token: ${{ secrets.SLACK_TOKEN }}
37-
method: chat.postMessage
38-
payload: |
39-
channel: "${{ secrets.SLACK_ADDONS_PRODUCTION_CHANNEL }}"
40-
text: "${{ env.event }} completed with ${{ env.conclusion }}"
41-
blocks:
42-
- type: section
43-
text:
44-
type: mrkdwn
45-
text: "${{ env.emoji }} *${{ env.event }}* completed with *${{ env.conclusion }}*"
46-
- type: context
47-
elements:
48-
- type: mrkdwn
49-
text: "*Workflow:* ${{ env.workflow_id }}"
50-
- type: mrkdwn
51-
text: "*Actor:* ${{ github.event.workflow_run.actor.login }}"
52-
- type: mrkdwn
53-
text: "*Commit:* ${{ github.event.workflow_run.head_commit.id }}"
54-
- type: actions
55-
elements:
56-
- type: button
57-
text:
58-
type: plain_text
59-
text: "View Workflow"
60-
emoji: true
61-
value: workflow_url
62-
url: ${{ env.workflow_url }}
63-
49+
slack_channel: ${{ secrets.SLACK_ADDONS_PRODUCTION_CHANNEL }}
50+
actor: ${{ matrix.actor }}
51+
event: ${{ matrix.event }}
52+
conclusion: ${{ matrix.conclusion }}
53+
env: ${{ matrix.env }}
54+
ref: ${{ matrix.ref }}
55+
workflow_id: ${{ github.event.workflow_run.id }}

0 commit comments

Comments
 (0)