Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Created May 6, 2022 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongkongkiwi/3b5c51c9dc56609823547217d2a8e837 to your computer and use it in GitHub Desktop.
Save hongkongkiwi/3b5c51c9dc56609823547217d2a8e837 to your computer and use it in GitHub Desktop.
Grab a version nicely from Gitlab (works in CI or non-CI)
# Use CI_COMMIT_TAG if it exists, otherwise lookup for a pkgversion file
PKGVERSION="$CI_COMMIT_TAG:-"$(head -n1 "$CI_PROJECT_DIR/pkgversion" 2>/dev/null)"}"
if [ -z "$PKGVERSION" ]; then
cd "${CI_PROJECT_DIR:-"."}"
# Check if we can find a tag otherwise specifically fetch tags
git describe --tags >/dev/null 2>&1 || git fetch --tags
# Set our package version correctly
PKGVERSION="$(git describe --tags 2>/dev/null)"
# If our package version couldn't be set this way then use Gitlab CI
if [ -z "$PKGVERSION" -a -n "$CI_COMMIT_SHORT_SHA" -a -n "$CI_PIPELINE_IID" -a -n "$CI_JOB_TOKEN" ]; then
# get most recent tag via gitlab api (if any)
PKGVERSION="$LAST_TAG-$CI_PIPELINE_IID-g$CI_COMMIT_SHORT_SHA"
fi
fi
# Set to UNKNOWN if string is empty (e.g. all the methods above failed)
PKGVERSION="${PKGVERSION:-"UNKNOWN"}"
# Save it so we can save time in future lookups
[ "$PKGVERSION" != "UNKNOWN" ] && echo "$PKGVERSION" > "$CI_PROJECT_DIR/pkgversion"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment