Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 843beef

Browse files
committed
feat: add dynamic versioning
1 parent 0627c58 commit 843beef

File tree

5 files changed

+68
-32
lines changed

5 files changed

+68
-32
lines changed

.github/workflows/build-release.yml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,31 @@ jobs:
193193
- name: Download dependencies
194194
run: go mod download
195195

196+
- name: Set version
197+
id: version
198+
run: |
199+
if [[ "${{ github.ref_type }}" == "tag" ]]; then
200+
VERSION=${GITHUB_REF_NAME#v}
201+
elif [ -f .version ]; then
202+
VERSION=$(cat .version)
203+
else
204+
VERSION="dev-${GITHUB_SHA::8}"
205+
fi
206+
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
207+
196208
- name: Build binary
197209
env:
198210
GOOS: ${{ matrix.goos }}
199211
GOARCH: ${{ matrix.goarch }}
200212
CGO_ENABLED: 0
201213
run: |
202214
mkdir -p bin
215+
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
203216
204-
# Set version info
205-
VERSION=${GITHUB_REF#refs/tags/}
206-
if [[ ! "$VERSION" =~ ^v[0-9] ]]; then
207-
VERSION="dev-${GITHUB_SHA::8}"
208-
fi
209-
210-
go build -v -ldflags="-w -s -X main.version=${VERSION} -X main.commit=${GITHUB_SHA}" \
217+
go build -v -ldflags="-w -s \
218+
-X github.com/ofkm/arcane-agent/internal/version.Version=${{ steps.version.outputs.VERSION }} \
219+
-X github.com/ofkm/arcane-agent/internal/version.Commit=${GITHUB_SHA} \
220+
-X github.com/ofkm/arcane-agent/internal/version.Date=${BUILD_DATE}" \
211221
-o bin/${{ env.BINARY_NAME }}-${{ matrix.platform }}${{ matrix.extension }} \
212222
./cmd/agent
213223
@@ -250,6 +260,7 @@ jobs:
250260
type=semver,pattern={{major}}.{{minor}}
251261
type=semver,pattern={{major}}
252262
type=sha
263+
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
253264
254265
- name: Build and push Docker image
255266
uses: docker/build-push-action@v6

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.0
1+
1.2.0

Makefile

Lines changed: 0 additions & 23 deletions
This file was deleted.

internal/agent/http_client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/ofkm/arcane-agent/internal/config"
1818
"github.com/ofkm/arcane-agent/internal/tasks"
19+
"github.com/ofkm/arcane-agent/internal/version"
1920
"github.com/ofkm/arcane-agent/pkg/types"
2021
)
2122

@@ -80,7 +81,7 @@ func (h *HTTPClient) registerAgent() error {
8081
"hostname": hostname,
8182
"platform": runtime.GOOS,
8283
"arch": runtime.GOARCH,
83-
"version": "1.1.1",
84+
"version": version.GetVersion(),
8485
"capabilities": []string{"docker", "compose"},
8586
}
8687

internal/version/version.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package version
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"runtime"
7+
"strings"
8+
)
9+
10+
// Build-time variables (set via ldflags)
11+
var (
12+
Version = "dev"
13+
Commit = "unknown"
14+
Date = "unknown"
15+
)
16+
17+
// GetVersion returns the version, preferring build-time version over version file
18+
func GetVersion() string {
19+
if Version != "dev" {
20+
return Version
21+
}
22+
23+
// Try to read from .version file in project root
24+
_, filename, _, ok := runtime.Caller(0)
25+
if !ok {
26+
return "dev"
27+
}
28+
29+
// Go up to project root from internal/version/version.go
30+
projectRoot := filepath.Dir(filepath.Dir(filepath.Dir(filename)))
31+
versionFile := filepath.Join(projectRoot, ".version")
32+
33+
if data, err := os.ReadFile(versionFile); err == nil {
34+
return strings.TrimSpace(string(data))
35+
}
36+
37+
return "dev"
38+
}
39+
40+
// GetFullVersion returns version with commit and date info
41+
func GetFullVersion() string {
42+
version := GetVersion()
43+
if Commit != "unknown" {
44+
version += "+" + Commit
45+
}
46+
return version
47+
}

0 commit comments

Comments
 (0)