From 93be9949fc77edfad501125b29e5d4223c894ec9 Mon Sep 17 00:00:00 2001 From: lufreita Date: Wed, 9 Sep 2026 18:26:56 -0300 Subject: [PATCH] ROSAENG-66692 | feat: inject DefaultVersion from git tag at build time Change DefaultVersion from const to var so it can be overridden via -ldflags at build time. Add -X DefaultVersion= to every build entry point: Makefile, hack/build_cli.sh, and .goreleaser.yaml. Tagged release builds now always report the correct version without requiring a manual source edit. Local builds without a tag fall back to the value checked into pkg/info/info.go. Signed-off-by: Lucas Freitas Signed-off-by: lufreita --- .goreleaser.yaml | 1 + Makefile | 8 ++++++-- hack/build_cli.sh | 7 ++++++- pkg/info/info.go | 7 +++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 595604379a..772ef4adef 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,6 +10,7 @@ builds: - CGO_ENABLED=0 ldflags: - -X github.com/openshift/rosa/pkg/info.Build={{ .ShortCommit }} + - -X github.com/openshift/rosa/pkg/info.DefaultVersion={{ .Version }} goos: - linux - windows diff --git a/Makefile b/Makefile index 523864ede3..a75af90a7a 100644 --- a/Makefile +++ b/Makefile @@ -37,9 +37,13 @@ export CGO_ENABLED=0 # Unset GOFLAG for CI and ensure we've got nothing accidently set unexport GOFLAGS +# Version from the exact tag on HEAD; empty (uses the fallback in pkg/info/info.go) +# when HEAD is not tagged. +VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null | sed 's/^v//') + .PHONY: rosa rosa: - go build -ldflags="-X github.com/openshift/rosa/pkg/info.Build=$(shell git rev-parse --short HEAD)" ./cmd/rosa + go build -ldflags="-X github.com/openshift/rosa/pkg/info.Build=$(shell git rev-parse --short HEAD) $(if $(VERSION),-X github.com/openshift/rosa/pkg/info.DefaultVersion=$(VERSION),)" ./cmd/rosa .PHONY: test test: @@ -55,7 +59,7 @@ coverage-changed-files: .PHONY: install install: - go install -ldflags="-X github.com/openshift/rosa/pkg/info.Build=$(shell git rev-parse --short HEAD)" ./cmd/rosa + go install -ldflags="-X github.com/openshift/rosa/pkg/info.Build=$(shell git rev-parse --short HEAD) $(if $(VERSION),-X github.com/openshift/rosa/pkg/info.DefaultVersion=$(VERSION),)" ./cmd/rosa .PHONY: fmt fmt: $(GCI) diff --git a/hack/build_cli.sh b/hack/build_cli.sh index f8eff92930..edc45562ed 100644 --- a/hack/build_cli.sh +++ b/hack/build_cli.sh @@ -9,6 +9,11 @@ oses=(darwin linux windows) mkdir -p releases build_release() { +# Resolve exact tag on HEAD (stable or prerelease). Prerelease builds +# intentionally report the prerelease version (e.g. 1.2.66-rc1). +release_version=$(git describe --tags --exact-match 2>/dev/null || true) +release_version=${release_version#v} + for os in "${oses[@]}" do for arch in "${archs[@]}" @@ -19,7 +24,7 @@ do fi tmpdir=$(mktemp -d) trap 'rm -rf "$tmpdir"' EXIT - GOOS="${os}" GOARCH="${arch}" go build -ldflags="-X github.com/openshift/rosa/pkg/info.Build=$(git rev-parse --short HEAD)" -o "${tmpdir}/rosa${extension}" ./cmd/rosa + GOOS="${os}" GOARCH="${arch}" go build -ldflags="-X github.com/openshift/rosa/pkg/info.Build=$(git rev-parse --short HEAD)${release_version:+ -X github.com/openshift/rosa/pkg/info.DefaultVersion=${release_version}}" -o "${tmpdir}/rosa${extension}" ./cmd/rosa tar -czf "releases/rosa_${os}_${arch}.tar.gz" -C "${tmpdir}" "rosa${extension}" ( cd "${tmpdir}" && \ diff --git a/pkg/info/info.go b/pkg/info/info.go index f200fe524c..2e877dda90 100644 --- a/pkg/info/info.go +++ b/pkg/info/info.go @@ -18,9 +18,12 @@ limitations under the License. package info -const DefaultVersion = "1.2.65" +// DefaultVersion is the CLI version. For release builds it is overridden via +// -ldflags with the value derived from the git tag. The fallback here is kept +// current by an automated post-release workflow. +var DefaultVersion = "1.2.65" -// Build contains the short Git SHA of the CLI at the point it was build. Set via `-ldflags` at build time +// Build contains the short Git SHA of the CLI at the point it was built. Set via `-ldflags` at build time. var Build = "local" const DefaultUserAgent = "ROSACLI"