Skip to content

Commit 71c4ed1

Browse files
committed
Include and print build details
1 parent b1cb7fd commit 71c4ed1

File tree

7 files changed

+92
-7
lines changed

7 files changed

+92
-7
lines changed

.github/workflows/build.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ jobs:
3939
echo "VERSION=${VERSION}" >> $GITHUB_ENV
4040
echo "VERSION=${VERSION}"
4141
42+
- name: Set build args
43+
id: build-args
44+
run: |
45+
GIT_COMMIT=$(git rev-parse HEAD)
46+
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
47+
echo "git_commit=${GIT_COMMIT}" >> $GITHUB_OUTPUT
48+
echo "build_date=${BUILD_DATE}" >> $GITHUB_OUTPUT
49+
echo "GIT_COMMIT=${GIT_COMMIT}"
50+
echo "BUILD_DATE=${BUILD_DATE}"
51+
4252
- name: Set image tags
4353
id: set-tags
4454
run: |
@@ -79,6 +89,10 @@ jobs:
7989
platforms: linux/amd64,linux/arm64
8090
cache-from: type=gha
8191
cache-to: type=gha,mode=max
92+
build-args: |
93+
GIT_VERSION=${{ env.VERSION }}
94+
GIT_COMMIT=${{ steps.build-args.outputs.git_commit }}
95+
BUILD_DATE=${{ steps.build-args.outputs.build_date }}
8296
8397
publish-helm-charts-containers:
8498
needs: build-and-push-image

Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
FROM registry.access.redhat.com/ubi9/go-toolset:1.24.6 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
5+
ARG GIT_VERSION=unknown
6+
ARG GIT_COMMIT=unknown
7+
ARG BUILD_DATE=unknown
58

69
# Copy the Go Modules manifests
710
COPY go.mod go.mod
@@ -20,7 +23,10 @@ COPY internal/ internal/
2023
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2124
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2225
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
23-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
26+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} \
27+
go build -a \
28+
-ldflags "-X main.version=${GIT_VERSION} -X main.gitCommit=${GIT_COMMIT} -X main.buildDate=${BUILD_DATE}" \
29+
-o manager cmd/main.go
2430
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o router cmd/router/main.go
2531

2632
FROM registry.access.redhat.com/ubi9/ubi-micro:9.5

Makefile

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ DOCKER_TAG = $(shell echo $(IMG) | cut -d: -f2)
77
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
88
ENVTEST_K8S_VERSION = 1.30.0
99

10+
# Version information
11+
GIT_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "unknown")
12+
GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
13+
BUILD_DATE := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
14+
15+
# LDFLAGS for version information
16+
LDFLAGS := -X main.version=$(GIT_VERSION) \
17+
-X main.gitCommit=$(GIT_COMMIT) \
18+
-X main.buildDate=$(BUILD_DATE)
19+
1020
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
1121
ifeq (,$(shell go env GOBIN))
1222
GOBIN=$(shell go env GOPATH)/bin
@@ -91,7 +101,7 @@ build-operator:
91101

92102
.PHONY: build
93103
build: manifests generate fmt vet ## Build manager binary.
94-
go build -o bin/manager cmd/main.go
104+
go build -ldflags "$(LDFLAGS)" -o bin/manager cmd/main.go
95105
go build -o bin/router cmd/router/main.go
96106

97107
.PHONY: run
@@ -107,7 +117,11 @@ run-router: manifests generate fmt vet ## Run a router from your host.
107117
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
108118
.PHONY: docker-build
109119
docker-build: ## Build docker image with the manager.
110-
$(CONTAINER_TOOL) build -t ${IMG} .
120+
$(CONTAINER_TOOL) build \
121+
--build-arg GIT_VERSION=$(GIT_VERSION) \
122+
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
123+
--build-arg BUILD_DATE=$(BUILD_DATE) \
124+
-t ${IMG} .
111125

112126
.PHONY: docker-push
113127
docker-push: ## Push docker image with the manager.
@@ -127,6 +141,9 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
127141
- $(CONTAINER_TOOL) buildx create --name jumpstarter-controller-builder
128142
$(CONTAINER_TOOL) buildx use jumpstarter-controller-builder
129143
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) \
144+
--build-arg GIT_VERSION=$(GIT_VERSION) \
145+
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
146+
--build-arg BUILD_DATE=$(BUILD_DATE) \
130147
--tag ${DOCKER_REPO}:${DOCKER_TAG} \
131148
--tag ${DOCKER_REPO}:latest \
132149
-f Dockerfile.cross .

cmd/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ import (
5656
var (
5757
scheme = runtime.NewScheme()
5858
setupLog = ctrl.Log.WithName("setup")
59+
60+
// Version information - set via ldflags at build time
61+
version = "dev"
62+
gitCommit = "unknown"
63+
buildDate = "unknown"
5964
)
6065

6166
const (
@@ -120,6 +125,13 @@ func main() {
120125

121126
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
122127

128+
// Print version information
129+
setupLog.Info("Jumpstarter Controller starting",
130+
"version", version,
131+
"gitCommit", gitCommit,
132+
"buildDate", buildDate,
133+
)
134+
123135
// if the enable-http2 flag is false (the default), http/2 should be disabled
124136
// due to its vulnerabilities. More specifically, disabling http/2 will
125137
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and

deploy/operator/Dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
FROM registry.access.redhat.com/ubi9/go-toolset:1.24.6 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
5+
ARG GIT_VERSION=unknown
6+
ARG GIT_COMMIT=unknown
7+
ARG BUILD_DATE=unknown
58

69
# Copy the Go Modules manifests
710
COPY --chown=1001:0 go.mod go.mod
@@ -20,7 +23,10 @@ COPY --chown=1001:0 internal/ internal/
2023
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2124
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2225
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
23-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
26+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} \
27+
go build -a \
28+
-ldflags "-X main.version=${GIT_VERSION} -X main.gitCommit=${GIT_COMMIT} -X main.buildDate=${BUILD_DATE}" \
29+
-o manager cmd/main.go
2430

2531
FROM registry.access.redhat.com/ubi9/ubi-micro:9.5
2632
WORKDIR /

deploy/operator/Makefile

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ endif
6565
# tools. (i.e. podman)
6666
CONTAINER_TOOL ?= podman
6767

68+
# Version information
69+
GIT_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "unknown")
70+
GIT_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
71+
BUILD_DATE := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
72+
73+
# LDFLAGS for version information
74+
LDFLAGS := -X main.version=$(GIT_VERSION) \
75+
-X main.gitCommit=$(GIT_COMMIT) \
76+
-X main.buildDate=$(BUILD_DATE)
77+
6878
# Setting SHELL to bash allows bash commands to be executed by recipes.
6979
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
7080
SHELL = /usr/bin/env bash -o pipefail
@@ -157,7 +167,7 @@ lint-config: golangci-lint ## Verify golangci-lint linter configuration
157167

158168
.PHONY: build
159169
build: manifests generate fmt vet ## Build manager binary.
160-
go build -o bin/manager cmd/main.go
170+
go build -ldflags "$(LDFLAGS)" -o bin/manager cmd/main.go
161171

162172
.PHONY: run
163173
run: manifests generate fmt vet ## Run a controller from your host.
@@ -168,7 +178,11 @@ run: manifests generate fmt vet ## Run a controller from your host.
168178
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
169179
.PHONY: docker-build
170180
docker-build: ## Build docker image with the manager.
171-
$(CONTAINER_TOOL) build -t ${IMG} .
181+
$(CONTAINER_TOOL) build \
182+
--build-arg GIT_VERSION=$(GIT_VERSION) \
183+
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
184+
--build-arg BUILD_DATE=$(BUILD_DATE) \
185+
-t ${IMG} .
172186

173187
.PHONY: docker-push
174188
docker-push: ## Push docker image with the manager.
@@ -187,7 +201,11 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
187201
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
188202
- $(CONTAINER_TOOL) buildx create --name jumpstarter-operator-builder
189203
$(CONTAINER_TOOL) buildx use jumpstarter-operator-builder
190-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
204+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) \
205+
--build-arg GIT_VERSION=$(GIT_VERSION) \
206+
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
207+
--build-arg BUILD_DATE=$(BUILD_DATE) \
208+
--tag ${IMG} -f Dockerfile.cross .
191209
- $(CONTAINER_TOOL) buildx rm jumpstarter-operator-builder
192210
rm Dockerfile.cross
193211

deploy/operator/cmd/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ import (
4646
var (
4747
scheme = runtime.NewScheme()
4848
setupLog = ctrl.Log.WithName("setup")
49+
50+
// Version information - set via ldflags at build time
51+
version = "dev"
52+
gitCommit = "unknown"
53+
buildDate = "unknown"
4954
)
5055

5156
func init() {
@@ -90,6 +95,13 @@ func main() {
9095

9196
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
9297

98+
// Print version information
99+
setupLog.Info("Jumpstarter Operator starting",
100+
"version", version,
101+
"gitCommit", gitCommit,
102+
"buildDate", buildDate,
103+
)
104+
93105
// if the enable-http2 flag is false (the default), http/2 should be disabled
94106
// due to its vulnerabilities. More specifically, disabling http/2 will
95107
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and

0 commit comments

Comments
 (0)