From 1b0f50b0e6628ce3ea4e7065f4a9c20a94df7ef0 Mon Sep 17 00:00:00 2001 From: Simon Baird Date: Fri, 5 Jan 2024 13:25:23 -0500 Subject: [PATCH 1/4] Tweak build number for TAP builds I learned that we do now have the origin/main branch ref available, so we can use the `git merge-base --fork-point ...` idea mentioned in the comments. --- Dockerfile.dist | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile.dist b/Dockerfile.dist index f523104aa..cc6456080 100644 --- a/Dockerfile.dist +++ b/Dockerfile.dist @@ -56,16 +56,16 @@ RUN go mod download # Beware that EC_GIT_ORIGIN_BRANCH, EC_GIT_BRANCH and EC_VERSION may be # blank in a pre-merge PR because PR branch refs are generally not present. # -# For EC_BUILD_NUM, I'd like to try this: -# $( git rev-list --count $( git merge-base --fork-point main )..HEAD ) -# but I don't think the main branch ref will be known. +# Alternative for EC_BUILD_NUM using the nearest tag in case we decide we don't +# like the merge-base technique: +# EC_BUILD_NUM=$( git rev-list --count $( git describe --tags --abbrev=0 )..HEAD ); \ # RUN \ EC_GIT_SHA=$( git rev-parse --short HEAD ); \ EC_GIT_ORIGIN_BRANCH=$( git for-each-ref --points-at HEAD --format='%(refname:short)' refs/remotes/origin/ ); \ EC_GIT_BRANCH=${EC_GIT_ORIGIN_BRANCH#"origin/"}; \ EC_VERSION=${EC_GIT_BRANCH#"release-"}; \ - EC_BUILD_NUM=$( git rev-list --count $( git describe --tags --abbrev=0 )..HEAD ); \ + EC_BUILD_NUM=$( git rev-list --count $( git merge-base --fork-point origin/main )..HEAD ); \ EC_FULL_VERSION="${EC_VERSION}-${EC_BUILD_NUM}-${EC_GIT_SHA}"; \ if [ "$EC_GIT_BRANCH" = "$EC_VERSION" ]; then \ BUILDS="${TARGETOS}_${TARGETARCH}"; \ From 2145fa69629380312b5da515aec12892cee71aaa Mon Sep 17 00:00:00 2001 From: Simon Baird Date: Fri, 5 Jan 2024 13:53:22 -0500 Subject: [PATCH 2/4] Use generic name if there's no branch name version I don't think there's any significant impact for this, but for cosmetic reasons I think it's nicer to use something other than an empty string. Also fix a bug causing all arch/os builds to be build for a per-merge PR build. Note: The bash logic in the Docker file is starting to look quite messy. We might consider moving it to the Makefile if it gets any busier. --- Dockerfile.dist | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Dockerfile.dist b/Dockerfile.dist index cc6456080..cebafa6ab 100644 --- a/Dockerfile.dist +++ b/Dockerfile.dist @@ -53,8 +53,10 @@ RUN go mod download # EC_GIT_BRANCH=$( git rev-parse --abbrev-ref HEAD ) # but it doesn't work because the git-clone task checks out a sha directly # rather than a branch. That's why we need to use `git for-each-ref`. -# Beware that EC_GIT_ORIGIN_BRANCH, EC_GIT_BRANCH and EC_VERSION may be -# blank in a pre-merge PR because PR branch refs are generally not present. +# +# Note that EC_GIT_ORIGIN_BRANCH, EC_GIT_BRANCH may be blank in a pre-merge PR +# because the PR branch refs are generally not present. In that case we'll use +# "_ci_build" as the version. # # Alternative for EC_BUILD_NUM using the nearest tag in case we decide we don't # like the merge-base technique: @@ -66,12 +68,19 @@ RUN \ EC_GIT_BRANCH=${EC_GIT_ORIGIN_BRANCH#"origin/"}; \ EC_VERSION=${EC_GIT_BRANCH#"release-"}; \ EC_BUILD_NUM=$( git rev-list --count $( git merge-base --fork-point origin/main )..HEAD ); \ - EC_FULL_VERSION="${EC_VERSION}-${EC_BUILD_NUM}-${EC_GIT_SHA}"; \ - if [ "$EC_GIT_BRANCH" = "$EC_VERSION" ]; then \ + \ + if [ -z "$EC_VERSION" ]; then \ + EC_FULL_VERSION="_ci_build-${EC_GIT_SHA}"; \ + else \ + EC_FULL_VERSION="${EC_VERSION}-${EC_BUILD_NUM}-${EC_GIT_SHA}"; \ + fi; \ + \ + if [ -z "$EC_VERSION" -o "$EC_GIT_BRANCH" = "$EC_VERSION" ]; then \ BUILDS="${TARGETOS}_${TARGETARCH}"; \ else \ BUILDS="${BUILD_LIST}"; \ fi; \ + \ for os_arch in ${BUILDS}; do \ export GOOS="${os_arch%_*}"; \ export GOARCH="${os_arch#*_}"; \ From 663e3e8545045031fbf25648586f0cc39a19980b Mon Sep 17 00:00:00 2001 From: Simon Baird Date: Fri, 5 Jan 2024 15:17:36 -0500 Subject: [PATCH 3/4] Better go mod download layer caching It probably makes no difference when building in Konflux (formerly RHTAP), but for local builds, e.g. when you're testing and debugging the Dockerfile, this can save some time. It works because podman can now decide it doesn't have to rebuild the layer with all the go modules if the mod file didn't change from last time it was built. --- Dockerfile.dist | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile.dist b/Dockerfile.dist index cebafa6ab..eb6080e62 100644 --- a/Dockerfile.dist +++ b/Dockerfile.dist @@ -34,12 +34,15 @@ ARG TARGETARCH ARG BUILD_LIST="linux_amd64 linux_arm64 linux_ppc64le darwin_arm64" #ARG BUILD_LIST="linux_amd64 linux_arm64 linux_ppc64le darwin_arm64 darwin_amd64 windows_amd64" -COPY . /build - WORKDIR /build +# Copy just the mod file for better layer caching when building locally +COPY go.mod go.sum . RUN go mod download +# Now copy everything including .git +COPY . . + # Notes: # Derive the version from the branch name with the "release-" prefix removed # if it's present, e.g. in the branch "release-v0.1-alpha" the version will From e9662893d310a7e0dbafb551a112b0d91f62f072 Mon Sep 17 00:00:00 2001 From: Simon Baird Date: Fri, 5 Jan 2024 15:23:56 -0500 Subject: [PATCH 4/4] Adjust build list to match other rhtas clis IIUC the RHTAS clis build linux/darwin/windows for amd64 only, so let's follow their lead. I decided to include the darwin_arm64 as well since I think there are plenty of macbooks users out there who might want to try ec. --- Dockerfile.dist | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile.dist b/Dockerfile.dist index eb6080e62..ec3b6a610 100644 --- a/Dockerfile.dist +++ b/Dockerfile.dist @@ -31,8 +31,8 @@ ARG TARGETOS ARG TARGETARCH # Adjust as required -ARG BUILD_LIST="linux_amd64 linux_arm64 linux_ppc64le darwin_arm64" -#ARG BUILD_LIST="linux_amd64 linux_arm64 linux_ppc64le darwin_arm64 darwin_amd64 windows_amd64" +ARG BUILD_LIST="linux_amd64 darwin_amd64 windows_amd64 darwin_arm64" +#ARG BUILD_LIST="linux_amd64 darwin_amd64 windows_amd64 darwin_arm64 linux_arm64 linux_ppc64le" WORKDIR /build