build: add dynamic version injection via ldflags and Makefile

- Add cmd.Version variable set at build time via -ldflags
- Fix Dockerfile ldflags path to use cmd.Version instead of main.version
- Add Makefile with auto-detection from git tags (git describe)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
viettranxandClaude Opus 4.6 committed 2026-02-26 10:42:54 +07:00
1 parent 6066adc15a
commit a0ef0c2f91
3 files changed
+22 -2

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ RUN set -eux; \
fi; \
if [ -n "$TAGS" ]; then TAGS="-tags $TAGS"; fi; \
CGO_ENABLED=0 GOOS=linux \
go build -ldflags="-s -w -X main.version=${VERSION}" \
go build -ldflags="-s -w -X github.com/nextlevelbuilder/goclaw/cmd.Version=${VERSION}" \
${TAGS} -o /out/goclaw .
# ── Stage 2: Runtime ──
+17
View File
@@ -0,0 +1,17 @@
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
LDFLAGS = -s -w -X github.com/nextlevelbuilder/goclaw/cmd.Version=$(VERSION)
BINARY = goclaw
.PHONY: build run clean version
build:
CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o $(BINARY) .
run: build
./$(BINARY)
clean:
rm -f $(BINARY)
version:
@echo $(VERSION)
+4 -1
View File
@@ -9,6 +9,9 @@ import (
"github.com/nextlevelbuilder/goclaw/pkg/protocol"
)
// Version is set at build time via -ldflags "-X github.com/nextlevelbuilder/goclaw/cmd.Version=v1.0.0"
var Version = "dev"
var (
cfgFile string
verbose bool
@@ -46,7 +49,7 @@ func versionCmd() *cobra.Command {
Use: "version",
Short: "Print version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("goclaw v0.2.0 (protocol %d)\n", protocol.ProtocolVersion)
fmt.Printf("goclaw %s (protocol %d)\n", Version, protocol.ProtocolVersion)
},
}
}