forked from ipdxco/unified-github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
271 lines (256 loc) · 12.5 KB
/
release-check.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
name: Release Checker
on:
workflow_call:
inputs:
branches:
required: false
type: string
default: ${{ format('["{0}"]', github.event.repository.default_branch) }}
jobs:
release-check:
runs-on: ubuntu-latest
steps:
- run: echo "EOF=EOF$RANDOM" >> $GITHUB_ENV
- id: pr
name: Retrieve PR information
env:
PULL_REQUEST: ${{ toJSON(github.event.pull_request) }}
uses: actions/github-script@v6
with:
script: |
let pr = JSON.parse(process.env.PULL_REQUEST);
if (pr == null) {
const {data: pulls} = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.sha
});
const open = pulls.filter(p => p.state == 'open');
if (open.length == 0) {
core.setFailed(`No open PR found for commit ${context.sha}`);
return;
}
if (open.length > 1) {
core.setFailed(`Multiple open PRs found for commit ${context.sha}`);
return;
}
pr = open[0];
}
core.setOutput('json', JSON.stringify(pr));
- uses: actions/checkout@v3
- id: go-mod
uses: pl-strflt/uci/.github/actions/read-go-mod@main
- uses: actions/setup-go@v3
with:
go-version: ${{ fromJSON(steps.go-mod.outputs.json).Go }}.x
- id: version
name: Determine version
env:
GITHUB_TOKEN: ${{ github.token }}
HEAD_FULL_NAME: ${{ fromJSON(steps.pr.outputs.json).head.repo.full_name }}
HEAD_SHA: ${{ fromJSON(steps.pr.outputs.json).head.sha }}
run: |
# If `version.json` file doesn't exists, `version` is `""` and `404` is printed on stderr.
# The step won't be marked as a failure though because the error happens in a subshell.
version="$(gh api -X GET "repos/$HEAD_FULL_NAME/contents/version.json" -f ref="$HEAD_SHA" --jq '.content' | base64 -d | jq -r '.version')"
echo "version=$version"
echo "version=$version" >> $GITHUB_OUTPUT
- id: branch
name: Check if the branch is a release branch
if: steps.version.outputs.version != ''
env:
BRANCHES: ${{ inputs.branches }}
BASE_REF: ${{ fromJSON(steps.pr.outputs.json).base.ref }}
uses: actions/github-script@v6
with:
script: |
const branches = JSON.parse(process.env.BRANCHES);
const release = branches.some(b => {
const regexPattern = b.replace(/\*/g, '.*');
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(process.env.BASE_REF);
});
console.log(`This is a release branch: ${release}`);
core.setOutput('release', release);
- id: tag
name: Check if the tag already exists
if: steps.version.outputs.version != ''
env:
VERSION: ${{ steps.version.outputs.version }}
ALLOWED: ${{ steps.branch.outputs.release == 'true' || contains(fromJSON(steps.pr.outputs.json).labels.*.name, 'release') }}
run: |
git fetch origin --tags
status=0
git rev-list "$VERSION" &> /dev/null || status=$?
if [[ $status == 0 ]]; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "needed=false" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "needed=$ALLOWED" >> $GITHUB_OUTPUT
fi
- name: Install semver (node command line tool)
if: steps.tag.outputs.needed == 'true'
run: npm install -g "https://github.com/npm/node-semver#dc0fe202faaf19a545ce5eeab3480be84180a082" # v7.3.8
- name: Check version
if: steps.tag.outputs.needed == 'true'
env:
VERSION: ${{ steps.version.outputs.version }}
# semver fails if the version is not valid (e.g. v0.1 would fail)
run: semver "$VERSION"
- id: prerelease
if: steps.tag.outputs.needed == 'true'
name: Check if this is a pre-release
env:
VERSION: ${{ steps.version.outputs.version }}
# semver -r fails if the version is not valid or if it is a pre-release (e.g v0.1 or v0.1.0-rc1 would fail)
run: echo "prerelease=$(semver -r "$VERSION" && echo false || echo true)" >> $GITHUB_OUTPUT
- id: prev
name: Determine version number to compare to
if: steps.tag.outputs.needed == 'true'
env:
VERSION: ${{ steps.version.outputs.version }}
PRERELEASE: ${{ steps.prerelease.outputs.prerelease }}
# We need to determine the version number we want to compare to,
# taking into account that this might be a (patch) release on a release branch.
# Example:
# Imagine a module that has releases for v0.1.0, v0.2.0, v0.3.0 and v0.3.0-rc1.
# When trying to cut a release v0.2.1, we need to base our comparisons on v0.2.0.
# When trying to cut a release v0.3.1 or v0.4.0, we need to base our comparisons on v0.3.0.
# When trying to cut a release v0.3.0-rc2, we need to base our comparisons on v0.3.0-rc1.
run: |
git fetch origin --tags
go install github.com/marten-seemann/semver-highest@fcdc98f8820ff0e6613c1bee071c096febd98dbf
vs=$(git tag | paste -sd , -)
if [[ ! -z "$vs" ]]; then
v=$(semver-highest -target "$VERSION" -versions "$vs" -prerelease="$PRERELEASE")
status=$?
if [[ $status != 0 ]]; then
echo $v
exit $status
fi
echo "version=$v" >> $GITHUB_OUTPUT
fi
- id: git-diff
name: run git diff on go.mod file(s)
if: steps.tag.outputs.needed == 'true' && steps.prev.outputs.version != ''
env:
PREV_VERSION: ${{ steps.prev.outputs.version }}
run: |
# First get the diff for the go.mod file in the root directory...
output=$(git diff "$PREV_VERSION..HEAD" -- './go.mod')
# ... then get the diff for all go.mod files in subdirectories.
# Note that this command also finds go.mod files more than one level deep in the directory structure.
output+=$(git diff "$PREV_VERSION..HEAD" -- '*/go.mod')
if [[ -z "$output" ]]; then
output="(empty)"
fi
printf "output<<$EOF\n%s\n$EOF" "$output" >> $GITHUB_OUTPUT
- id: gorelease
name: Run gorelease
if: steps.tag.outputs.needed == 'true' && steps.prev.outputs.version != ''
env:
PREV_VERSION: ${{ steps.prev.outputs.version }}
# see https://github.com/golang/exp/commits/master/cmd/gorelease
run: |
go mod download
go install golang.org/x/exp/cmd/gorelease@f062dba9d201f5ec084d25785efec05637818c00 # https://cs.opensource.google/go/x/exp/+/f062dba9d201f5ec084d25785efec05637818c00
output=$((gorelease -base "$PREV_VERSION") 2>&1 || true)
printf "output<<$EOF\n%s\n$EOF" "$output" >> $GITHUB_OUTPUT
- id: gocompat
name: Check Compatibility
if: steps.tag.outputs.needed == 'true' && steps.prev.outputs.version != ''
env:
PREV_VERSION: ${{ steps.prev.outputs.version }}
run: |
go install github.com/smola/gocompat/cmd/gocompat@8498b97a44792a3a6063c47014726baa63e2e669 # v0.3.0
output=$(gocompat compare --go1compat --git-refs="$PREV_VERSION..HEAD" ./... || true)
if [[ -z "$output" ]]; then
output="(empty)"
fi
printf "output<<$EOF\n%s\n$EOF" "$output" >> $GITHUB_OUTPUT
- id: release
if: steps.tag.outputs.needed == 'true' && fromJSON(steps.pr.outputs.json).head.repo.full_name == fromJSON(steps.pr.outputs.json).base.repo.full_name
uses: galargh/action-gh-release@571276229e7c9e6ea18f99bad24122a4c3ec813f # https://github.com/galargh/action-gh-release/pull/1
with:
draft: true
tag_name: ${{ steps.version.outputs.version }}
generate_release_notes: true
target_commitish: ${{ fromJSON(steps.pr.outputs.json).base.ref }}
- id: message
if: steps.tag.outputs.exists == 'false'
env:
HEADER: |
Suggested version: `${{ steps.version.outputs.version }}`
BODY: |
Comparing to: [${{ steps.prev.outputs.version }}](${{ fromJSON(steps.pr.outputs.json).base.repo.html_url }}/releases/tag/${{ steps.prev.outputs.version }}) ([diff](${{ fromJSON(steps.pr.outputs.json).base.repo.html_url }}/compare/${{ steps.prev.outputs.version }}..${{ fromJSON(steps.pr.outputs.json).head.label }}))
Changes in `go.mod` file(s):
```diff
${{ steps.git-diff.outputs.output }}
```
`gorelease` says:
```
${{ steps.gorelease.outputs.output }}
```
`gocompat` says:
```
${{ steps.gocompat.outputs.output }}
```
BODY_ALT: |
This is the first release of this module.
RELEASE_BRANCH_NOTICE: |
## Cutting a Release (when not on `${{ inputs.branches }}`)
This PR is targeting `${{ fromJSON(steps.pr.outputs.json).base.ref }}`, which is not any of ${{ inputs.branches }}.
If you wish to cut a release once this PR is merged, please add the `release` label to this PR.
DIFF_NOTICE: |
## Cutting a Release (and modifying non-markdown files)
This PR is modifying both `version.json` and non-markdown files.
The Release Checker is not able to analyse files that are not checked in to `${{ fromJSON(steps.pr.outputs.json).base.ref }}`. This might cause the above analysis to be inaccurate.
Please consider performing all the code changes in a separate PR before cutting the release.
RELEASE_NOTICE: |
## Automatically created GitHub Release
A [draft GitHub Release](${{ steps.release.outputs.url }}) has been created.
It is going to be published when this PR is merged.
You can modify its' body to include any release notes you wish to include with the release.
RELEASE_NOTICE_ALT: |
## Automatically created GitHub Release
Pre-creating GitHub Releases on release PRs initiated from forks is not supported.
If you wish to prepare release notes yourself, you should create a draft GitHub Release for tag `${{ steps.version.outputs.version }}` manually.
The draft GitHub Release is going to be published when this PR is merged.
If you choose not to create a draft GitHub Release, a published GitHub Released is going to be created when this PR is merged.
BASE_REF: ${{ fromJSON(steps.pr.outputs.json).base.ref }}
HEAD_LABEL: ${{ fromJSON(steps.pr.outputs.json).head.label }}
HEAD_FULL_NAME: ${{ fromJSON(steps.pr.outputs.json).head.repo.full_name }}
GITHUB_TOKEN: ${{ github.token }}
PREV_VERSION: ${{ steps.prev.outputs.version }}
TAG_NEEDED: ${{ steps.tag.outputs.needed }}
run: |
echo "output<<$EOF" >> $GITHUB_OUTPUT
if [[ "$TAG_NEEDED" == "false" ]]; then
echo "$RELEASE_BRANCH_NOTICE" >> $GITHUB_OUTPUT
else
echo "$HEADER" >> $GITHUB_OUTPUT
if [[ "$PREV_VERSION" != "" ]]; then
echo "$BODY" >> $GITHUB_OUTPUT
else
echo "$BODY_ALT" >> $GITHUB_OUTPUT
fi
diff="$(gh api -X GET "repos/$GITHUB_REPOSITORY/compare/$BASE_REF...$HEAD_LABEL" --jq '.files | map(.filename) | map(select(test("^(version\\.json|.*\\.md)$") | not)) | .[]')"
if [[ "$diff" != "" ]]; then
echo "$DIFF_NOTICE" >> $GITHUB_OUTPUT
fi
if [[ "$GITHUB_REPOSITORY" == "$HEAD_FULL_NAME" ]]; then
echo "$RELEASE_NOTICE" >> $GITHUB_OUTPUT
else
echo "$RELEASE_NOTICE_ALT" >> $GITHUB_OUTPUT
fi
fi
echo "$EOF" >> $GITHUB_OUTPUT
- name: Post message on PR
uses: marocchino/sticky-pull-request-comment@f61b6cf21ef2fcc468f4345cdfcc9bda741d2343 # v2.6.2
if: steps.tag.outputs.exists == 'false'
with:
header: release-check
recreate: true
message: ${{ steps.message.outputs.output }}
number: ${{ fromJSON(steps.pr.outputs.json).number }}