-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
155 lines (149 loc) · 6.28 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: Dispatch CI
description: GitHub Action to trigger ci.yml workflow_dispatch event via GitHub API
runs:
using: composite
steps:
- name: Get type
id: gettype
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# repository that has a twice-weekly schedule run on consecutive days - use day of year
date_part=$(date +%-j)
type=""
if [[ $GITHUB_REPOSITORY == "silverstripe/installer" ]] || \
[[ $GITHUB_REPOSITORY == "silverstripe/recipe-kitchen-sink" ]]
then
# respository that has daily schedule - use hour
date_part=$(date +%H)
fi
if (( $date_part % 2 == 1 )); then
# current major, next-minor e.g. "5"
type=current,next-minor
else
# previous major, next-patch e.g. "4.13"
type=previous,next-patch
fi
echo "type=$type" >> $GITHUB_OUTPUT
- name: Check if should dispatch workflow
id: checkshoulddispatch
shell: bash
env:
RUN_BRANCHES_ON_PREVIOUS_MAJOR: "false"
TYPE: ${{ steps.gettype.outputs.type }}
run: |
major_type=$(echo $TYPE | cut -d "," -f 1)
if [[ $RUN_BRANCHES_ON_PREVIOUS_MAJOR == "false" ]] && [[ $major_type == "previous" ]]; then
echo "Workflow is configured to not dispatch workflow for branches on previous major. Exiting."
echo "do_dispatch=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "do_dispatch=true" >> $GITHUB_OUTPUT
- name: Get branch
id: getbranch
if: steps.checkshoulddispatch.outputs.do_dispatch == 'true'
shell: bash
env:
TYPE: ${{ steps.gettype.outputs.type }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
major_type=$(echo $TYPE | cut -d "," -f 1)
minor_type=$(echo $TYPE | cut -d "," -f 2)
# https://docs.github.com/en/rest/branches/branches#list-branches
RESP_CODE=$(curl -w %{http_code} -s -L -o __branches.json \
-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 \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Failed to list branches - HTTP response code was $RESP_CODE"
exit 1
fi
RESP_CODE=$(curl -w %{http_code} -s -L -o __tags.json \
-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/tags \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Failed to list tags - HTTP response code was $RESP_CODE"
exit 1
fi
major_branch=""
minor_branch=""
major_branches=$(jq -r '.[] | select(.name | test("^[0-9]+$")) | .name' __branches.json | sort -V -r)
# "major_branches" needs to be quoted to preserve line-breaks
major_branches_count=$(echo "$major_branches" | wc -l)
if (( $major_branches_count < 1 )); then
echo "No major branches found, cannot continue"
exit 1
fi
latest_major_branch=$(echo "$major_branches" | head -1)
latest_major_stable_tags_count=0
# not escaping rx . as \. because jq will say it is a compile error
latest_major_stable_tags=$(jq -r ".[] | select(.name | test(\"^$latest_major_branch.[0-9]+.[0-9]+$\")) | .name" __tags.json | sort -V -r)
if [[ "$latest_major_stable_tags" != "" ]]; then
latest_major_stable_tags_count=$(echo "$latest_major_stable_tags" | wc -l)
fi
if [[ $major_type == "current" ]]; then
major_branch=$(echo "$major_branches" | head -1)
if (( $latest_major_stable_tags_count == 0 )) && (( $major_branches_count >= 2 )); then
major_branch=$(echo "$major_branches" | head -2 | tail -1)
fi
elif [[ $major_type == "previous" ]]; then
if (( $major_branches_count < 2 )); then
echo "Only one major branch found, not running CI on this cycle - will run on next cycle"
exit 0
fi
major_branch=$(echo "$major_branches" | head -2 | tail -1)
if (( $latest_major_stable_tags_count == 0 )) && (( $major_branches_count >= 3 )); then
major_branch=$(echo "$major_branches" | head -3 | tail -1)
fi
fi
if [[ $minor_type == "next-patch" ]]; then
minor_branches=$(jq -r ".[] | select(.name | test(\"^$major_branch.[0-9]+$\")) | .name" __branches.json | sort -V -r)
minor_branches_count=$(echo "$minor_branches" | wc -l)
if (( $minor_branches_count == 0 )); then
echo "No minor branches found on major branch $major_branch when minor_type is next-patch, cannot continue"
exit 1
fi
minor_branch=$(echo "$minor_branches" | head -1)
fi
branch=$major_branch
if [[ $minor_branch != "" ]]; then
branch="$minor_branch"
fi
echo "branch is $branch"
echo "branch=$branch" >> $GITHUB_OUTPUT
- name: Send API request
if: steps.checkshoulddispatch.outputs.do_dispatch == 'true'
shell: bash
env:
GITHUB_REPOSITORY: ${{ github.repository }}
REF: ${{ steps.getbranch.outputs.branch }}
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/ci.yml/dispatches \
-d "{\"ref\":\"$REF\"}"
)
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
if [[ -f __tags.json ]]; then
rm __tags.json
fi