-
Notifications
You must be signed in to change notification settings - Fork 11
66 lines (60 loc) · 3.12 KB
/
release.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
name: Release
on:
pull_request:
paths: [internal/version/version.go]
push:
branches: [main]
paths: [internal/version/version.go]
permissions: read-all
jobs:
validate:
name: Validate Release
runs-on: ubuntu-latest
permissions:
contents: write # To be able to create draft releases
steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
- name: Setup go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5
with:
go-version: stable
cache-dependency-path: '**/go.mod'
# Obtains the current configured version tag from source, and verifies it is a valid tag name.
# Also checks whether the tag already exists.
- name: Determine version
id: version
run: |-
set -euo pipefail
# From https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string, with added v prefix.
VERSION_TAG_REGEX='^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-((0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$'
version=$(grep -E "${VERSION_TAG_REGEX}" <(go run . version | cut -d' ' -f 2))
echo "tag=${version}" >> "${GITHUB_OUTPUT}"
if gh release view "${version}" --json isDraft; then
echo "exists=true" >> "${GITHUB_OUTPUT}"
else
echo "exists=false" >> "${GITHUB_OUTPUT}"
fi
env:
GH_TOKEN: ${{ github.token }}
# If this is a pull request, and the release does not yet exist, the PR title must be "release: <tag>"
- name: 'Pull Request title must be "release: ${{ steps.version.outputs.tag }}"'
if: "github.event_name == 'pull_request' && !fromJSON(steps.version.outputs.exists) && format('release: {0}', steps.version.outputs.tag) != github.event.pull_request.title"
run: |-
echo "Please update the PR title to \"release: ${{ steps.version.outputs.tag }}\" (instead of ${EVENT_PR_TITLE})"
exit 1
env:
EVENT_PR_TITLE: ${{ toJSON(github.event.pull_request.title) }}
# Release must not already exist (if the PR title suggests this is intended to be a release)
- name: Release ${{ steps.version.outputs.tag }} already exists
if: github.event_name == 'pull_request' && fromJSON(steps.version.outputs.exists) && startsWith(github.event.pull_request.title, 'release:')
run: |-
echo 'A release already exists for tag ${{ steps.version.outputs.tag }}. Please update to another version.'
exit 1
# If the release does not yet exist, create a draft release targeting this commit.
- name: Create draft release
if: github.event_name == 'push' && steps.version.outputs.exists == 'false'
run: |-
gh release create '${{ steps.version.outputs.tag }}' --draft --generate-notes --target='${{ github.sha }}' --title='${{ steps.version.outputs.tag }}' ${{ contains(steps.version.outputs.tag, '-') && '--prerelease' || '' }}
env:
GH_TOKEN: ${{ github.token }}