Trigger build of Debian package for Git Jenkins CI

#!/bin/bash
#
# This is a helper script to trigger the build of a debian package for a
# project which is in Git SCM and integrated via Jenkins CI
#
# The trigger used for the package build is a string "bump for release"
# which is part of the commit message for the Git repository.
#
# The Changelog is then parsed to obtain the release version and then
# the repository is exported for package building
#
# Author: Jeffery Fernandez <jeffery@fernandez.net.au>
#
# Requires the following Debian packages:
# dpkg-dev debhelper devscripts fakeroot lintian
##

# Extract the last changelog and discard unwanted data
RELEASE_BUMP=`git log --max-count=1 --pretty -- . | grep -v "^Author: " | grep -v "^Date: " | grep -v "^Merge: " | grep -v "^commit"`

# Very if a release was requested
echo $RELEASE_BUMP | grep -i "bump for release" > /dev/null
if [ $? -eq 1 ]
then
echo "Release request not Found" && exit 0;
fi

# Make sure we have a Job name ( Comes from Jenkins CI)
if [ -z "$JOB_NAME" ]
then
echo "Job Name not provided" && exit 1;
fi

# The PWD needs to be the parent to the debian folder inorder to extract the Version to package
VERSION=`dpkg-parsechangelog | awk '/^Version/ {print $2}'`

# Path for building the Debian Package
PACKAGE_PATH="${JOB_NAME}-${VERSION}"

# Obtain the last Git Revision Hash
HASH=`git rev-parse HEAD`

echo "Building: ${VERSION} from Commit: ${HASH}"

# Clean up old Build path, if it exists
if [ -d "${PACKAGE_PATH}" ]
then
rm -fR "${PACKAGE_PATH}"
fi

# Make sure the Exporting directory exists
mkdir -p "${PACKAGE_PATH}"

# Export the revision and Unarchive it into a build folder
git archive ${HASH} | ( cd "${PACKAGE_PATH}" && tar -xf - ) || {
echo "Exporting Git Repository Failed"; exit 1;
}

# Remove any Build Config file, as we don't want to run tests anymore
if [ -f "${PACKAGE_PATH}/build.xml" ]
then
rm "${PACKAGE_PATH}/build.xml"
fi

# Build Package
cd "${PACKAGE_PATH}" && debuild -i -us -uc

This entry was posted in BASh, Continuous Integration, Debian, Git, Jenkins, Packaging, SCM, Software. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">