From bde3b9368e78f0cbae6316edd9657b8a5c0f55d3 Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Tue, 13 Aug 2024 12:10:31 +0200 Subject: [PATCH] build: set build_version dynamically (#6013) Set BUILD_VERSION to the content of the version descriptor (version.txt) if it changed in the last commit, assuming that the last commit was a release preparation commit that updated the version descriptor. Close #4928 --- .github/workflows/cd.yml | 13 ++++++++++++- .github/workflows/ci.yml | 18 +++++++++++++----- Makefile | 3 +++ docs/run-linter-locally.md | 7 ++++--- scripts/build-metadata.sh | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100755 scripts/build-metadata.sh diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 7c78d7c6..130ffc9c 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -48,6 +48,13 @@ jobs: exit 1 fi + . scripts/build-metadata.sh + + if [ -z "${BUILD_DATE}" ]; then + echo "[ERROR] BUILD_DATE is empty" + exit 1 + fi + if [ -z "${BUILD_REVISION}" ]; then echo "[ERROR] BUILD_REVISION is empty" exit 1 @@ -58,8 +65,12 @@ jobs: exit 1 fi + echo "Build date (GH Actions workflow): ${BUILD_DATE}" + echo "Build revision (GH Actions workflow): ${BUILD_REVISION}" + echo "Build version (GH Actions workflow): ${BUILD_VERSION}" + { - echo "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" + echo "BUILD_DATE=${BUILD_DATE}" echo "BUILD_REVISION=${BUILD_REVISION}" echo "BUILD_VERSION=${BUILD_VERSION}" } >> "${GITHUB_ENV}" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd0c148c..965c13e6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,6 +25,9 @@ jobs: CONTAINER_IMAGE_BUILD_REVISION: ${{ steps.set-container-image-build-metadata.outputs.CONTAINER_IMAGE_BUILD_REVISION }} CONTAINER_IMAGE_BUILD_VERSION: ${{ steps.set-container-image-build-metadata.outputs.CONTAINER_IMAGE_BUILD_VERSION }} steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Set build metadata id: set-container-image-build-metadata run: | @@ -39,6 +42,13 @@ jobs: exit 1 fi + . scripts/build-metadata.sh + + if [ -z "${BUILD_DATE}" ]; then + echo "[ERROR] BUILD_DATE is empty" + exit 1 + fi + if [ -z "${BUILD_REVISION}" ]; then echo "[ERROR] BUILD_REVISION is empty" exit 1 @@ -49,11 +59,9 @@ jobs: exit 1 fi - BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') - - echo "Build date: ${BUILD_DATE}" - echo "Build revision: ${BUILD_REVISION}" - echo "Build version: ${BUILD_VERSION}" + echo "Build date (GH Actions workflow): ${BUILD_DATE}" + echo "Build revision (GH Actions workflow): ${BUILD_REVISION}" + echo "Build version (GH Actions workflow): ${BUILD_VERSION}" { echo "BUILD_DATE=${BUILD_DATE}" diff --git a/Makefile b/Makefile index d29a2a42..3bca7fdd 100644 --- a/Makefile +++ b/Makefile @@ -82,6 +82,9 @@ info: ## Gather information about the runtime environment echo "whoami: $$(whoami)"; \ echo "pwd: $$(pwd)"; \ echo "IMAGE:" $(IMAGE); \ + echo "Build date: ${BUILD_DATE}"; \ + echo "Build revision: ${BUILD_REVISION}"; \ + echo "Build version: ${BUILD_VERSION}"; \ echo "SUPER_LINTER_TEST_CONTAINER_URL:" $(SUPER_LINTER_TEST_CONTAINER_URL); \ echo "ls -ahl: $$(ls -ahl)"; \ docker images; \ diff --git a/docs/run-linter-locally.md b/docs/run-linter-locally.md index 854f0a99..0180e459 100644 --- a/docs/run-linter-locally.md +++ b/docs/run-linter-locally.md @@ -100,11 +100,12 @@ directory, do the following: 1. Run the build process: ```bash - make + . ./scripts/build-metadata.sh && make ``` -To avoid invalidating the build cache, and reuse it, you can set build metadata -to arbitrary values before running `make`: +To avoid invalidating the build cache because of changing values of build +arguments, you can set build arguments to arbitrary values before running +`make`, instead of sourcing `scripts/build-metadata.sh`: ```bash BUILD_DATE=2023-12-12T09:32:05Z \ diff --git a/scripts/build-metadata.sh b/scripts/build-metadata.sh new file mode 100755 index 00000000..0dc6ed4a --- /dev/null +++ b/scripts/build-metadata.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +GetBuildDate() { + date -u +'%Y-%m-%dT%H:%M:%SZ' +} + +GetBuildRevision() { + git rev-parse HEAD +} + +GetBuildVersion() { + local VERSION_FILE_PATH="version.txt" + local BUILD_REVISION="${1}" + # Get the version from the version descriptor if changed in the last commit. + # This assumes that the last commit was a "release preparation" commit that + # updated the version descriptor + if git diff-tree --no-commit-id --name-only -r "${BUILD_REVISION}" | grep -q "${VERSION_FILE_PATH}"; then + cat "${VERSION_FILE_PATH}" + else + GetBuildRevision + fi +} + +BUILD_DATE="${BUILD_DATE:-"$(GetBuildDate)"}" +export BUILD_DATE +BUILD_REVISION="${BUILD_REVISION:-"$(GetBuildRevision)"}" +export BUILD_REVISION +BUILD_VERSION="${BUILD_VERSION:-"$(GetBuildVersion "${BUILD_REVISION}")"}" +export BUILD_VERSION + +echo "Build date: ${BUILD_DATE}" +echo "Build revision: ${BUILD_REVISION}" +echo "Build version: ${BUILD_VERSION}"