Skip to content

Commit 2716b2c

Browse files
committed
Update release workflows
1 parent 59822ab commit 2716b2c

3 files changed

Lines changed: 156 additions & 33 deletions

File tree

.github/workflows/release.yaml

Lines changed: 112 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,148 @@ jobs:
1515
steps:
1616
- name: Checkout code
1717
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0 # Fetch all history for changelog generation
1820

1921
- name: Set up Go
20-
uses: actions/setup-go@v4
22+
uses: actions/setup-go@v5
23+
with:
24+
go-version: '1.23'
25+
26+
- name: Cache Go modules
27+
uses: actions/cache@v4
2128
with:
22-
go-version: '1.22' # You can adjust this to your Go version
29+
path: |
30+
~/.cache/go-build
31+
~/go/pkg/mod
32+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
33+
restore-keys: |
34+
${{ runner.os }}-go-
2335
2436
- name: Get tag version
2537
id: get_version
2638
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
2739

28-
- name: Get dependencies
29-
run: go mod tidy
40+
- name: Download dependencies
41+
run: go mod download
42+
43+
- name: Verify dependencies
44+
run: go mod verify
45+
46+
- name: Run tests
47+
run: go test -v -race ./...
48+
49+
- name: Run linting
50+
uses: golangci/golangci-lint-action@v7
51+
with:
52+
version: v2.7.2
53+
args: --timeout=5m
3054

3155
- name: Build binaries
56+
env:
57+
VERSION: ${{ steps.get_version.outputs.VERSION }}
58+
COMMIT: ${{ github.sha }}
3259
run: |
3360
mkdir -p release
34-
61+
62+
# Build flags for version info
63+
LDFLAGS="-s -w -X main.Version=${VERSION} -X main.Commit=${COMMIT:0:8} -X main.BuildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
64+
3565
# Build for Linux
36-
GOOS=linux GOARCH=amd64 go build -o release/workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-amd64 ./
37-
GOOS=linux GOARCH=arm64 go build -o release/workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-arm64 ./
38-
66+
echo "Building for Linux amd64..."
67+
GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o release/workload-exporter-${VERSION}-linux-amd64 ./
68+
echo "Building for Linux arm64..."
69+
GOOS=linux GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o release/workload-exporter-${VERSION}-linux-arm64 ./
70+
3971
# Build for macOS
40-
GOOS=darwin GOARCH=amd64 go build -o release/workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-amd64 ./
41-
GOOS=darwin GOARCH=arm64 go build -o release/workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-arm64 ./
42-
72+
echo "Building for macOS amd64..."
73+
GOOS=darwin GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o release/workload-exporter-${VERSION}-darwin-amd64 ./
74+
echo "Building for macOS arm64..."
75+
GOOS=darwin GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o release/workload-exporter-${VERSION}-darwin-arm64 ./
76+
4377
# Build for Windows
44-
GOOS=windows GOARCH=amd64 go build -o release/workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-amd64.exe ./
45-
GOOS=windows GOARCH=386 go build -o release/workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-386.exe ./
78+
echo "Building for Windows amd64..."
79+
GOOS=windows GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o release/workload-exporter-${VERSION}-windows-amd64.exe ./
80+
echo "Building for Windows 386..."
81+
GOOS=windows GOARCH=386 go build -ldflags="${LDFLAGS}" -o release/workload-exporter-${VERSION}-windows-386.exe ./
4682
4783
- name: Create checksums
84+
working-directory: release
4885
run: |
49-
cd release
50-
for file in *; do
51-
sha256sum "$file" > "${file}.sha256"
52-
done
53-
cd ..
86+
sha256sum * > checksums.txt
87+
cat checksums.txt
5488
5589
- name: Package binaries
90+
env:
91+
VERSION: ${{ steps.get_version.outputs.VERSION }}
92+
working-directory: release
5693
run: |
57-
cd release
58-
5994
# Linux
60-
tar czf workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-amd64.tar.gz workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-amd64 workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-amd64.sha256
61-
tar czf workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-arm64.tar.gz workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-arm64 workload-exporter-${{ steps.get_version.outputs.VERSION }}-linux-arm64.sha256
62-
95+
tar czf workload-exporter-${VERSION}-linux-amd64.tar.gz workload-exporter-${VERSION}-linux-amd64
96+
tar czf workload-exporter-${VERSION}-linux-arm64.tar.gz workload-exporter-${VERSION}-linux-arm64
97+
6398
# macOS
64-
tar czf workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-amd64.tar.gz workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-amd64 workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-amd64.sha256
65-
tar czf workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-arm64.tar.gz workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-arm64 workload-exporter-${{ steps.get_version.outputs.VERSION }}-darwin-arm64.sha256
66-
99+
tar czf workload-exporter-${VERSION}-darwin-amd64.tar.gz workload-exporter-${VERSION}-darwin-amd64
100+
tar czf workload-exporter-${VERSION}-darwin-arm64.tar.gz workload-exporter-${VERSION}-darwin-arm64
101+
67102
# Windows
68-
zip workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-amd64.zip workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-amd64.exe workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-amd64.exe.sha256
69-
zip workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-386.zip workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-386.exe workload-exporter-${{ steps.get_version.outputs.VERSION }}-windows-386.exe.sha256
70-
71-
cd ..
103+
zip workload-exporter-${VERSION}-windows-amd64.zip workload-exporter-${VERSION}-windows-amd64.exe
104+
zip workload-exporter-${VERSION}-windows-386.zip workload-exporter-${VERSION}-windows-386.exe
105+
106+
# Clean up raw binaries (keep only archives)
107+
rm -f workload-exporter-${VERSION}-*-amd64 workload-exporter-${VERSION}-*-arm64
108+
rm -f workload-exporter-${VERSION}-*.exe
109+
110+
# Update checksums with only archives
111+
sha256sum *.tar.gz *.zip > checksums.txt
112+
cat checksums.txt
113+
114+
- name: Generate changelog
115+
id: changelog
116+
run: |
117+
# Get previous tag
118+
PREV_TAG=$(git describe --abbrev=0 --tags $(git rev-list --tags --skip=1 --max-count=1) 2>/dev/null || echo "")
119+
120+
if [ -z "$PREV_TAG" ]; then
121+
echo "First release"
122+
CHANGELOG="Initial release of workload-exporter"
123+
else
124+
echo "Generating changelog from ${PREV_TAG} to ${{ steps.get_version.outputs.VERSION }}"
125+
CHANGELOG=$(git log ${PREV_TAG}..${{ steps.get_version.outputs.VERSION }} --pretty=format:"- %s (%h)" --no-merges)
126+
fi
127+
128+
# Save to file and output
129+
echo "${CHANGELOG}" > CHANGELOG.md
130+
echo "changelog<<EOF" >> $GITHUB_OUTPUT
131+
echo "${CHANGELOG}" >> $GITHUB_OUTPUT
132+
echo "EOF" >> $GITHUB_OUTPUT
72133
73134
- name: Create Release
74-
uses: softprops/action-gh-release@v1
135+
uses: softprops/action-gh-release@v2
75136
with:
76137
name: Release ${{ steps.get_version.outputs.VERSION }}
138+
body: |
139+
## What's Changed
140+
141+
${{ steps.changelog.outputs.changelog }}
142+
143+
## Installation
144+
145+
Download the appropriate binary for your platform from the assets below.
146+
147+
### Verify checksums
148+
```bash
149+
sha256sum -c checksums.txt
150+
```
151+
152+
## Full Changelog
153+
154+
See all commits: https://github.com/${{ github.repository }}/compare/${{ steps.get_version.outputs.VERSION }}...main
77155
draft: false
78-
prerelease: false
156+
prerelease: ${{ contains(steps.get_version.outputs.VERSION, '-rc') || contains(steps.get_version.outputs.VERSION, '-beta') || contains(steps.get_version.outputs.VERSION, '-alpha') }}
79157
files: |
80158
release/*.tar.gz
81159
release/*.zip
160+
release/checksums.txt
82161
env:
83-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
162+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

cmd/version.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Version information - set via ldflags in main package
10+
var (
11+
Version = "dev"
12+
Commit = "unknown"
13+
BuildDate = "unknown"
14+
)
15+
16+
// SetVersionInfo sets the version information from main package
17+
func SetVersionInfo(version, commit, buildDate string) {
18+
Version = version
19+
Commit = commit
20+
BuildDate = buildDate
21+
}
22+
23+
var versionCmd = &cobra.Command{
24+
Use: "version",
25+
Short: "Print version information",
26+
Run: func(cmd *cobra.Command, args []string) {
27+
fmt.Printf("workload-exporter version %s\n", Version)
28+
fmt.Printf("Commit: %s\n", Commit)
29+
fmt.Printf("Built: %s\n", BuildDate)
30+
},
31+
}
32+
33+
func init() {
34+
rootCmd.AddCommand(versionCmd)
35+
}

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ package main
33

44
import "github.com/cockroachlabs/workload-exporter/cmd"
55

6+
// Version information set via ldflags during build
7+
var (
8+
Version = "dev"
9+
Commit = "unknown"
10+
BuildDate = "unknown"
11+
)
12+
613
func main() {
14+
// Pass version info to cmd package
15+
cmd.SetVersionInfo(Version, Commit, BuildDate)
716
cmd.Execute()
817
}

0 commit comments

Comments
 (0)