Skip to content

Commit 33eb761

Browse files
DCodeBotdector
andcommitted
ci: add snapshot prerelease workflow
Co-authored-by: D <code@dector.space>
1 parent ea76061 commit 33eb761

4 files changed

Lines changed: 155 additions & 14 deletions

File tree

.github/workflows/snapshot.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Snapshot
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- CI
7+
types:
8+
- completed
9+
10+
permissions:
11+
actions: read
12+
contents: write
13+
14+
concurrency:
15+
group: snapshot
16+
cancel-in-progress: true
17+
18+
jobs:
19+
publish:
20+
name: Publish snapshot
21+
if: >-
22+
github.event.workflow_run.conclusion == 'success' &&
23+
github.event.workflow_run.event == 'push' &&
24+
github.event.workflow_run.head_branch == 'trunk'
25+
runs-on: ubuntu-latest
26+
steps:
27+
- name: Checkout tested commit
28+
uses: actions/checkout@v4
29+
with:
30+
ref: ${{ github.event.workflow_run.head_sha }}
31+
fetch-depth: 0
32+
33+
- name: Download build artifacts
34+
uses: actions/download-artifact@v4
35+
with:
36+
name: release-artifacts
37+
path: out/release
38+
run-id: ${{ github.event.workflow_run.id }}
39+
github-token: ${{ github.token }}
40+
41+
- name: Publish snapshot prerelease
42+
env:
43+
GH_TOKEN: ${{ github.token }}
44+
SNAPSHOT_SHA: ${{ github.event.workflow_run.head_sha }}
45+
SNAPSHOT_RUN_URL: ${{ github.event.workflow_run.html_url }}
46+
run: |
47+
set -euo pipefail
48+
49+
git config user.name "github-actions[bot]"
50+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
51+
git tag -f snapshot "$SNAPSHOT_SHA"
52+
git push --force origin refs/tags/snapshot
53+
54+
cat > release-notes.md <<EOF
55+
Snapshot build for \`$SNAPSHOT_SHA\`.
56+
57+
CI run: $SNAPSHOT_RUN_URL
58+
EOF
59+
60+
if gh release view snapshot >/dev/null 2>&1; then
61+
gh release edit snapshot \
62+
--title snapshot \
63+
--notes-file release-notes.md \
64+
--prerelease \
65+
--latest=false
66+
else
67+
gh release create snapshot \
68+
--title snapshot \
69+
--notes-file release-notes.md \
70+
--prerelease \
71+
--latest=false \
72+
--target "$SNAPSHOT_SHA"
73+
fi
74+
75+
gh release view snapshot --json assets --jq '.assets[].name' |
76+
xargs -r -n 1 gh release delete-asset snapshot -y
77+
78+
gh release upload snapshot out/release/* --clobber

.github/workflows/test.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
test-build:
12+
name: Test and build
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Set up Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version-file: go.mod
24+
25+
- name: Set up Task
26+
uses: arduino/setup-task@v2
27+
28+
- name: Compute CI version
29+
run: |
30+
base_version="$(awk -F'"' '/^version:/ { print $2 }' xx.yml)"
31+
echo "VERSION=${base_version}.${GITHUB_SHA::7}" >> "$GITHUB_ENV"
32+
33+
- name: Run tests
34+
run: task ci:test
35+
36+
- name: Build all binaries
37+
run: task ci:build
38+
39+
- name: Upload build artifacts
40+
if: github.event_name == 'push' && github.ref == 'refs/heads/trunk'
41+
uses: actions/upload-artifact@v4
42+
with:
43+
name: release-artifacts
44+
path: |
45+
out/release/checksums.txt
46+
out/release/serv-*

Taskfile.yml

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,34 @@ tasks:
1010
cmds:
1111
- amber run tools/mark-release
1212

13+
ci:test:
14+
cmds:
15+
- go test ./...
16+
17+
ci:build:
18+
env:
19+
CGO_ENABLED: 0
20+
cmds:
21+
- |
22+
test -n "${VERSION:-}" || (echo "VERSION is required" >&2; exit 1)
23+
rm -rf ./out/release
24+
mkdir -p ./out/release
25+
LDFLAGS="-X main.versionOverride=$VERSION"
26+
GOOS=linux GOARCH=amd64 go build -ldflags "$LDFLAGS" -o out/release/serv-$VERSION-linux-amd64 ./
27+
GOOS=linux GOARCH=arm64 go build -ldflags "$LDFLAGS" -o out/release/serv-$VERSION-linux-arm64 ./
28+
GOOS=darwin GOARCH=amd64 go build -ldflags "$LDFLAGS" -o out/release/serv-$VERSION-darwin-amd64 ./
29+
GOOS=darwin GOARCH=arm64 go build -ldflags "$LDFLAGS" -o out/release/serv-$VERSION-darwin-arm64 ./
30+
GOOS=windows GOARCH=amd64 go build -ldflags "$LDFLAGS" -o out/release/serv-$VERSION-windows-amd64.exe ./
31+
GOOS=windows GOARCH=arm64 go build -ldflags "$LDFLAGS" -o out/release/serv-$VERSION-windows-arm64.exe ./
32+
sha256sum ./out/release/serv-* > ./out/release/checksums.txt
33+
1334
build:all:
14-
# deps:
15-
# - gen
1635
env:
1736
CGO_ENABLED: 0
1837
cmds:
19-
- rm -r ./out/release || true
20-
- SUFFIX=$(date +%s%3N | shasum | head -c 8)
21-
- go build -o /tmp/serv-$SUFFIX ./
2238
- |
23-
VERSION=$(/tmp/serv-$SUFFIX --version)
24-
rm /tmp/serv-$SUFFIX
25-
GOOS=linux GOARCH=amd64 go build -o out/release/serv-$VERSION-linux-amd64 ./
26-
GOOS=linux GOARCH=arm64 go build -o out/release/serv-$VERSION-linux-arm64 ./
27-
GOOS=darwin GOARCH=amd64 go build -o out/release/serv-$VERSION-darwin-amd64 ./
28-
GOOS=darwin GOARCH=arm64 go build -o out/release/serv-$VERSION-darwin-arm64 ./
29-
GOOS=windows GOARCH=amd64 go build -o out/release/serv-$VERSION-windows-amd64.exe ./
30-
GOOS=windows GOARCH=arm64 go build -o out/release/serv-$VERSION-windows-arm64.exe ./
31-
- sha256sum ./out/release/serv-* > ./out/release/checksums.txt
39+
tmp_binary="$(mktemp -t serv.XXXXXX)"
40+
go build -o "$tmp_binary" ./
41+
VERSION="$($tmp_binary --version)"
42+
rm "$tmp_binary"
43+
VERSION="$VERSION" task ci:build

meta.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ func (self *Globals) Init() {
2020
//go:embed xx.yml
2121
var metaYAML string
2222

23+
var versionOverride string
24+
2325
const DefaultPort = 8080
2426

2527
func getVersion() string {
28+
if version := strings.TrimSpace(versionOverride); version != "" {
29+
return version
30+
}
2631
var meta struct {
2732
Version string `yaml:"version"`
2833
}

0 commit comments

Comments
 (0)