diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c8556aca..0a641c2e 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -126,7 +126,7 @@ jobs: ACTOR: ${{ github.actor }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - make buildimg IMG="${IMAGE}:${TAG}" + make buildimg IMG="${IMAGE}:${TAG}" VERSION="$(cat VERSION)" podman login -u "${ACTOR}" -p "${GH_TOKEN}" ghcr.io podman push "${IMAGE}:${TAG}" diff --git a/Containerfile b/Containerfile index 04e7f3c5..9d2c2805 100644 --- a/Containerfile +++ b/Containerfile @@ -21,7 +21,7 @@ RUN --mount=type=cache,id=dnf,target=/var/cache/libdnf5 \ FROM ${GO_BUILDER_IMAGE} AS builder ARG VERSION=dev -ARG GIT_COMMIT=unknown +ARG GIT_COMMIT= WORKDIR /workspace COPY go.mod go.sum ./ RUN --mount=type=cache,id=gomod,target=/root/go/pkg/mod \ diff --git a/Makefile b/Makefile index 8ea3cf02..778c8edf 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ CONTAINER_TOOL ?= podman BINK_CLUSTER_NAME ?= e2e KUBECONFIG_BINK ?= ./kubeconfig-$(BINK_CLUSTER_NAME) ARTIFACTS ?= $(abspath _output/logs) -VERSION ?= $(shell git describe --tags --always --dirty) -GIT_COMMIT ?= $(shell git rev-parse --short HEAD) +VERSION ?= dev +GIT_COMMIT ?= $(if $(shell git status --porcelain --untracked-files=no),dirty,$(shell git rev-parse HEAD)) LDFLAGS := -X github.com/bootc-dev/bootc-operator/internal/version.Version=$(VERSION) \ -X github.com/bootc-dev/bootc-operator/internal/version.GitCommit=$(GIT_COMMIT) DEFAULT_KUBE_MINOR ?= 1.36 @@ -176,7 +176,9 @@ build-daemon: ## Build daemon binary. .PHONY: buildimg buildimg: ## Build container image. - $(CONTAINER_TOOL) build -t $(IMG) . + $(CONTAINER_TOOL) build \ + --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) \ + -t $(IMG) . .PHONY: release-manifest release-manifest: kustomize yq ## Build install manifest (override IMG to set the image reference). diff --git a/cmd/controller/main.go b/cmd/controller/main.go index f15223d3..96b3d2a7 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -4,6 +4,7 @@ package main import ( "flag" + "fmt" "os" "strings" "time" @@ -46,6 +47,7 @@ func init() { func main() { var enableLeaderElection bool + var showVersion bool var probeAddr string var tagResolutionInterval time.Duration var allowInsecureRegistry bool @@ -70,11 +72,16 @@ func main() { flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") + flag.BoolVar(&showVersion, "version", false, "Print version and exit.") opts := zap.Options{ Development: true, } opts.BindFlags(flag.CommandLine) flag.Parse() + if showVersion { + fmt.Println(version.String()) + return + } ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index cdebba84..c5fb680b 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -36,18 +36,24 @@ func init() { func main() { var pollInterval time.Duration + var showVersion bool flag.DurationVar( &pollInterval, "bootc-poll-interval", daemon.DefaultPollInterval, "Interval for polling bootc status as a fallback to fsnotify", ) + flag.BoolVar(&showVersion, "version", false, "Print version and exit.") opts := zap.Options{ Development: true, } opts.BindFlags(flag.CommandLine) flag.Parse() + if showVersion { + fmt.Println(version.String()) + return + } ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) diff --git a/internal/version/version.go b/internal/version/version.go index de51aa6f..06ca996c 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -3,7 +3,21 @@ // Package version holds build metadata set via linker flags. package version +// dirtyCommit marks builds from a worktree with uncommitted changes. +const dirtyCommit = "dirty" + var ( Version = "dev" - GitCommit = "unknown" + GitCommit = "" ) + +// String returns the version and its Git revision when available. +func String() string { + switch GitCommit { + case "": + return Version + case dirtyCommit: + return Version + " (" + dirtyCommit + ")" + } + return Version + " (commit " + GitCommit + ")" +}