From 9b3e8456a99c4faaefec3927d4b7483fe59aadbe Mon Sep 17 00:00:00 2001 From: Meng Yan Date: Thu, 17 Oct 2024 21:54:23 +0800 Subject: [PATCH] :sparkles: Add git commit to the to the images (#1169) * add git commit to the to the image and container Signed-off-by: myan * add image Signed-off-by: myan * replace the env Signed-off-by: myan --------- Signed-off-by: myan --- Makefile | 7 ++++--- agent/Dockerfile | 2 ++ manager/Dockerfile | 7 ++++--- operator/Dockerfile | 10 ++++++---- pkg/utils/utils.go | 1 + 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 59736e261..92050e147 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ REGISTRY ?= quay.io/stolostron IMAGE_TAG ?= latest TMP_BIN ?= /tmp/cr-tests-bin GO_TEST ?= go test -v +GIT_COMMIT := $(shell git rev-parse --short HEAD) .PHONY: vendor ##download all third party libraries and puts them inside vendor directory vendor: @@ -22,7 +23,7 @@ clean-vendor: build-operator-image: vendor cd operator && make - docker build -t ${REGISTRY}/multicluster-global-hub-operator:${IMAGE_TAG} . -f operator/Dockerfile + docker build -t ${REGISTRY}/multicluster-global-hub-operator:${IMAGE_TAG} . -f operator/Dockerfile --build-arg GIT_COMMIT=$(GIT_COMMIT) push-operator-image: docker push ${REGISTRY}/multicluster-global-hub-operator:${IMAGE_TAG} @@ -35,14 +36,14 @@ undeploy-operator: build-manager-image: vendor cd manager && make - docker build -t ${REGISTRY}/multicluster-global-hub-manager:${IMAGE_TAG} . -f manager/Dockerfile + docker build -t ${REGISTRY}/multicluster-global-hub-manager:${IMAGE_TAG} . -f manager/Dockerfile --build-arg GIT_COMMIT=$(GIT_COMMIT) push-manager-image: docker push ${REGISTRY}/multicluster-global-hub-manager:${IMAGE_TAG} build-agent-image: vendor cd agent && make - docker build -t ${REGISTRY}/multicluster-global-hub-agent:${IMAGE_TAG} . -f agent/Dockerfile + docker build -t ${REGISTRY}/multicluster-global-hub-agent:${IMAGE_TAG} . -f agent/Dockerfile --build-arg GIT_COMMIT=$(GIT_COMMIT) push-agent-image: docker push ${REGISTRY}/multicluster-global-hub-agent:${IMAGE_TAG} diff --git a/agent/Dockerfile b/agent/Dockerfile index b4e8d7092..0f135c4d2 100644 --- a/agent/Dockerfile +++ b/agent/Dockerfile @@ -13,8 +13,10 @@ RUN go build -o bin/agent ./agent/cmd/main.go # Stage 2: Copy the binaries from the image builder to the base image FROM registry.access.redhat.com/ubi9/ubi-minimal:latest +ARG GIT_COMMIT ENV USER_UID=1001 ENV USER_NAME=agent +ENV GIT_COMMIT=${GIT_COMMIT} # install operator binary COPY --from=builder /workspace/bin/agent /usr/local/bin/agent diff --git a/manager/Dockerfile b/manager/Dockerfile index 6d47aa59d..396a25c88 100644 --- a/manager/Dockerfile +++ b/manager/Dockerfile @@ -14,17 +14,18 @@ RUN go build -o bin/manager ./manager/cmd/manager/main.go # Stage 2: Copy the binaries from the image builder to the base image FROM registry.access.redhat.com/ubi9/ubi-minimal:latest +ARG GIT_COMMIT ENV USER_UID=1001 ENV USER_NAME=manager +ENV GIT_COMMIT=${GIT_COMMIT} # install operator binary COPY --from=builder /workspace/bin/manager /usr/local/bin/manager COPY ./manager/scripts/user_setup /usr/local/scripts/user_setup -RUN /usr/local/scripts/user_setup +RUN /usr/local/scripts/user_setup -RUN microdnf update -y && \ - microdnf clean all +RUN microdnf update -y && microdnf clean all USER ${USER_UID} ENTRYPOINT ["/usr/local/bin/manager"] diff --git a/operator/Dockerfile b/operator/Dockerfile index efe4aed98..f389957ce 100644 --- a/operator/Dockerfile +++ b/operator/Dockerfile @@ -13,16 +13,18 @@ RUN CGO_ENABLED=1 go build -a -installsuffix cgo -o bin/multicluster-global-hub- # Stage 2: Copy the binaries from the image builder to the base image FROM registry.access.redhat.com/ubi9/ubi-minimal:latest - +ARG GIT_COMMIT ENV OPERATOR=/usr/local/bin/multicluster-global-hub-operator \ - USER_UID=1001 \ - USER_NAME=operator + USER_UID=1001 \ + USER_NAME=operator +ENV GIT_COMMIT=${GIT_COMMIT} + # install operator binary COPY --from=builder /workspace/bin/multicluster-global-hub-operator ${OPERATOR} RUN microdnf update -y && \ - microdnf clean all + microdnf clean all USER ${USER_UID} ENTRYPOINT ["/usr/local/bin/multicluster-global-hub-operator"] diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index acc9cced7..226da5c1b 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -25,6 +25,7 @@ import ( func PrintVersion(log logr.Logger) { log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) + log.Info(fmt.Sprintf("Git Commit: %s", os.Getenv("GIT_COMMIT"))) } func CtrlZapOptions() zap.Options {