Skip to content

Commit

Permalink
tools/image-tag: report consistent build information
Browse files Browse the repository at this point in the history
This commit updates tools/image-tag to always report consistent build
information, with escape hatches to force the reported version:

1. If RELEASE_TAG is set (which happens via Drone pipelines), then the
   script emits the value of the RELEASE_TAG environment variable.

2. If a build is being performed against a Git tag, then the script
   emits the value of that Git tag.

3. Finally, if neither of the above are true, the version from the
   VERSION file is used, followed by the prerelease being set to `devel`
   and the short SHA added as build metadata.

Provided healthy workflwos where RELEASE_TAG and Git tags are always
semantic versions, this guarantees that versions reported by builds are
consistent and can be parsed properly.
  • Loading branch information
rfratto committed Mar 19, 2024
1 parent 0ceaf33 commit b21cc1f
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions tools/image-tag
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#!/usr/bin/env bash
#
# image-tag determines which version to embed into a built image.
#
# It prefers the following in precedence order:
#
# 1. RELEASE_TAG environment variable
# 2. The Git tag of the current commit (if any)
# 3. The version in the VERSION file, suffixed with -devel plus build
# information.
set -o errexit
set -o pipefail

VERSION=$(sed -e '/^#/d' -e '/^$/d' VERSION | tr -d '\n')
DETECTED_TAG=$(git describe --match 'v*' --exact-match 2>/dev/null || echo -n "")

if [ ! -z "${RELEASE_TAG}" ]; then
echo ${RELEASE_TAG}
exit 0
elif [ ! -z "${DETECTED_TAG}" ]; then
echo ${DETECTED_TAG}
exit 0
fi

set -o nounset

WIP=$(git diff --quiet || echo '-WIP')
BRANCH=$(git rev-parse --abbrev-ref HEAD | sed 's#/#-#g')

# When 7 chars are not enough to be unique, git automatically uses more.
# We are forcing to 7 here, as we are doing for grafana/grafana as well.
SHA=$(git rev-parse --short=7 HEAD | head -c7)

# If this is a tag, use it, otherwise use <branch>-<hash>
TAG=$(git describe --exact-match 2> /dev/null || echo "${BRANCH}-${SHA}")
echo ${TAG}${WIP}
if [[ -z $(git status -s) ]]; then
# There are no changes; report version as VERSION-devel+SHA.
SHA=$(git rev-parse --short HEAD)
echo ${VERSION}-devel+${SHA}
else
# Git is dirty; tag as VERSION-devel+wip.
echo ${VERSION}-devel+wip
fi

0 comments on commit b21cc1f

Please sign in to comment.