public
Created

Trigger the build of a debian package for a Git SCM project via Jenkins CI

  • Download Gist
Debianize-Git-Jenkins
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.