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
This commit is contained in:
Marco Ferrari 2024-08-13 12:10:31 +02:00 committed by GitHub
parent 0c6e9a5778
commit bde3b9368e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 69 additions and 9 deletions

View file

@ -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}"

View file

@ -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}"

View file

@ -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; \

View file

@ -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 \

37
scripts/build-metadata.sh Executable file
View file

@ -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}"