Calculate VERSION and RELEASE dynamically from git

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2024-03-28 21:47:41 -06:00
parent ed529c5e0c
commit f07401ce41
6 changed files with 113 additions and 12 deletions

View File

@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump github.com/go-jose/go-jose/v3 to 3.0.3
- Bump gopkg.in/go-jose/go-jose.v2 to 2.6.3
- Bump github.com/opencontainers/runc to 1.1.12
- Dynamically calculate version and release from Git. #1162
### Fixed

View File

@@ -150,6 +150,7 @@ dist:
rm -rf .dist/ $(WAREWULF)-$(VERSION).tar.gz
mkdir -p .dist/$(WAREWULF)-$(VERSION)
rsync -a --exclude=".github" --exclude=".vscode" --exclude "*~" * .dist/$(WAREWULF)-$(VERSION)/
scripts/get-version.sh >.dist/$(WAREWULF)-$(VERSION)/VERSION
cd .dist; tar -czf ../$(WAREWULF)-$(VERSION).tar.gz $(WAREWULF)-$(VERSION)
rm -rf .dist

View File

@@ -13,18 +13,8 @@ VARLIST := OS
# Project Information
VARLIST += WAREWULF VERSION RELEASE
WAREWULF ?= warewulf
VERSION ?= 4.5.x
RELEASE_INCREMENT ?= 1
GIT_HASH := $(shell test -e .git && git log -1 --format="%h")
GIT_TAG := $(shell test -e .git && git describe --tags | head -n1)
ifeq ($(GIT_TAG),v$(VERSION))
RELEASE ?= $(RELEASE_INCREMENT)
else ifdef GIT_HASH
RELEASE ?= $(RELEASE_INCREMENT).git_$(GIT_HASH)
else
RELEASE ?= $(RELEASE_INCREMENT)
endif
VERSION ?= $(shell scripts/rpm-version.sh $$(scripts/get-version.sh))
RELEASE ?= $(shell scripts/rpm-release.sh $$(scripts/get-version.sh))
# Use LSB-compliant paths if OS is known
ifneq ($(OS),)

77
scripts/get-version.sh Executable file
View File

@@ -0,0 +1,77 @@
#!/bin/sh
# Copyright (c) Contributors to the Apptainer project, established as
# Apptainer a Series of LF Projects LLC.
# For website terms of use, trademark policy, privacy policy and other
# project policies see https://lfprojects.org/policies
# optionally take commit-ish as argument for debugging purposes
head=$1
gitrepo=true
toplevel="$(git rev-parse --show-toplevel 2> /dev/null)"
if test -z "${toplevel}" ; then
# failed to get toplevel directory, we are not inside a git
# repo?
gitrepo=false
toplevel=$PWD
fi
if test -f "${toplevel}/VERSION" ; then
cat "${toplevel}/VERSION"
exit 0
fi
# no VERSION file
if ! ${gitrepo} ; then
echo "E: Not inside a git repository and no VERSION file found. Abort." 1>&2
exit 1
fi
# gitdesc extracts the closest version tag, removing the leading "v" and
# returning the rest
gitdesc() {
git describe --match='v[0-9]*' --always --candidates=1 "$@" 2>/dev/null |
sed -e 's,^v,,'
}
closest_tag=$(gitdesc --abbrev=0 ${head})
current_commit=$(gitdesc ${head})
if test -n "${head}" ; then
# cannot pass --dirty when using commit-ish
dirty_info=${current_commit}
else
dirty_info=$(gitdesc --dirty)
fi
# First figure out if the current commit matches a tag:
#
# v3.4.1
# v3.4.1-rc.1
if [ "${closest_tag}" = "${current_commit}" ] ; then
# We are on a tag, leave it alone, maybe including the dirty
# mark.
#
# This works even if we ever use a tag like v3.4.1+pl1 (which we
# will hopefully never have).
echo "${dirty_info}"
exit 0
fi
# We have something which doesn't exactly match a tag:
#
# v3.4.1-315-g21e6d6765
# v3.4.1-rc.1-315-g21e6d6765
#
# Use the -315-g21e6d6765 part as metadata, using + to indicate that
# this is 3.4.1 with 315 additional changes. Keep the g21e6d6765 part to
# have a record of the corresponding git commit hash.
#
# Transform the above into:
#
# v3.4.1+315-g21e6d6765
# v3.4.1-rc.1+315-g21e6d6765
echo "${dirty_info}" | sed -e "s,^\(${closest_tag}\)-,\1+,"

18
scripts/rpm-release.sh Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
# translate given version to rpm equivalent
if [ $# != 1 ]; then
echo "Usage: rpm-release versionstring" >&2
exit 2
fi
# Extract release from a colon-separated suffix:
#
# 3.4.2-rc.1:2 -> 2
release=$(echo "$1" | cut -sd: -f 2)
if [ "${release}" == "" ]
then
echo 1
else
echo "${release}"
fi

14
scripts/rpm-version.sh Executable file
View File

@@ -0,0 +1,14 @@
#!/bin/bash
# translate given version to rpm equivalent
if [ $# != 1 ]; then
echo "Usage: rpm-version versionstring" >&2
exit 2
fi
# Transform version numbers so that rpm accepts them:
#
# 3.4.2-rc.1 -> 3.4.2~rc.1
# 3.4.2 -> 3.4.2
# 3.4.2+522-gee98ef356 -> 3.4.2+522.gee98ef356
echo "$1" | sed -e 's/:.*//' | sed -e 's,\(^[^+]\+\)-,\1~,; s,-,.,g'