-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
111 lines (107 loc) · 4.31 KB
/
action.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
name: job-notification
description: A GitHub Action for sending job notifications
inputs:
repository:
description: |
Name of the repository (e.g scribd/job-notification)
Useful for sending notifications on behalf of another repository.
required: false
default: ${{ github.repository }}
token:
description: Slack token to send the notification via "gitscribd" app.
required: true
channel:
description: The Slack channel name for receiving the notification
required: true
status:
description: Customize the notification status to be "success", "failure" or a custom value.
required: false
default: ${{ job.status }}
environment:
description: Customize the environment to be something other than branch name
required: false
default: ${{ github.ref_name }}
message:
description: Customize the notification message.
runs:
using: composite
steps:
- name: Set fields
env:
input_message: ${{ inputs.message }}
commit_message: ${{ github.event.head_commit.message }}
shell: bash
if: always()
id: fields
run: |
if [ "${{ inputs.status }}" = "success" ]; then
echo "emoji=large_green_square" >> $GITHUB_OUTPUT
echo "color=good" >> $GITHUB_OUTPUT
elif [ "${{ inputs.status }}" = "failure" ]; then
echo "emoji=large_red_square" >> $GITHUB_OUTPUT
echo "color=danger" >> $GITHUB_OUTPUT
else
echo "emoji=large_orange_square" >> $GITHUB_OUTPUT
echo "color=warning" >> $GITHUB_OUTPUT
fi
if [ ! -z "$input_message" ]; then
message=$input_message
elif [ ! -z "$commit_message" ]; then
# get commit message from `push` trigger
commit_message=$(echo "$commit_message" | head -n 1)
message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>"
elif git rev-parse --is-inside-git-dir > /dev/null 2>&1; then
# get commit message from the current git directory to support `workflow_dispatch` trigger
commit_message=$(git log --format=%B -n 1 | head -n 1)
message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>"
else
commit_message="Link to the latest commit in the repository"
message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>"
fi
echo "message=$message" >> $GITHUB_OUTPUT
author=${{ github.event.pusher.name }} # context from `push` trigger
author=${author:-${{ github.event.sender.login }}} # context from `workflow_dispatch` trigger
echo "author=$author" >> $GITHUB_OUTPUT
- name: Send notification
if: always()
uses: slackapi/[email protected]
env:
SLACK_BOT_TOKEN: ${{ inputs.token }}
with:
channel-id: ${{ inputs.channel }}
payload: |
{
"text": ":${{ steps.fields.outputs.emoji }}: *Workflow <https://github.com/${{ inputs.repository }}/actions/runs/${{ github.run_id }}/|${{ github.workflow }}> ${{ inputs.status }}*",
"attachments": [
{
"color": "${{ steps.fields.outputs.color }}",
"fields": [
{
"title": "Repository",
"value": "<https://github.com/${{ inputs.repository }}|${{ inputs.repository }}>",
"short": true
},
{
"title": "Environment/Branch",
"value": "${{ inputs.environment || github.ref_name }}",
"short": true
},
{
"title": "Author",
"value": "<https://github.com/${{ steps.fields.outputs.author }}|${{ steps.fields.outputs.author }}>",
"short": true
},
{
"title": "Job",
"value": "${{ github.job }}",
"short": true
},
{
"title": "Commit Message",
"value": ${{ toJSON(steps.fields.outputs.message) }},
"short": false
}
]
}
]
}