Skip to content

Commit 99662a5

Browse files
authored
propagate BUILD_VERSION and BUILD_DATE to docker build/buildx (#105)
1 parent db35ae4 commit 99662a5

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
FROM golang:1.21 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
5+
ARG BUILD_VERSION=Unknown
6+
ARG BUILD_DATE=Unknown
57

68
WORKDIR /workspace
79
# Copy the Go Modules manifests
@@ -22,7 +24,9 @@ COPY pkg/ pkg/
2224
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2325
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2426
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
25-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
27+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a \
28+
-ldflags "-X 'main.BuildVersion=${BUILD_VERSION}' -X 'main.BuildDate=${BUILD_DATE}'" \
29+
-o manager cmd/main.go
2630

2731
# Use distroless as minimal base image to package the manager binary
2832
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ build: manifests generate fmt vet ## Build manager binary.
139139
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
140140
.PHONY: docker-build
141141
docker-build: ## Build docker image with the manager.
142-
$(CONTAINER_TOOL) build -t ${IMG} .
142+
$(CONTAINER_TOOL) build --build-arg='BUILD_VERSION=$(BUILD_VERSION)' --build-arg='BUILD_DATE=$(BUILD_DATE)' -t ${IMG} .
143143

144144
.PHONY: docker-push
145145
docker-push: ## Push docker image with the manager.
@@ -162,7 +162,7 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
162162
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
163163
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
164164
$(CONTAINER_TOOL) buildx use project-v3-builder
165-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
165+
- $(CONTAINER_TOOL) buildx build --build-arg='BUILD_VERSION=$(BUILD_VERSION)' --build-arg='BUILD_DATE=$(BUILD_DATE)' --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
166166
- $(CONTAINER_TOOL) buildx rm project-v3-builder
167167
rm Dockerfile.cross
168168

0 commit comments

Comments
 (0)