-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
101 lines (95 loc) · 3.84 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
name: Trigger CI
description: GitHub Action to find and trigger the appropriate CI workflow
inputs:
branch:
type: string
required: true
validate_branch:
type: boolean
required: false
default: true
runs:
using: composite
steps:
- name: Validate branch exists
shell: bash
if: ${{ inputs.validate_branch == 'true' }}
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BRANCH: ${{ inputs.branch }}
run: |
# https://docs.github.com/en/free-pro-team@latest/rest/branches/branches?apiVersion=2022-11-28#list-branches
RESP_CODE=$(curl -w %{http_code} -s -L -o __branches.json \
-X GET \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$GITHUB_REPOSITORY/branches?per_page=100 \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Failed to read branches - HTTP response code was $RESP_CODE"
exit 1
fi
if [[ $(cat __branches.json | jq "any(.[]; .name == \"$BRANCH\")") == "false" ]]; then
echo "Could not find branch $BRANCH"
exit 1
fi
- name: Find workflow
id: find-workflow
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BRANCH: ${{ inputs.branch }}
run: |
FOUND=0
# Need to loop specifically in this order to find the right file, this is because:
# - gha-ci has both a ci.yml file (shared with other workflows) and an action-ci.yml file (the one we want to run)
# - gha-action-ci has both an action-ci.yml file (shared with other workflows) and an action-ci-self.yml file (the one we want to run)
FILENAMES='action-ci-self.yml action-ci.yml ci.yml'
for FILENAME in $FILENAMES; do
URL=https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$BRANCH/.github/workflows/$FILENAME
RESP_CODE=$(curl -w %{http_code} -s -L -o /dev/null $URL)
if [[ $RESP_CODE == "200" ]]; then
FOUND=1
echo "FILENAME is $FILENAME"
echo "filename=$FILENAME" >> $GITHUB_OUTPUT
break
elif [[ $RESP_CODE != "404" ]]; then
echo "Failed to check file $FILENAME - HTTP response code was $RESP_CODE"
exit 1
fi
done
if [[ $FOUND == 0 ]]; then
echo "Could not workflow file for branch $BRANCH - tried $FILENAMES"
# If there's no CI file, then this workflow has worked as expected. Don't mark it as a failure.
# This allows us to do auto-merge ups for things like developer-docs which don't have CI
# without adding a dummy CI which could result in a false sense of security when reviewing PRs etc.
exit 0
fi
- name: Send API request
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
BRANCH: ${{ inputs.branch }}
FILENAME: ${{ steps.find-workflow.outputs.filename }}
run: |
# https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
RESP_CODE=$(curl -w %{http_code} -s -L -o /dev/null \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$GITHUB_REPOSITORY/actions/workflows/$FILENAME/dispatches \
-d "{\"ref\":\"$BRANCH\"}"
)
if [[ $RESP_CODE != "204" ]]; then
echo "Failed to dispatch workflow - HTTP response code was $RESP_CODE"
exit 1
fi
- name: Delete temporary files
shell: bash
if: always()
run: |
if [[ -f __branches.json ]]; then
rm __branches.json
fi