-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaction.yml
227 lines (219 loc) · 8.69 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
name: Tag and release
description: GitHub Action to create a tag and an optional release
inputs:
# Note: there is an explicit reason why there is no sha input parameter - see the readme
tag:
type: string
required: true
delete_existing:
type: boolean
required: false
default: false
release:
type: boolean
required: false
default: false
release_description:
type: string
required: false
default: ''
release_auto_notes:
type: boolean
required: false
default: false
runs:
using: composite
steps:
- name: Validate inputs
shell: bash
env:
TAG: ${{ inputs.tag }}
run: |
git check-ref-format "tags/$TAG" > /dev/null
if [[ $? != "0" ]]; then
echo "Invalid tag"
exit 1
fi
- name: Delete existing release if one exists
if: ${{ inputs.release == 'true' && inputs.delete_existing == 'true' }}
shell: bash
env:
TAG: ${{ inputs.tag }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# Get id for an existing release matching $TAG
# https://docs.github.com/en/rest/releases/releases#get-a-release-by-tag-name
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X GET https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG \
-H "Accept: application/vnd.github.v3+json")
if [[ $RESP_CODE != "200" ]]; then
RESP_MSG=$(jq -r .message __response.json)
if [[ $RESP_MSG != "Not Found" ]]; then
echo "Unable to check tag status for $TAG - HTTP response code was $RESP_CODE and message was $RESP_MSG"
cat __response.json
exit 1
fi
fi
RELEASE_ID=$(jq .id __response.json)
if [[ $RESP_CODE == "404" || $RELEASE_ID == "null" ]]; then
echo "Did not find an existing release for tag $TAG"
else
# https://docs.github.com/en/rest/releases/releases#delete-a-release
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X DELETE https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}")
if [[ $RESP_CODE != "204" ]]; then
echo "Unable to delete release $RELEASE_ID for tag $TAG - HTTP response code was $RESP_CODE"
cat __response.json
exit 1
fi
echo "Deleted existing release $RELEASE_ID for tag $TAG"
fi
- name: Delete existing tag if one exists
if: ${{ inputs.delete_existing == 'true' }}
shell: bash
env:
TAG: ${{ inputs.tag }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# Check if tag currently exists
# Note: not using https://api.github.com/repos/$GITHUB_REPOSITORY/git/refs/tags/<tag>
# because that uses a "starts-with" filter, so it will also match -beta1 and -rc1 tags
# https://docs.github.com/en/rest/git/refs#get-a-reference
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X GET https://api.github.com/repos/$GITHUB_REPOSITORY/git/refs/tags \
-H "Accept: application/vnd.github.v3+json")
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to check tag status - HTTP response code was $RESP_CODE"
cat __response.json
exit 1
fi
FOUND="true"
# Check there are any tags so we can use jq array selector later
if [[ $(jq 'map(type)' __response.json) =~ object ]]; then
EXISTING_TAG=$(jq ".[] | select(.ref == \"refs/tags/$TAG\")" __response.json)
if [[ $EXISTING_TAG == "" ]]; then
FOUND="false"
fi
fi
if [[ $FOUND == "false" ]]; then
echo "Did not find an existing tag for $TAG"
else
# Delete tag via GitHub API
# https://docs.github.com/en/rest/reference/git#delete-a-reference
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X DELETE https://api.github.com/repos/$GITHUB_REPOSITORY/git/refs/tags/$TAG \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}")
if [[ $RESP_CODE != "204" ]]; then
echo "Unable to delete existing TAG $TAG - HTTP response code was $RESP_CODE"
cat __response.json
exit 1
fi
echo "Deleted existing tag $TAG"
fi
- name: Create tag
# Creating a release will also create a tag, so only create explicitly create tag if not creating release
if: ${{ inputs.release == 'false' }}
shell: bash
env:
TAG: ${{ inputs.tag }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_SHA: ${{ github.sha }}
run: |
# Create new tag via GitHub API
# https://docs.github.com/en/rest/reference/git#create-a-reference
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X POST https://api.github.com/repos/$GITHUB_REPOSITORY/git/refs \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}" \
-d @- << EOF
{
"sha": "$GITHUB_SHA",
"ref": "refs/tags/$TAG"
}
EOF
)
if [[ $RESP_CODE != "201" ]]; then
echo "Unable to create tag $TAG for sha ${{ github.sha }} - HTTP response code was $RESP_CODE"
cat __response.json
exit 1
fi
echo "New tag $TAG created for sha ${{ github.sha }}"
- name: Create release
if: ${{ inputs.release == 'true' }}
shell: bash
env:
TAG: ${{ inputs.tag }}
RELEASE_DESCRIPTION: ${{ inputs.release_description }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_SHA: ${{ github.sha }}
run: |
# Work out if release should be marked as the latest
# Only do the for stable semver tags
# Note = not using "legacy" as GitHub seems to only consider if it's the latest tag in a particular major line
# whereas we only want to mark it the latest if it's the latest tag overall
MAKE_LATEST="false"
if [[ $TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
# Gets latest 100 tags across all majors from via GitHub API
# https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repository-tags
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X GET "https://api.github.com/repos/${GITHUB_REPOSITORY}/tags?per_page=100" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ github.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
)
if [[ $RESP_CODE != "200" ]]; then
echo "Unable to read list of tags - HTTP response code was $RESP_CODE"
cat __response.json
exit 1
fi
# Get the latest stable tag
LATEST_TAG=$(jq -r '.[].name' __response.json | grep -Po '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V -r | head -n 1)
echo "LATEST_TAG is $LATEST_TAG"
if [[ $LATEST_TAG =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
PATCH=${BASH_REMATCH[3]}
NEXT_TAG="$MAJOR.$MINOR.$((PATCH+1))"
if [[ $TAG == $NEXT_TAG ]]; then
MAKE_LATEST="true"
fi
fi
fi
echo "MAKE_LATEST is $MAKE_LATEST"
# Escape double quotes '"' => '\"'
RELEASE_DESCRIPTION=${RELEASE_DESCRIPTION//\"/\\\"}
# Create new release via GitHub API
# https://docs.github.com/en/rest/releases/releases#create-a-release
RESP_CODE=$(curl -w %{http_code} -s -o __response.json \
-X POST https://api.github.com/repos/$GITHUB_REPOSITORY/releases \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}" \
-d @- << EOF
{
"tag_name": "$TAG",
"target_commitish": "$GITHUB_SHA",
"name": "$TAG",
"body": "$RELEASE_DESCRIPTION",
"draft": false,
"prerelease": false,
"generate_release_notes": ${{ inputs.release_auto_notes }},
"make_latest": "$MAKE_LATEST"
}
EOF
)
if [[ $RESP_CODE != "201" ]]; then
echo "Unable to create release for tag $TAG - HTTP response code was $RESP_CODE"
cat __response.json
exit 1
fi
echo "New release $TAG created"
- name: Delete temporary files
shell: bash
if: always()
run: |
if [[ -f __response.json ]]; then
rm __response.json
fi