forked from scylladb/argus
-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (161 loc) · 7.21 KB
/
cli-release.yml
File metadata and controls
186 lines (161 loc) · 7.21 KB
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
name: CLI – Release
# Trigger: push a tag of the form cli/vX.Y.Z
# Examples:
# git tag cli/v1.0.0 && git push origin cli/v1.0.0
on:
push:
tags:
# Require a numeric version immediately after the prefix so that
# accidental tags like cli/vtest or cli/vfoo never trigger a release.
- "cli/v[0-9]*"
# Guard: only proceed when the tag is reachable from master/main.
# This prevents a cli/v* tag pushed on a feature branch from publishing a release.
# The check runs as the very first step in each job via job-level `if`.
# github.event.base_ref is the branch the tag points to when pushed via
# `git push origin <tag>`; for annotated tags it may be empty, so we also
# perform an explicit ancestry check inside the changelog job.
# Least-privilege: default to read-only; the release job escalates below.
permissions:
contents: read
jobs:
# -------------------------------------------------------------------------
# 1. Generate the CLI-only changelog
# -------------------------------------------------------------------------
changelog:
name: Generate Changelog
runs-on: ubuntu-latest
outputs:
prev_tag: ${{ steps.prev_tag.outputs.tag }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Verify tag is on master/main
shell: bash
run: |
# Abort if the tagged commit is not an ancestor of master (or main).
# This prevents a cli/v* tag pushed on a feature branch from
# accidentally publishing a release.
TAG_SHA=$(git rev-list -n1 "${GITHUB_REF_NAME}")
for branch in master main; do
if git show-ref --verify --quiet "refs/remotes/origin/${branch}"; then
if git merge-base --is-ancestor "${TAG_SHA}" "origin/${branch}"; then
echo "Tag ${GITHUB_REF_NAME} (${TAG_SHA}) is on ${branch}. Proceeding."
exit 0
fi
fi
done
echo "ERROR: Tag ${GITHUB_REF_NAME} is not reachable from master/main. Aborting release." >&2
exit 1
- name: Resolve previous CLI tag
id: prev_tag
shell: bash
run: |
CURRENT_TAG="${GITHUB_REF_NAME}" # e.g. cli/v1.2.0
# List cli/v[0-9]* tags reachable from HEAD, sorted ascending by
# git's own version comparator (version:refname). This correctly
# orders pre-releases: cli/v1.0.0-rc1 < cli/v1.0.0, unlike sort -V
# which can produce the wrong result for pre-release suffixes.
# Exclude the current tag then take the last entry = immediate predecessor.
PREV=$(git tag --merged HEAD --list 'cli/v[0-9]*' --sort=version:refname \
| grep -v "^${CURRENT_TAG}$" \
| tail -1)
if [[ -z "$PREV" ]]; then
echo "No previous CLI tag found – treating this as the first release."
PREV="FIRST"
fi
echo "Previous tag: ${PREV}"
echo "tag=${PREV}" >> "$GITHUB_OUTPUT"
- name: Generate changelog
id: gen
shell: bash
run: |
NOTES_FILE="${RUNNER_TEMP}/cli-changelog.md"
PREV_TAG="${{ steps.prev_tag.outputs.tag }}"
CURRENT_TAG="${GITHUB_REF_NAME}"
bash cli/scripts/generate-changelog.sh "${PREV_TAG}" "${CURRENT_TAG}" \
> "${NOTES_FILE}"
echo "--- Generated changelog ---"
cat "${NOTES_FILE}"
echo "---"
echo "notes_file=${NOTES_FILE}" >> "$GITHUB_OUTPUT"
- name: Upload changelog artifact
uses: actions/upload-artifact@v4
with:
name: cli-changelog
path: ${{ steps.gen.outputs.notes_file }}
retention-days: 1
# -------------------------------------------------------------------------
# 2. Run tests before cutting a release
# -------------------------------------------------------------------------
test:
name: Test
runs-on: ubuntu-latest
defaults:
run:
working-directory: cli
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: cli/go.mod
cache: true
cache-dependency-path: cli/go.sum
- name: Install gotestfmt
uses: gotesttools/gotestfmt-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Run tests
run: |
set -euo pipefail
go test -race -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
# -------------------------------------------------------------------------
# 3. GoReleaser – build binaries and publish the GitHub Release
# -------------------------------------------------------------------------
release:
name: Release
runs-on: ubuntu-latest
needs: [changelog, test]
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version-file: cli/go.mod
cache: true
cache-dependency-path: cli/go.sum
- name: Download changelog artifact
uses: actions/download-artifact@v4
with:
name: cli-changelog
path: ${{ runner.temp }}
# GoReleaser uses the tag as-is for the version.
# cli/v1.2.0 → strip the "cli/" prefix so archives are named v1.2.0.
- name: Extract semver from tag
id: semver
run: |
TAG="${GITHUB_REF_NAME}" # cli/v1.2.0
VERSION="${TAG#cli/}" # v1.2.0
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser
version: "~> v2"
# Config lives inside cli/ but GoReleaser runs from the repo root
# so that git commands resolve correctly.
args: >-
release
--config cli/.goreleaser.yml
--release-notes "${{ runner.temp }}/cli-changelog.md"
--clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Tell GoReleaser to treat the stripped semver as the current tag so
# it doesn't choke on the "cli/" prefix.
GORELEASER_CURRENT_TAG: ${{ steps.semver.outputs.version }}