Skip to content

Commit 7b2a555

Browse files
authored
Merge pull request #2 from scribd/helen/SERF-2442/action
[SERF-2442] Add notification action
2 parents c1cb8a5 + 49b5194 commit 7b2a555

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

.github/workflows/test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Test
2+
3+
on: [push, workflow_dispatch]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-22.04
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v3
11+
with:
12+
token: ${{ secrets.SCRIBD_GITHUB_GENERIC_TOKEN }}
13+
- name: Run action
14+
uses: ./
15+
with:
16+
token: ${{ secrets.SCRIBD_SLACK_GENERIC_TOKEN }}
17+
channel: test-release-notification
18+
test-custom-message:
19+
runs-on: ubuntu-22.04
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v3
23+
with:
24+
token: ${{ secrets.SCRIBD_GITHUB_GENERIC_TOKEN }}
25+
- name: Run action
26+
uses: ./
27+
with:
28+
token: ${{ secrets.SCRIBD_SLACK_GENERIC_TOKEN }}
29+
channel: test-release-notification
30+
message: <https://scribd.com|Custom message>
31+
test-overwrite-repository:
32+
runs-on: ubuntu-22.04
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v3
36+
with:
37+
token: ${{ secrets.SCRIBD_GITHUB_GENERIC_TOKEN }}
38+
- name: Run action
39+
uses: ./
40+
with:
41+
token: ${{ secrets.SCRIBD_SLACK_GENERIC_TOKEN }}
42+
channel: test-release-notification
43+
repository: scribd/node-chassis

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
11
# job-notification
2+
3+
[![Test](https://github.com/scribd/job-notification/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/scribd/job-notification/actions/workflows/test.yml)
4+
5+
A GitHub Action for sending job notifications
6+
7+
## Table of contents
8+
9+
- [Quick Start](#quick-start)
10+
- [Advanced usage](#advanced-usage)
11+
- [Customize message](#customize-message)
12+
- [Overwrite repository link](#overwrite-repository-link)
13+
- [Development](#development)
14+
- [Testing](#testing)
15+
- [Maintainers](#maintainers)
16+
17+
## Quick Start
18+
19+
```yaml
20+
steps:
21+
- name: Send notification
22+
if: always() # Always send notification regardless of the job status.
23+
uses: scribd/job-notification@main
24+
with:
25+
token: ${{ secrets.SCRIBD_SLACK_GENERIC_TOKEN }}
26+
channel: test-release-notification
27+
```
28+
29+
## Advanced usage
30+
31+
### Customize message
32+
33+
By default, the notification uses the latest commit message. You can overwrite that using the `message` field.
34+
35+
To test the message formating, use the [Block Kit Builder](https://app.slack.com/block-kit-builder/).
36+
37+
```yaml
38+
steps:
39+
- name: Send notification
40+
if: always()
41+
uses: scribd/job-notification@main
42+
with:
43+
token: ${{ secrets.SCRIBD_SLACK_GENERIC_TOKEN }}
44+
channel: test-release-notification
45+
message: <https://github.com/${{ github.repository }}|Released update>
46+
```
47+
48+
### Overwrite repository link
49+
50+
By default, the notification links to the repository that triggers the job. You can overwrite that using the `repository` field, which is useful for managing the release for multiple repositories in one place. See [scribd-api-proto](https://github.com/scribd/scribd-api-proto) for example.
51+
52+
```yaml
53+
# Example: release.yml in scribd-api-proto
54+
steps:
55+
- name: Send notification
56+
if: always()
57+
uses: scribd/job-notification@main
58+
with:
59+
token: ${{ secrets.SCRIBD_SLACK_GENERIC_TOKEN }}
60+
channel: test-release-notification
61+
repository: scribd/scribd-api-ruby
62+
```
63+
64+
## Development
65+
66+
### Testing
67+
68+
You can test your changes by pushing them to a branch, which will trigger the [test](.github/workflows/test.yml) workflow to send the notifications to the [test-release-notification](https://scribd.slack.com/archives/C04U68KR6CU) channel on Slack.
69+
70+
## Maintainers
71+
72+
Made with ❤️ by the Service Foundations teams.

action.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: job-notification
2+
description: A GitHub Action for sending job notifications
3+
inputs:
4+
repository:
5+
description: |
6+
Name of the repository (e.g scribd/job-notification)
7+
Useful for sending notifications on behalf of another repository.
8+
required: true
9+
default: ${{ github.repository }}
10+
token:
11+
description: Slack token to send the notification via "gitscribd" app.
12+
required: true
13+
channel:
14+
description: The Slack channel name for receiving the notification
15+
required: true
16+
message:
17+
description: Customize the notification message.
18+
19+
runs:
20+
using: composite
21+
steps:
22+
- name: Set fields
23+
shell: bash
24+
if: always()
25+
id: fields
26+
run: |
27+
if [ "${{ job.status }}" = "success" ]; then
28+
echo "emoji=large_green_square" >> $GITHUB_OUTPUT
29+
echo "color=good" >> $GITHUB_OUTPUT
30+
elif [ "${{ job.status }}" = "failure" ]; then
31+
echo "emoji=large_red_square" >> $GITHUB_OUTPUT
32+
echo "color=danger" >> $GITHUB_OUTPUT
33+
else
34+
echo "emoji=large_orange_square" >> $GITHUB_OUTPUT
35+
echo "color=warning" >> $GITHUB_OUTPUT
36+
fi
37+
38+
if [ ! -z "${{ inputs.message }}" ]; then
39+
message="${{ inputs.message }}"
40+
elif [ ! -z "${{ github.event.head_commit.message }}" ]; then
41+
# get commit message from `push` trigger
42+
commit_message=$(echo "${{ github.event.head_commit.message }}" | head -n 1)
43+
message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>"
44+
elif git rev-parse --is-inside-git-dir > /dev/null 2>&1; then
45+
# get commit message from the current git directory to support `workflow_dispatch` trigger
46+
commit_message=$(git log --format=%B -n 1 | head -n 1)
47+
message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>"
48+
else
49+
commit_message=Link to the latest commit in the repository
50+
message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>"
51+
fi
52+
echo "message=$message" >> $GITHUB_OUTPUT
53+
54+
author=${{ github.event.pusher.name }} # context from `push` trigger
55+
author=${author:-${{ github.event.sender.login }}} # context from `workflow_dispatch` trigger
56+
echo "author=$author" >> $GITHUB_OUTPUT
57+
58+
- name: Send notification
59+
if: always()
60+
uses: slackapi/[email protected]
61+
env:
62+
SLACK_BOT_TOKEN: ${{ inputs.token }}
63+
with:
64+
channel-id: ${{ inputs.channel }}
65+
payload: |
66+
{
67+
"text": ":${{ steps.fields.outputs.emoji }}: *Workflow <https://github.com/${{ inputs.repository }}/actions/runs/${{ github.run_id }}/|${{ github.workflow }}> ${{ job.status }}*",
68+
"attachments": [
69+
{
70+
"color": "${{ steps.fields.outputs.color }}",
71+
"fields": [
72+
{
73+
"title": "Repository",
74+
"value": "<https://github.com/${{ inputs.repository }}|${{ inputs.repository }}>",
75+
"short": true
76+
},
77+
{
78+
"title": "Environment",
79+
"value": "${{ github.ref_name == 'main' && 'production' || github.ref_name }}",
80+
"short": true
81+
},
82+
{
83+
"title": "Author",
84+
"value": "<https://github.com/${{ steps.fields.outputs.author }}|${{ steps.fields.outputs.author }}>",
85+
"short": true
86+
},
87+
{
88+
"title": "Job",
89+
"value": "${{ github.job }}",
90+
"short": true
91+
},
92+
{
93+
"title": "Commit Message",
94+
"value": "${{ steps.fields.outputs.message }}",
95+
"short": false
96+
}
97+
]
98+
}
99+
]
100+
}

0 commit comments

Comments
 (0)