| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
if [ -z ${JENKINS_DEBIAN_GLUE_QUIET:-} ]; then |
| 4 |
set -x |
| 5 |
fi |
| 6 |
set -e |
| 7 |
set -u |
| 8 |
|
| 9 |
export LC_ALL=C |
| 10 |
export LANG=C |
| 11 |
|
| 12 |
if [ -r /etc/jenkins/debian_glue ] ; then |
| 13 |
. /etc/jenkins/debian_glue |
| 14 |
fi |
| 15 |
|
| 16 |
[ -n "${DEBEMAIL:-}" ] || DEBEMAIL="jenkins-debian-glue Autobuilder <jenkins@`hostname -f`>" |
| 17 |
export DEBEMAIL |
| 18 |
|
| 19 |
if [ ! -d source ] ; then |
| 20 |
echo "Please run the script in the jenkins workspace." >&2 |
| 21 |
echo 'NOTE: Make sure to set the "Local module directory (optional)" option in the Subversion configuration to "source"' >&2 |
| 22 |
exit 1 |
| 23 |
fi |
| 24 |
|
| 25 |
if [ -z "${BUILD_NUMBER:-}" ] ; then |
| 26 |
echo "No BUILD_NUMBER defined, please run it in jenkins." >&2 |
| 27 |
exit 1 |
| 28 |
fi |
| 29 |
|
| 30 |
if [ -z "${SVN_REVISION:-}" ] ; then |
| 31 |
echo "No SVN_REVISION defined, please run it with a subversion repository." >&2 |
| 32 |
exit 1 |
| 33 |
fi |
| 34 |
|
| 35 |
if ! [ -x "$(which svn2cl)" ] ; then |
| 36 |
echo "Error: svn2cl not available, please install subversion-tools <= 1.6.18dfsg-1 or svn2cl." >&2 |
| 37 |
exit 1 |
| 38 |
fi |
| 39 |
|
| 40 |
if ! [ -x "$(which svn)" ] ; then |
| 41 |
echo "Error: svn not available, please install subversion." >&2 |
| 42 |
exit 1 |
| 43 |
fi |
| 44 |
|
| 45 |
if ! [ -x "$(which dpkg-parsechangelog)" ] ; then |
| 46 |
echo "Error: dpkg-parsechangelog not available, please install dpkg-dev." >&2 |
| 47 |
exit 1 |
| 48 |
fi |
| 49 |
|
| 50 |
JENKINS_DEBIAN_GLUE_VERSION=$(dpkg --list jenkins-debian-glue 2>/dev/null | awk '/^ii/ {print $3}') |
| 51 |
if [ -n "${JENKINS_DEBIAN_GLUE_VERSION:-}" ] ; then |
| 52 |
echo "*** Running jenkins-debian-glue version $JENKINS_DEBIAN_GLUE_VERSION ***" |
| 53 |
fi |
| 54 |
|
| 55 |
echo "*** source package build phase ***" |
| 56 |
rm -f ./* || true |
| 57 |
|
| 58 |
ORIG_DIR=$(pwd) |
| 59 |
cd source/${branch:-} |
| 60 |
|
| 61 |
if ! [ -r debian/changelog ] ; then |
| 62 |
echo "Error: could not find debian/changelog (not a Debian package or wrong tag/branch?)" >&2 |
| 63 |
exit 1 |
| 64 |
fi |
| 65 |
|
| 66 |
# Force the upgrade if subversion 1.7 (which introduced the upgrade command) or |
| 67 |
# later is available. Otherwise, if the system has been upgraded, the 'svn info' |
| 68 |
# will fail with an unhelpful error message |
| 69 |
SVN_VERSION="$(svn --version | awk '/, version / {print $3}')" |
| 70 |
if dpkg --compare-versions "$SVN_VERSION" ge 1.7 ; then |
| 71 |
echo "*** Executing 'svn upgrade' to make sure svn is up2date ***" |
| 72 |
svn upgrade |
| 73 |
fi |
| 74 |
|
| 75 |
SINCE_REVISION=$(svn info debian/changelog 2>/dev/null | awk '/Last Changed Rev:/ {print $4}') |
| 76 |
|
| 77 |
if [ -z "${SINCE_REVISION:-}" ] ; then |
| 78 |
echo "Error: could not detect svn revision which modified debian/changelog." >&2 |
| 79 |
exit 1 |
| 80 |
fi |
| 81 |
|
| 82 |
# required for dpkg-parsechangelog |
| 83 |
if ! [ -r debian/changelog ] ; then |
| 84 |
echo "Error: debian/changelog could not be read. Is this really a Debian package?" >&2 |
| 85 |
exit 1 |
| 86 |
fi |
| 87 |
|
| 88 |
# package name |
| 89 |
PACKAGE=$(dpkg-parsechangelog --count 1 | awk '/^Source: / {print $2}') |
| 90 |
|
| 91 |
# get newest version number from changelog |
| 92 |
PREVIOUS_VERSION=$(dpkg-parsechangelog --count 1 | awk '/^Version: / {print $2}') |
| 93 |
|
| 94 |
build_snapshot() { |
| 95 |
|
| 96 |
DISTRIBUTION=$(dpkg-parsechangelog --count 1 | awk '/^Distribution/ {print $2}') |
| 97 |
|
| 98 |
if [ -n "${TIMESTAMP_FORMAT:-}" ] ; then |
| 99 |
echo "*** Using custom timestamp format as specified by TIMESTAMP_FORMAT ***" |
| 100 |
TIMESTAMP="$(date -u +"$TIMESTAMP_FORMAT")" |
| 101 |
else |
| 102 |
TIMESTAMP="$(date -u +%Y%m%d%H%M%S)" |
| 103 |
fi |
| 104 |
|
| 105 |
if [ "$DISTRIBUTION" = "UNRELEASED" ] ; then |
| 106 |
# we do NOT raise the version number, if we detect an unreleased version |
| 107 |
SNAPSHOT_VERSION="${PREVIOUS_VERSION}~${TIMESTAMP}.svn${SVN_REVISION}.${BUILD_NUMBER}" |
| 108 |
else |
| 109 |
# calculate new snapshot version |
| 110 |
SNAPSHOT_VERSION="$(jdg-increase-version-number "$PREVIOUS_VERSION")~${TIMESTAMP}.svn${SVN_REVISION}.${BUILD_NUMBER}" |
| 111 |
fi |
| 112 |
|
| 113 |
# generate changelog |
| 114 |
SVN_CHANGELOG_ORIG=$(mktemp) |
| 115 |
SVN_CHANGELOG_EDIT=$(mktemp) |
| 116 |
# build GNU like changelog with svn2cl as helper tool |
| 117 |
# timeout after 15 seconds without progress, we might be running into e.g. |
| 118 |
# "xsltApplyXSLTTemplate: A potential infinite template recursion was detected." |
| 119 |
if ! timeout -k 16 16 svn2cl --reparagraph --stdout --include-rev -r HEAD:$SINCE_REVISION > "$SVN_CHANGELOG_ORIG" ; then |
| 120 |
echo ' * Sorry, generating changelog using svn2cl failed. :( |
| 121 |
Make sure the svn2cl command works on the jenkins host. |
| 122 |
For example validating server certificates is a common error. |
| 123 |
' > "$SVN_CHANGELOG_ORIG" |
| 124 |
fi |
| 125 |
|
| 126 |
## disclaimer: use separate sed calls to make it easier to adjust if necessary |
| 127 |
# reduce changelog to just the first line of the commit message |
| 128 |
sed -n '/^\t*\s*\* \[r/,/^\t*\s*$/p' "$SVN_CHANGELOG_ORIG" > "$SVN_CHANGELOG_EDIT" |
| 129 |
|
| 130 |
# get rid of author information that might be present from git-svn users |
| 131 |
sed -i '/^\t\s\sFrom: /d' "$SVN_CHANGELOG_EDIT" |
| 132 |
|
| 133 |
# replace tabs at beginning of lines with two spaces |
| 134 |
sed -i 's/^\t/ /' "$SVN_CHANGELOG_EDIT" |
| 135 |
|
| 136 |
# get rid of lines with just 4 space chars only |
| 137 |
sed -i '/^ $/d' "$SVN_CHANGELOG_EDIT" |
| 138 |
|
| 139 |
# get rid of empty lines |
| 140 |
sed -i '/^$/d' "$SVN_CHANGELOG_EDIT" |
| 141 |
|
| 142 |
# get rid of of the timestamp lines, looking like: |
| 143 |
# 2011-12-02 21:25 mprokop |
| 144 |
sed -ri '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}/d' "$SVN_CHANGELOG_EDIT" |
| 145 |
|
| 146 |
cat > debian/changelog.new << EOF |
| 147 |
$PACKAGE ($SNAPSHOT_VERSION) UNRELEASED; urgency=low |
| 148 |
|
| 149 |
** SNAPSHOT build ** |
| 150 |
|
| 151 |
EOF |
| 152 |
|
| 153 |
cat "$SVN_CHANGELOG_EDIT" >> debian/changelog.new |
| 154 |
|
| 155 |
cat >> debian/changelog.new << EOF |
| 156 |
|
| 157 |
-- $DEBEMAIL $(date -u -R) |
| 158 |
|
| 159 |
EOF |
| 160 |
|
| 161 |
# append original changelog |
| 162 |
cat debian/changelog >> debian/changelog.new |
| 163 |
|
| 164 |
# finally install new changelog |
| 165 |
mv debian/changelog.new debian/changelog |
| 166 |
rm -f "$SVN_CHANGELOG_ORIG" "$SVN_CHANGELOG_EDIT" |
| 167 |
} |
| 168 |
|
| 169 |
case "${branch:-}" in |
| 170 |
tags/*) echo "Building a tag version, not modifying the package" ;; |
| 171 |
*) build_snapshot;; |
| 172 |
esac |
| 173 |
|
| 174 |
cd $ORIG_DIR |
| 175 |
|
| 176 |
# subversion >=1.9 fails hard, see github issue #164 |
| 177 |
debian_only="$(svn propget mergeWithUpstream source/${branch:-}/debian || true)" |
| 178 |
|
| 179 |
if [ -n "${SBP_OPTS:-}" ] ; then |
| 180 |
echo "*** Found environment variable SBP_OPTS, set to ${SBP_OPTS} ***" |
| 181 |
else |
| 182 |
echo "*** Using svn-buildpackage default options provided by jenkins-debian-glue ***" |
| 183 |
SBP_OPTS="-nc --svn-download-orig -S --svn-builder dpkg-buildpackage -d --svn-move-to=${ORIG_DIR} --svn-dont-purge -uc -us --svn-ignore-new -rfakeroot" |
| 184 |
fi |
| 185 |
|
| 186 |
if [ "${debian_only:-}" = "1" ] ; then |
| 187 |
( |
| 188 |
cd "source/${branch:-}" |
| 189 |
echo "mergeWithUpstream detected, using svn-buildpackage to create source package" |
| 190 |
svn-buildpackage ${SBP_OPTS:-} |
| 191 |
) |
| 192 |
else |
| 193 |
if [ -n "${PRE_SOURCE_HOOK:-}" ] ; then |
| 194 |
echo "*** Found environment variable PRE_SOURCE_HOOK, set to ${PRE_SOURCE_HOOK:-} ***" |
| 195 |
sh ${PRE_SOURCE_HOOK:-} |
| 196 |
fi |
| 197 |
|
| 198 |
dpkg-source --tar-ignore=\.svn -b "source/${branch:-}" |
| 199 |
( |
| 200 |
if [ -n "${SNAPSHOT_VERSION:-}" ] ; then |
| 201 |
CHANGES_FILE="${ORIG_DIR}/${PACKAGE}_${SNAPSHOT_VERSION}_source.changes" |
| 202 |
else |
| 203 |
CHANGES_FILE="${ORIG_DIR}/${PACKAGE}_${PREVIOUS_VERSION}_source.changes" |
| 204 |
fi |
| 205 |
|
| 206 |
cd "source/${branch:-}" |
| 207 |
dpkg-genchanges -S -u"${ORIG_DIR}" > "${CHANGES_FILE}" |
| 208 |
) |
| 209 |
fi |
| 210 |
|
| 211 |
if [ -n "${KEY_ID:-}" ] ; then |
| 212 |
echo "*** Found environment variable KEY_ID, set to ${KEY_ID:-}, signing source package ***" |
| 213 |
|
| 214 |
if ! [ -x "$(which debsign)" ] ; then |
| 215 |
echo "Error: debsign not available, please make sure the devscripts package is installed." >&2 |
| 216 |
exit 1 |
| 217 |
fi |
| 218 |
|
| 219 |
debsign -k"${KEY_ID:-}" *_source.changes |
| 220 |
fi |
| 221 |
|
| 222 |
# revert to original debian/changelog to avoid highly increasing version numbers with each build |
| 223 |
( cd source/${branch:-} ; svn revert debian/changelog ) |
| 224 |
|
| 225 |
# vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2 |