lint/scripts/build-metadata.sh
Marco Ferrari bde3b9368e
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
2024-08-13 12:10:31 +02:00

37 lines
976 B
Bash
Executable file

#!/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}"