From 82094c879beda024f7a29b351fb67a3be54913e0 Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Tue, 13 Aug 2024 14:26:26 +0200 Subject: [PATCH] build: fix build metadata (#6015) - Don't set BUILD_VERSION in CI/CD workflows otherwise the build-metadata script will always fall back to those values instead of computing new ones. - When calculating BUILD_REVISION, check if BUILD_REVISION is set before falling back. --- .github/workflows/cd.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- scripts/build-metadata.sh | 9 ++++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 130ffc9c..d30e9003 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -34,15 +34,15 @@ jobs: steps: - name: Checkout Code uses: actions/checkout@v4 + with: + fetch-depth: 2 - name: Set build metadata run: | if [[ ${{ github.event_name }} == 'push' ]] || [[ ${{ github.event_name }} == 'merge_group' ]]; then BUILD_REVISION=${{ github.sha }} - BUILD_VERSION=${{ github.sha }} elif [[ ${{ github.event_name }} == 'pull_request' ]]; then BUILD_REVISION=${{ github.event.pull_request.head.sha }} - BUILD_VERSION=${{ github.event.pull_request.head.sha }} else echo "[ERROR] Event not supported when setting build revision and build version" exit 1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 965c13e6..ec5ae62b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,16 +27,16 @@ jobs: steps: - name: Checkout Code uses: actions/checkout@v4 + with: + fetch-depth: 2 - name: Set build metadata id: set-container-image-build-metadata run: | if [[ ${{ github.event_name }} == 'push' ]] || [[ ${{ github.event_name }} == 'merge_group' ]]; then BUILD_REVISION=${{ github.sha }} - BUILD_VERSION=${{ github.sha }} elif [[ ${{ github.event_name }} == 'pull_request' ]]; then BUILD_REVISION=${{ github.event.pull_request.head.sha }} - BUILD_VERSION=${{ github.event.pull_request.head.sha }} else echo "[ERROR] Event not supported when setting build revision and build version" exit 1 diff --git a/scripts/build-metadata.sh b/scripts/build-metadata.sh index 0dc6ed4a..f5624a52 100755 --- a/scripts/build-metadata.sh +++ b/scripts/build-metadata.sh @@ -9,7 +9,12 @@ GetBuildDate() { } GetBuildRevision() { - git rev-parse HEAD + if [[ -v BUILD_REVISION ]]; then + # BUILD_REVISION is already set, no need to compute it + echo "${BUILD_REVISION}" + else + git rev-parse HEAD + fi } GetBuildVersion() { @@ -21,6 +26,8 @@ GetBuildVersion() { if git diff-tree --no-commit-id --name-only -r "${BUILD_REVISION}" | grep -q "${VERSION_FILE_PATH}"; then cat "${VERSION_FILE_PATH}" else + # Fallback on the build revision to avoid that a non-release container image + # has BUILD_VERSION set to a release string GetBuildRevision fi }