From cdef31276a7f3c5ee193baf50794e81bcafa1d22 Mon Sep 17 00:00:00 2001 From: Blake Pettersson Date: Tue, 20 Jun 2023 21:19:09 +0200 Subject: [PATCH] chore: add build-args for git-commit etc (#14114) * build: add build-args for git-commit etc Add the ability to specify `GIT_TAG`, `GIT_COMMIT`, `BUILD_DATE` and `GIT_TREE_STATE` as optional build-args. As well as resolving #13683 (which was caused by #12620), this has the bonus of making the `docker build` slightly more deterministic (since we now have the ability to specify the same inputs into the docker build which was hitherto computed on every `docker build`). Signed-off-by: Blake Pettersson * Update .github/workflows/image-reuse.yaml Co-authored-by: Josh Soref <2119212+jsoref@users.noreply.github.com> Signed-off-by: Blake Pettersson --------- Signed-off-by: Blake Pettersson Co-authored-by: Josh Soref <2119212+jsoref@users.noreply.github.com> --- .github/workflows/image-reuse.yaml | 14 +++++++++++++- Dockerfile | 13 ++++++++++++- Makefile | 8 ++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/.github/workflows/image-reuse.yaml b/.github/workflows/image-reuse.yaml index d5232e148c037..399d68f2eb6ec 100644 --- a/.github/workflows/image-reuse.yaml +++ b/.github/workflows/image-reuse.yaml @@ -128,6 +128,13 @@ jobs: password: ${{ secrets.docker_password }} if: ${{ inputs.docker_image_name && inputs.push }} + - name: Set up build args for container image + run: | + echo "GIT_TAG=$(if [ -z "`git status --porcelain`" ]; then git describe --exact-match --tags HEAD 2>/dev/null; fi)" >> $GITHUB_ENV + echo "GIT_COMMIT=$(git rev-parse HEAD)" >> $GITHUB_ENV + echo "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_ENV + echo "GIT_TREE_STATE=$(if [ -z "`git status --porcelain`" ]; then echo "clean" ; else echo "dirty"; fi)" >> $GITHUB_ENV + - name: Build and push container image id: image uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825 #v4.1.1 @@ -139,7 +146,12 @@ jobs: target: ${{ inputs.target }} provenance: false sbom: false - + build-args: | + GIT_TAG=${{env.GIT_TAG}} + GIT_COMMIT=${{env.GIT_COMMIT}} + BUILD_DATE=${{env.BUILD_DATE}} + GIT_TREE_STATE=${{env.GIT_TREE_STATE}} + - name: Sign container images run: | for signing_tag in $SIGNING_TAGS; do diff --git a/Dockerfile b/Dockerfile index 896ab2f05dd90..d6bab6f899256 100644 --- a/Dockerfile +++ b/Dockerfile @@ -113,7 +113,18 @@ COPY . . COPY --from=argocd-ui /src/dist/app /go/src/github.com/argoproj/argo-cd/ui/dist/app ARG TARGETOS ARG TARGETARCH -RUN GOOS=$TARGETOS GOARCH=$TARGETARCH make argocd-all +# These build args are optional; if not specified the defaults will be taken from the Makefile +ARG GIT_TAG +ARG BUILD_DATE +ARG GIT_TREE_STATE +ARG GIT_COMMIT +RUN GIT_COMMIT=$GIT_COMMIT \ + GIT_TREE_STATE=$GIT_TREE_STATE \ + GIT_TAG=$GIT_TAG \ + BUILD_DATE=$BUILD_DATE \ + GOOS=$TARGETOS \ + GOARCH=$TARGETARCH \ + make argocd-all #################################################################################################### # Final image diff --git a/Makefile b/Makefile index ec22c096c652a..b34dbf0f6be11 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,10 @@ HOST_OS:=$(shell go env GOOS) HOST_ARCH:=$(shell go env GOARCH) VERSION=$(shell cat ${CURRENT_DIR}/VERSION) -BUILD_DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') -GIT_COMMIT=$(shell git rev-parse HEAD) -GIT_TAG=$(shell if [ -z "`git status --porcelain`" ]; then git describe --exact-match --tags HEAD 2>/dev/null; fi) -GIT_TREE_STATE=$(shell if [ -z "`git status --porcelain`" ]; then echo "clean" ; else echo "dirty"; fi) +BUILD_DATE:=$(if $(BUILD_DATE),$(BUILD_DATE),$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')) +GIT_COMMIT:=$(if $(GIT_COMMIT),$(GIT_COMMIT),$(shell git rev-parse HEAD)) +GIT_TAG:=$(if $(GIT_TAG),$(GIT_TAG),$(shell if [ -z "`git status --porcelain`" ]; then git describe --exact-match --tags HEAD 2>/dev/null; fi)) +GIT_TREE_STATE:=$(if $(GIT_TREE_STATE),$(GIT_TREE_STATE),$(shell if [ -z "`git status --porcelain`" ]; then echo "clean" ; else echo "dirty"; fi)) VOLUME_MOUNT=$(shell if test "$(go env GOOS)" = "darwin"; then echo ":delegated"; elif test selinuxenabled; then echo ":delegated"; else echo ""; fi) KUBECTL_VERSION=$(shell go list -m k8s.io/client-go | head -n 1 | rev | cut -d' ' -f1 | rev)