-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
47 lines (46 loc) · 2.13 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
name: 'Should I notify slack?'
description: 'Decide if a notification to slack is necessary taking into account the previous build result.'
branding:
icon: 'bell'
color: 'green'
inputs:
needs_context:
description: 'json representation of the `needs` object in your context (`toJson(needs)`)'
required: true
branch:
description: 'The branch to get the status from'
required: true
default: 'main'
github_token:
description: 'A github token (secrets.GITHUB_TOKEN will suffice)'
required: true
outputs:
should_send_message:
description: "`yes|no` depending on if the message should be sent"
value: ${{ steps.determine-notification.outputs.should_send_message }}
runs:
using: "composite"
steps:
- name: Determine if we need to notify
id: determine-notification
shell: bash
run: |
workflow_id=$(curl --header 'authorization: Bearer ${{ inputs.github_token }}' \
--header 'content-type: application/json' \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }} | jq -r .workflow_id)
echo "workflow id: $workflow_id"
last_status=$(curl --silent --header 'authorization: Bearer ${{ inputs.github_token }}' \
--header 'content-type: application/json' \
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/$workflow_id/runs?per_page=1&status=completed&branch=${{ inputs.branch }}" | jq -r .workflow_runs[0].conclusion)
echo "status of the previous build: $last_status"
needs_context="${{ inputs.needs_context }}"
if [[ $(echo $needs_context | grep "result: failure") ]]; then
echo "One of the jobs in the "needs" array failed"
echo "should_send_message=yes" >> $GITHUB_OUTPUT
elif [[ $last_status == failure ]]; then
echo "The last run for this workflow failed so this is a recovery"
echo "should_send_message=yes" >> $GITHUB_OUTPUT
else
echo "The last run did not fail and none of the jobs in needs failed either"
echo "should_send_message=no" >> $GITHUB_OUTPUT
fi