-
Notifications
You must be signed in to change notification settings - Fork 4
342 lines (294 loc) · 9.91 KB
/
Copy pathrelease.yml
File metadata and controls
342 lines (294 loc) · 9.91 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
name: Release
on:
workflow_run:
workflows: ["CI"]
branches: [main]
types: [completed]
workflow_dispatch:
# Prevent concurrent releases
concurrency:
group: release
cancel-in-progress: false
jobs:
check:
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.check.outputs.should_release }}
next_version: ${{ steps.version.outputs.next_version }}
changelog: ${{ steps.changelog.outputs.changelog }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if release needed
id: check
run: |
# Skip if the last commit is a release commit (avoid infinite loop)
LAST_MSG=$(git log -1 --pretty=%s)
if [[ "$LAST_MSG" == chore:\ release* ]]; then
echo "should_release=false" >> $GITHUB_OUTPUT
exit 0
fi
# Skip if no meaningful changes since last tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
echo "should_release=true" >> $GITHUB_OUTPUT
exit 0
fi
# Check for feat/fix/refactor commits since last tag
COMMITS=$(git log ${LAST_TAG}..HEAD --pretty=%s)
if echo "$COMMITS" | grep -qE '^(feat|fix|refactor|perf|breaking)'; then
echo "should_release=true" >> $GITHUB_OUTPUT
else
echo "should_release=false" >> $GITHUB_OUTPUT
fi
- name: Determine next version
if: steps.check.outputs.should_release == 'true'
id: version
run: |
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "Last tag: $LAST_TAG"
# Parse semver
VERSION=${LAST_TAG#v}
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
# Determine bump type from commits
COMMITS=$(git log ${LAST_TAG}..HEAD --pretty=%s 2>/dev/null || git log --pretty=%s)
if echo "$COMMITS" | grep -qiE '^breaking|BREAKING CHANGE'; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif echo "$COMMITS" | grep -qE '^feat'; then
MINOR=$((MINOR + 1))
PATCH=0
else
PATCH=$((PATCH + 1))
fi
NEXT="v${MAJOR}.${MINOR}.${PATCH}"
echo "Next version: $NEXT"
echo "next_version=$NEXT" >> $GITHUB_OUTPUT
- name: Generate changelog
if: steps.check.outputs.should_release == 'true'
id: changelog
run: |
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
RANGE="HEAD"
else
RANGE="${LAST_TAG}..HEAD"
fi
{
echo 'changelog<<CHANGELOG_EOF'
# Features
FEATS=$(git log $RANGE --pretty="- %s (%h)" --grep="^feat" --extended-regexp 2>/dev/null)
if [ -n "$FEATS" ]; then
echo "### Features"
echo "$FEATS"
echo ""
fi
# Fixes
FIXES=$(git log $RANGE --pretty="- %s (%h)" --grep="^fix" --extended-regexp 2>/dev/null)
if [ -n "$FIXES" ]; then
echo "### Bug Fixes"
echo "$FIXES"
echo ""
fi
# Other changes (refactor, perf, etc.)
OTHERS=$(git log $RANGE --pretty="- %s (%h)" --grep="^refactor|^perf|^chore|^ci|^docs|^test|^build" --extended-regexp 2>/dev/null)
if [ -n "$OTHERS" ]; then
echo "### Other Changes"
echo "$OTHERS"
echo ""
fi
echo "### Platforms"
echo "- Linux (amd64, arm64, arm, 386, mips, mipsle, mips64, mips64le, ppc64le, s390x, riscv64)"
echo "- macOS (amd64, arm64)"
echo "- Windows (amd64, arm64, 386)"
echo "- FreeBSD (amd64, arm64)"
echo "- OpenBSD (amd64)"
echo "- NetBSD (amd64)"
echo 'CHANGELOG_EOF'
} >> $GITHUB_OUTPUT
build-cross:
needs: check
if: needs.check.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.24.x'
cache: false
- name: Install dependencies
run: go mod download
- name: Build all non-darwin targets
env:
CGO_ENABLED: '0'
run: |
VERSION=${{ needs.check.outputs.next_version }}
COMMIT=$(git rev-parse --short HEAD)
DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS="-s -w -X codes/internal/commands.Version=${VERSION} -X codes/internal/commands.Commit=${COMMIT} -X codes/internal/commands.Date=${DATE}"
# Define targets: GOOS:GOARCH[:extra_env]
TARGETS=(
"linux:amd64"
"linux:arm64"
"linux:arm:GOARM=6"
"linux:386"
"linux:mips:GOMIPS=softfloat"
"linux:mipsle:GOMIPS=softfloat"
"linux:mips64"
"linux:mips64le"
"linux:ppc64le"
"linux:s390x"
"linux:riscv64"
"windows:amd64"
"windows:arm64"
"windows:386"
"freebsd:amd64"
"freebsd:arm64"
"openbsd:amd64"
"netbsd:amd64"
)
for target in "${TARGETS[@]}"; do
IFS=':' read -r os arch extra <<< "$target"
echo "==> Building ${os}/${arch} ${extra:+($extra)}"
# Set extra env vars if any
EXTRA_ENV=""
if [ -n "$extra" ]; then
export ${extra}
EXTRA_ENV="$extra"
fi
OUTPUT="codes"
if [ "$os" = "windows" ]; then
OUTPUT="codes.exe"
fi
GOOS=$os GOARCH=$arch go build -ldflags "$LDFLAGS" -o "dist/codes-${os}-${arch}/${OUTPUT}" ./cmd/codes
# Unset extra env vars
if [ -n "$EXTRA_ENV" ]; then
unset ${EXTRA_ENV%%=*}
fi
echo " OK: dist/codes-${os}-${arch}/${OUTPUT}"
done
echo ""
echo "==> All builds completed:"
ls -la dist/*/
- name: Smoke test linux-amd64
run: |
chmod +x dist/codes-linux-amd64/codes
dist/codes-linux-amd64/codes version
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: cross-binaries
path: dist/
retention-days: 7
build-darwin:
needs: check
if: needs.check.outputs.should_release == 'true'
runs-on: macos-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.24.x'
cache: false
- name: Install dependencies
run: go mod download
- name: Build darwin targets
env:
CGO_ENABLED: '0'
run: |
VERSION=${{ needs.check.outputs.next_version }}
COMMIT=$(git rev-parse --short HEAD)
DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS="-s -w -X codes/internal/commands.Version=${VERSION} -X codes/internal/commands.Commit=${COMMIT} -X codes/internal/commands.Date=${DATE}"
for arch in amd64 arm64; do
echo "==> Building darwin/${arch}"
GOOS=darwin GOARCH=$arch go build -ldflags "$LDFLAGS" -o "dist/codes-darwin-${arch}/codes" ./cmd/codes
echo " OK"
done
- name: Sign macOS binaries
run: |
for arch in amd64 arm64; do
echo "==> Signing darwin/${arch}"
codesign --force --sign - "dist/codes-darwin-${arch}/codes"
done
- name: Smoke test native binary
run: |
# macos-latest is arm64, test that binary
chmod +x dist/codes-darwin-arm64/codes
dist/codes-darwin-arm64/codes version
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: darwin-binaries
path: dist/
retention-days: 7
release:
needs: [check, build-cross, build-darwin]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download cross-compiled binaries
uses: actions/download-artifact@v4
with:
name: cross-binaries
path: dist
- name: Download darwin binaries
uses: actions/download-artifact@v4
with:
name: darwin-binaries
path: dist
- name: Package release archives
run: |
VERSION=${{ needs.check.outputs.next_version }}
mkdir -p release
for dir in dist/codes-*; do
[ -d "$dir" ] || continue
target=$(basename "$dir" | sed 's/codes-//')
os=$(echo "$target" | cut -d- -f1)
arch=$(echo "$target" | cut -d- -f2)
if [ "$os" = "windows" ]; then
# ZIP for Windows
ARCHIVE="codes-${VERSION}-${os}-${arch}.zip"
echo "==> Packaging ${ARCHIVE}"
(cd "$dir" && zip -q "${OLDPWD}/release/${ARCHIVE}" codes.exe)
else
# tar.gz for Unix
ARCHIVE="codes-${VERSION}-${os}-${arch}.tar.gz"
echo "==> Packaging ${ARCHIVE}"
chmod +x "$dir/codes"
tar -czf "release/${ARCHIVE}" -C "$dir" codes
fi
done
echo ""
echo "==> All archives:"
ls -lh release/
- name: Generate checksums
run: |
VERSION=${{ needs.check.outputs.next_version }}
cd release
sha256sum codes-${VERSION}-* > "codes-${VERSION}-checksums.txt"
echo "==> Checksums:"
cat "codes-${VERSION}-checksums.txt"
- name: Create release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.check.outputs.next_version }}
name: ${{ needs.check.outputs.next_version }}
body: ${{ needs.check.outputs.changelog }}
files: release/*
draft: false
prerelease: false
make_latest: true