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.
This commit is contained in:
Marco Ferrari 2024-08-13 14:26:26 +02:00 committed by GitHub
parent bde3b9368e
commit 82094c879b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 5 deletions

View file

@ -34,15 +34,15 @@ jobs:
steps: steps:
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Set build metadata - name: Set build metadata
run: | run: |
if [[ ${{ github.event_name }} == 'push' ]] || [[ ${{ github.event_name }} == 'merge_group' ]]; then if [[ ${{ github.event_name }} == 'push' ]] || [[ ${{ github.event_name }} == 'merge_group' ]]; then
BUILD_REVISION=${{ github.sha }} BUILD_REVISION=${{ github.sha }}
BUILD_VERSION=${{ github.sha }}
elif [[ ${{ github.event_name }} == 'pull_request' ]]; then elif [[ ${{ github.event_name }} == 'pull_request' ]]; then
BUILD_REVISION=${{ github.event.pull_request.head.sha }} BUILD_REVISION=${{ github.event.pull_request.head.sha }}
BUILD_VERSION=${{ github.event.pull_request.head.sha }}
else else
echo "[ERROR] Event not supported when setting build revision and build version" echo "[ERROR] Event not supported when setting build revision and build version"
exit 1 exit 1

View file

@ -27,16 +27,16 @@ jobs:
steps: steps:
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Set build metadata - name: Set build metadata
id: set-container-image-build-metadata id: set-container-image-build-metadata
run: | run: |
if [[ ${{ github.event_name }} == 'push' ]] || [[ ${{ github.event_name }} == 'merge_group' ]]; then if [[ ${{ github.event_name }} == 'push' ]] || [[ ${{ github.event_name }} == 'merge_group' ]]; then
BUILD_REVISION=${{ github.sha }} BUILD_REVISION=${{ github.sha }}
BUILD_VERSION=${{ github.sha }}
elif [[ ${{ github.event_name }} == 'pull_request' ]]; then elif [[ ${{ github.event_name }} == 'pull_request' ]]; then
BUILD_REVISION=${{ github.event.pull_request.head.sha }} BUILD_REVISION=${{ github.event.pull_request.head.sha }}
BUILD_VERSION=${{ github.event.pull_request.head.sha }}
else else
echo "[ERROR] Event not supported when setting build revision and build version" echo "[ERROR] Event not supported when setting build revision and build version"
exit 1 exit 1

View file

@ -9,7 +9,12 @@ GetBuildDate() {
} }
GetBuildRevision() { GetBuildRevision() {
if [[ -v BUILD_REVISION ]]; then
# BUILD_REVISION is already set, no need to compute it
echo "${BUILD_REVISION}"
else
git rev-parse HEAD git rev-parse HEAD
fi
} }
GetBuildVersion() { GetBuildVersion() {
@ -21,6 +26,8 @@ GetBuildVersion() {
if git diff-tree --no-commit-id --name-only -r "${BUILD_REVISION}" | grep -q "${VERSION_FILE_PATH}"; then if git diff-tree --no-commit-id --name-only -r "${BUILD_REVISION}" | grep -q "${VERSION_FILE_PATH}"; then
cat "${VERSION_FILE_PATH}" cat "${VERSION_FILE_PATH}"
else else
# Fallback on the build revision to avoid that a non-release container image
# has BUILD_VERSION set to a release string
GetBuildRevision GetBuildRevision
fi fi
} }