Skip to content

Commit 11785df

Browse files
dentargclaude
andauthored
Add cloudamqp version command (#18)
-v and --version supported too Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0d14e52 commit 11785df

7 files changed

Lines changed: 389 additions & 10 deletions

File tree

.github/workflows/README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# GitHub Actions Workflows
2+
3+
This directory contains the CI/CD workflows for the CloudAMQP CLI.
4+
5+
## Workflows
6+
7+
### CI Workflow (`ci.yml`)
8+
9+
Runs on every push to `main` or `develop` branches, and on pull requests.
10+
11+
**What it does:**
12+
- Tests with multiple Go versions (1.21, 1.22, 1.23)
13+
- Runs format checks, vet, and tests
14+
- Builds the binary with version information
15+
- Uploads test coverage to Codecov
16+
- Verifies the binary works with `--help` and `version` commands
17+
18+
**Version Information:**
19+
The CI build automatically includes:
20+
- Version: From `git describe --tags --always --dirty` (or "dev" as fallback)
21+
- Build Date: Current date in UTC (YYYY-MM-DD)
22+
- Git Commit: Short commit hash
23+
24+
### Release Workflow (`release.yml`)
25+
26+
Triggers when a tag starting with `v` is pushed (e.g., `v1.0.0`), or can be run manually.
27+
28+
**What it does:**
29+
1. **Build Job**: Builds cross-platform binaries for:
30+
- Linux (amd64, arm64)
31+
- macOS (amd64, arm64)
32+
- Windows (amd64, arm64)
33+
34+
2. **Package Job**: Creates release archives:
35+
- `.tar.gz` for Linux/macOS binaries
36+
- `.zip` for Windows binaries
37+
- Creates GitHub Release with all artifacts
38+
39+
**Version Information:**
40+
Release builds automatically include:
41+
- Version: From the git tag (e.g., `v1.0.0`)
42+
- Build Date: Current date in UTC (YYYY-MM-DD)
43+
- Git Commit: Short commit hash
44+
45+
The version info is embedded using Go's `-ldflags`:
46+
```bash
47+
-ldflags="-w -s -X cloudamqp-cli/cmd.Version=$VERSION -X cloudamqp-cli/cmd.BuildDate=$BUILD_DATE -X cloudamqp-cli/cmd.GitCommit=$GIT_COMMIT"
48+
```
49+
50+
## Creating a Release
51+
52+
To create a new release:
53+
54+
1. **Create and push a tag:**
55+
```bash
56+
git tag v1.0.0
57+
git push origin v1.0.0
58+
```
59+
60+
2. The release workflow will automatically:
61+
- Build binaries for all platforms
62+
- Create archives
63+
- Create a GitHub release with auto-generated release notes
64+
- Upload all artifacts
65+
66+
3. The released binaries will show proper version info:
67+
```bash
68+
$ cloudamqp version
69+
cloudamqp version v1.0.0 (2025-11-26)
70+
https://github.com/cloudamqp/cli/releases/tag/v1.0.0
71+
```
72+
73+
## Manual Workflow Trigger
74+
75+
The release workflow can also be triggered manually from the GitHub Actions UI:
76+
1. Go to Actions tab
77+
2. Select "Build Release" workflow
78+
3. Click "Run workflow"
79+
4. Select branch and click "Run workflow"
80+
81+
## Verification
82+
83+
Both workflows include verification steps:
84+
- **CI**: Tests the built binary with `--help` and `version` commands
85+
- **Release**: Attempts to run `version` command on non-Windows binaries (may skip for cross-compiled binaries)
86+
87+
## Caching
88+
89+
Both workflows use Go module caching to speed up builds:
90+
- Cache key includes Go version and `go.sum` hash
91+
- Cached paths: `~/.cache/go-build` and `~/go/pkg/mod`
92+
93+
## Build Flags
94+
95+
**CI builds:**
96+
- Standard build flags
97+
- Version information included
98+
99+
**Release builds:**
100+
- `-a`: Force rebuilding of packages
101+
- `-installsuffix cgo`: Use suffix for cgo-enabled builds
102+
- `-ldflags="-w -s"`: Strip debug info and symbol tables (smaller binaries)
103+
- Version information via `-X` flags
104+
- `CGO_ENABLED=0`: Disable cgo for static binaries
105+
106+
## Artifacts
107+
108+
**CI Workflow:**
109+
- Uploads test coverage to Codecov
110+
111+
**Release Workflow:**
112+
- Individual binaries (30 days retention)
113+
- Release archives (90 days retention)
114+
- GitHub Release assets (permanent)
115+
116+
## Troubleshooting
117+
118+
If version information is not showing correctly in releases:
119+
120+
1. Check that tags are pushed: `git push origin --tags`
121+
2. Verify tag format: Must start with `v` (e.g., `v1.0.0`)
122+
3. Check workflow logs for version extraction step
123+
4. Ensure no local modifications (avoid `-dirty` in version)
124+
125+
## Local Testing
126+
127+
To test version embedding locally:
128+
129+
```bash
130+
# Using Make (recommended)
131+
make build
132+
133+
# Manual
134+
VERSION=v1.0.0
135+
BUILD_DATE=$(date -u +"%Y-%m-%d")
136+
GIT_COMMIT=$(git rev-parse --short HEAD)
137+
138+
go build -ldflags "\
139+
-X cloudamqp-cli/cmd.Version=$VERSION \
140+
-X cloudamqp-cli/cmd.BuildDate=$BUILD_DATE \
141+
-X cloudamqp-cli/cmd.GitCommit=$GIT_COMMIT" \
142+
-o cloudamqp .
143+
```

.github/workflows/ci.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,24 @@ jobs:
5353
with:
5454
file: ./coverage.out
5555

56+
- name: Extract version information
57+
id: version
58+
run: |
59+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
60+
BUILD_DATE=$(date -u +"%Y-%m-%d")
61+
GIT_COMMIT=$(git rev-parse --short HEAD)
62+
echo "version=$VERSION" >> $GITHUB_OUTPUT
63+
echo "build_date=$BUILD_DATE" >> $GITHUB_OUTPUT
64+
echo "git_commit=$GIT_COMMIT" >> $GITHUB_OUTPUT
65+
echo "Building with version: $VERSION (commit: $GIT_COMMIT, date: $BUILD_DATE)"
66+
5667
- name: Build
57-
run: go build -v -o cloudamqp .
68+
run: |
69+
go build -v \
70+
-ldflags="-X cloudamqp-cli/cmd.Version=${{ steps.version.outputs.version }} -X cloudamqp-cli/cmd.BuildDate=${{ steps.version.outputs.build_date }} -X cloudamqp-cli/cmd.GitCommit=${{ steps.version.outputs.git_commit }}" \
71+
-o cloudamqp .
5872
5973
- name: Test binary
60-
run: ./cloudamqp --help
74+
run: |
75+
./cloudamqp --help
76+
./cloudamqp version

.github/workflows/release.yml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,42 @@ jobs:
5555
- name: Download dependencies
5656
run: go mod download
5757

58+
- name: Extract version information
59+
id: version
60+
run: |
61+
# Extract version from tag (remove 'v' prefix if present)
62+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
63+
VERSION="${GITHUB_REF#refs/tags/}"
64+
else
65+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
66+
fi
67+
echo "version=$VERSION" >> $GITHUB_OUTPUT
68+
69+
# Get build date in UTC
70+
BUILD_DATE=$(date -u +"%Y-%m-%d")
71+
echo "build_date=$BUILD_DATE" >> $GITHUB_OUTPUT
72+
73+
# Get short commit hash
74+
GIT_COMMIT=$(git rev-parse --short HEAD)
75+
echo "git_commit=$GIT_COMMIT" >> $GITHUB_OUTPUT
76+
77+
echo "Building version: $VERSION (commit: $GIT_COMMIT, date: $BUILD_DATE)"
78+
5879
- name: Build binary
5980
env:
6081
GOOS: ${{ matrix.goos }}
6182
GOARCH: ${{ matrix.goarch }}
6283
CGO_ENABLED: 0
6384
run: |
64-
go build -a -installsuffix cgo -ldflags="-w -s" -o cloudamqp-${{ matrix.name }}${{ matrix.ext }} .
85+
go build -a -installsuffix cgo \
86+
-ldflags="-w -s -X cloudamqp-cli/cmd.Version=${{ steps.version.outputs.version }} -X cloudamqp-cli/cmd.BuildDate=${{ steps.version.outputs.build_date }} -X cloudamqp-cli/cmd.GitCommit=${{ steps.version.outputs.git_commit }}" \
87+
-o cloudamqp-${{ matrix.name }}${{ matrix.ext }} .
88+
89+
- name: Verify version (Linux/macOS only)
90+
if: matrix.goos != 'windows'
91+
run: |
92+
chmod +x cloudamqp-${{ matrix.name }}${{ matrix.ext }}
93+
./cloudamqp-${{ matrix.name }}${{ matrix.ext }} version || echo "Version verification skipped for cross-compiled binary"
6594
6695
- name: Upload artifact
6796
uses: actions/upload-artifact@v4

BUILD_VERSION.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Building with Version Information
2+
3+
The `cloudamqp` CLI supports version information that can be set at build time using Go's `-ldflags`.
4+
5+
## Version Variables
6+
7+
The following variables are available in `cmd/version.go`:
8+
- `Version` - The version number (e.g., "1.0.0")
9+
- `BuildDate` - The build date (e.g., "2025-11-25")
10+
- `GitCommit` - The git commit hash (optional)
11+
12+
## Build Examples
13+
14+
### Using Make (Recommended)
15+
16+
The Makefile automatically extracts version information from git:
17+
18+
```bash
19+
# Build with automatic git version
20+
make build
21+
```
22+
23+
Output:
24+
```
25+
$ ./cloudamqp version
26+
cloudamqp version v0.1.0-8-g285bd2b (2025-11-26)
27+
https://github.com/cloudamqp/cli/releases/tag/v0.1.0-8-g285bd2b
28+
```
29+
30+
Show version information before building:
31+
```bash
32+
make version-info
33+
```
34+
35+
Override version manually:
36+
```bash
37+
make build VERSION=1.0.0 BUILD_DATE=2025-11-25
38+
```
39+
40+
### Manual Build (without Make)
41+
42+
#### Development Build
43+
```bash
44+
go build -o cloudamqp-cli
45+
```
46+
Output:
47+
```
48+
$ cloudamqp-cli version
49+
cloudamqp version dev (development build)
50+
```
51+
52+
#### Release Build
53+
```bash
54+
go build -ldflags "\
55+
-X cloudamqp-cli/cmd.Version=1.0.0 \
56+
-X cloudamqp-cli/cmd.BuildDate=2025-11-25 \
57+
-X cloudamqp-cli/cmd.GitCommit=abc123" \
58+
-o cloudamqp-cli
59+
```
60+
Output:
61+
```
62+
$ cloudamqp-cli version
63+
cloudamqp version 1.0.0 (2025-11-25)
64+
https://github.com/cloudamqp/cli/releases/tag/v1.0.0
65+
```
66+
67+
### Using Git Information
68+
```bash
69+
VERSION=$(git describe --tags --always --dirty)
70+
BUILD_DATE=$(date -u +"%Y-%m-%d")
71+
GIT_COMMIT=$(git rev-parse --short HEAD)
72+
73+
go build -ldflags "\
74+
-X cloudamqp-cli/cmd.Version=${VERSION} \
75+
-X cloudamqp-cli/cmd.BuildDate=${BUILD_DATE} \
76+
-X cloudamqp-cli/cmd.GitCommit=${GIT_COMMIT}" \
77+
-o cloudamqp-cli
78+
```
79+
80+
## Usage
81+
82+
The version can be displayed in two ways:
83+
84+
```bash
85+
# Using the version command
86+
cloudamqp version
87+
88+
# Using the --version or -v flag
89+
cloudamqp --version
90+
cloudamqp -v
91+
```
92+
93+
Both produce identical output, similar to GitHub CLI (`gh`).
94+
95+
## CI/CD Integration
96+
97+
### GitHub Actions Example
98+
```yaml
99+
- name: Build
100+
run: |
101+
VERSION=${GITHUB_REF#refs/tags/}
102+
BUILD_DATE=$(date -u +"%Y-%m-%d")
103+
GIT_COMMIT=${GITHUB_SHA::7}
104+
105+
go build -ldflags "\
106+
-X cloudamqp-cli/cmd.Version=${VERSION} \
107+
-X cloudamqp-cli/cmd.BuildDate=${BUILD_DATE} \
108+
-X cloudamqp-cli/cmd.GitCommit=${GIT_COMMIT}" \
109+
-o cloudamqp-cli
110+
```
111+
112+
### GoReleaser Example
113+
```yaml
114+
builds:
115+
- ldflags:
116+
- -X cloudamqp-cli/cmd.Version={{.Version}}
117+
- -X cloudamqp-cli/cmd.BuildDate={{.Date}}
118+
- -X cloudamqp-cli/cmd.GitCommit={{.ShortCommit}}
119+
```

0 commit comments

Comments
 (0)