You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jenkins-debian-glue/scripts/generate-git-snapshot

195 lines
6.0 KiB

#!/bin/sh
set -x
set -e
set -u
[ -n "${DEBEMAIL:-}" ] || DEBEMAIL="jenkins.grml.org Autobuilder <jenkins@grml.org>"
export DEBEMAIL
if [ ! -d source ] ; then
echo "Please run the script in the jenkins workspace." >&2
exit 1
fi
if [ -z "${BUILD_NUMBER:-}" ] ; then
echo "No BUILD_NUMBER defined, please run it in jenkins." >&2
exit 1
fi
if [ -z "${GIT_COMMIT:-}" ] ; then
echo "No GIT_COMMIT defined, please run it with git-plugin in jenkins. ">&2
exit 1
fi
# we want to get a version string like
# $ORIG_VERSION+0~$TIMESTAMP.$BUILD_NUMBER~1.$GIT_ID
# so the version is always increasing over time, no matter what
TIMESTAMP="$(date +%s)" # seconds since 1970-01-01 00:00:00 UTC, reversible via `date -d @$TIMESTAMP`
BUILD_VERSION="${TIMESTAMP}.${BUILD_NUMBER}" # git-dch appends something like ~1.gbp5f433e then
echo "*** source package build phase ***"
rm -f ./* || true
cd source
# version number
ORIG_VERSION=$(dpkg-parsechangelog --count 1 | awk '/Version/ {print $2}')
# we do NOT raise the version number, if we detect an unreleased version
DISTRIBUTION=$(dpkg-parsechangelog --count 1 | awk '/^Distribution/ {print $2}')
if [ "$DISTRIBUTION" = "UNRELEASED" ] ; then
echo "*** Not increasing version number as distribution is set to UNRELEASED ***"
INCREASED_VERSION="$ORIG_VERSION"
else
INCREASED_VERSION=$(increase-version-number $ORIG_VERSION)
fi
VERSION_STRING="${INCREASED_VERSION}~${BUILD_VERSION}"
if [ -n "${distribution:-}" ] ; then
echo "Distribution variable found. Adding distribution specific version."
VERSION_STRING="${VERSION_STRING}+${distribution}"
fi
# support overriding git-dch options
if [ -n "${DCH_OPTS:-}" ] ; then
echo "Found environment variable DCH_OPTS, set to ${DCH_OPTS}"
else
DCH_OPTS="-S --multimaint-merge --ignore-branch"
echo "Using default git-dch options (${DCH_OPTS})"
fi
# support overriding extra options for git-dch
if [ -n "${DCH_EXTRA_OPTS:-}" ] ; then
echo "Found environment variable DCH_EXTRA_OPTS, set to ${DCH_EXTRA_OPTS}"
else
DCH_EXTRA_OPTS="--new-version=${VERSION_STRING}"
echo "Using extra git-dch options (${DCH_EXTRA_OPTS})"
fi
create_local_branch() {
[ -n "${1:-}" ] || return 1
local BRANCH="$1"
if git branch -a | grep -q "remotes/origin/${BRANCH}" ; then
git branch -D "${BRANCH}" || true
git branch "${BRANCH}" "remotes/origin/${BRANCH}"
else
echo "NOTE: branch $BRANCH does not exist, ignoring request to checkout therefore."
fi
}
identify_latest_change() {
# debian/changelog might be a symlink (e.g. because debian points to
# pkg/debian), so make sure we don't access a non-existing file
git checkout -- $(readlink -f debian/changelog)
local OLD_VERSION=$(dpkg-parsechangelog | awk '/^Version: / {print $2}')
local tag=$(git describe $(git rev-list --tags='[^ju]*' --max-count=1 HEAD))
local last_merge=$(git describe $(git rev-list --all --merges --max-count=1 HEAD))
local since=${tag}
if [ -n "$last_merge" ] ; then
local m_date=$(git log ${last_merge} --pretty="format:%at" -1)
local t_date=$(git log ${tag} --pretty="format:%at" -1)
if [ ${m_date} -gt ${t_date} ] ; then
local since=${last_merge}
fi
fi
echo "Last tag / merge seems to be $since"
git-dch -s "${since}" $DCH_OPTS $DCH_EXTRA_OPTS
local NEW_VERSION=$(dpkg-parsechangelog | awk '/^Version: / {print $2}')
if dpkg --compare-versions "$NEW_VERSION" lt "$OLD_VERSION" ; then
echo "Calculated version is older than last release, falling back to auto mode."
# debian/changelog might be a symlink (e.g. because debian points to
# pkg/debian), so make sure we don't access a non-existing file
git checkout -- $(readlink -f debian/changelog)
git-dch --auto $DCH_OPTS $DCH_EXTRA_OPTS
fi
}
git_dch_auto() {
git-dch --auto $DCH_OPTS $DCH_EXTRA_OPTS
}
git_tag_build() {
git checkout "${tag}"
if [ -n "${distribution:-}" ] ; then
echo "Distribution found. Adding distribution specific version (${distribution})."
dch -b -v "${VERSION_STRING}+${distribution:-}" \
"Tagged autobuild for ${distribution} via jenkins-debian-glue."
else
dch -v "${VERSION_STRING}" \
"Tagged autobuild via jenkins-debian-glue."
fi
}
# Clean up any unexpected local changes
#git reset --hard HEAD
# make sure common branches are available for git-buildpackage
create_local_branch upstream
create_local_branch debian
create_local_branch pristine-tar
# Drop residual files
#git clean -xfd
if [ -n "${branch:-}" ] ; then
echo "Branch parameter set, checking out $branch"
git checkout "${branch}"
else
echo "No branch parameter set, checking out $GIT_COMMIT"
git checkout "$GIT_COMMIT"
fi
if [ -n "${tag:-}" ] ; then
echo "Tag parameter found, using dch for changelog generation."
git_tag_build
elif [ "${1:-}" = "auto" ] ; then
echo "Using git-dch with auto mode."
git_dch_auto
elif [ -r debian/gbp.conf ] ; then
echo "Found debian/gbp.conf, using git-dch with auto mode."
git_dch_auto
else
echo "Trying to identify latest tag / merge..."
if ! git describe $(git rev-list --tags='[^ju]*' --max-count=1 HEAD) >/dev/null ; then
echo "Failed to identify latest change, falling back to auto mode."
git_dch_auto
else
identify_latest_change
fi
fi
# get rid of "UNRELEASED" distribution header
debchange --release ""
# prepare orig.tar.gz using pristine-tar, but without actually building the source package
if [ "${IGNORE_GIT_BUILDPACKAGE:-}" = "true" ] ; then
echo "Skipping git-buildpackage execution as requested via IGNORE_GIT_BUILDPACKAGE ***"
else
git-buildpackage -nc --git-force-create --git-ignore-new -S -us -uc --git-verbose --git-builder=/bin/true --git-cleaner=/bin/true
fi
# build source package
( cd .. ; dpkg-source -i\.git -b source )
# revert to original debian/changelog to avoid merge conflicts
git checkout -- debian/changelog
# needed for deploying artifacts
mkdir -p ${JENKINS_HOME}/userContent/${JOB_NAME}/
echo "Cleaning up ${JENKINS_HOME}/userContent/${JOB_NAME} to get rid of possibly outdated data"
rm -f "${JENKINS_HOME}/userContent/${JOB_NAME}/"*
# vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2