Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Debian functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | def cleanup(image="debian:sid") { |
| 10 | def common = new com.mirantis.mk.Common() |
| 11 | def img = docker.image(image) |
| 12 | |
| 13 | workspace = common.getWorkspace() |
| 14 | sh("docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c 'rm -rf build-area || true'") |
| 15 | } |
| 16 | |
| 17 | /* |
| 18 | * Build binary Debian package from existing dsc |
| 19 | * |
| 20 | * @param file dsc file to build |
| 21 | * @param image Image name to use for build (default debian:sid) |
| 22 | */ |
| 23 | def buildBinary(file, image="debian:sid", extraRepoUrl=null, extraRepoKeyUrl=null) { |
| 24 | def common = new com.mirantis.mk.Common() |
| 25 | def pkg = file.split('/')[-1].split('_')[0] |
| 26 | def img = docker.image(image) |
| 27 | |
| 28 | workspace = common.getWorkspace() |
| 29 | sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c ' |
| 30 | which eatmydata || (apt-get update && apt-get install -y eatmydata) && |
| 31 | export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata && |
| 32 | export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so && |
| 33 | [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list && |
| 34 | [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || ( |
| 35 | which curl || (apt-get update && apt-get install -y curl) && |
| 36 | curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add - |
| 37 | ) && |
| 38 | apt-get update && apt-get install -y build-essential devscripts equivs && |
| 39 | dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} && |
| 40 | mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control |
| 41 | debuild --no-lintian -uc -us -b'""") |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Build source package from directory |
| 46 | * |
| 47 | * @param dir Tree to build |
| 48 | * @param image Image name to use for build (default debian:sid) |
| 49 | * @param snapshot Generate snapshot version (default false) |
| 50 | */ |
| 51 | def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") { |
| 52 | def isGit |
| 53 | try { |
| 54 | sh("test -d ${dir}/.git") |
| 55 | isGit = true |
| 56 | } catch (Exception e) { |
| 57 | isGit = false |
| 58 | } |
| 59 | |
| 60 | if (isGit == true) { |
| 61 | buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix) |
| 62 | } else { |
| 63 | buildSourceUscan(dir, image) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Build source package, fetching upstream code using uscan |
| 69 | * |
| 70 | * @param dir Tree to build |
| 71 | * @param image Image name to use for build (default debian:sid) |
| 72 | */ |
| 73 | def buildSourceUscan(dir, image="debian:sid") { |
| 74 | def common = new com.mirantis.mk.Common() |
| 75 | def img = docker.image(image) |
| 76 | workspace = common.getWorkspace() |
| 77 | sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c ' |
| 78 | apt-get update && apt-get install -y build-essential devscripts && |
| 79 | cd ${dir} && uscan --download-current-version && |
| 80 | dpkg-buildpackage -S -nc -uc -us'""") |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Build source package using git-buildpackage |
| 85 | * |
| 86 | * @param dir Tree to build |
| 87 | * @param image Image name to use for build (default debian:sid) |
| 88 | * @param snapshot Generate snapshot version (default false) |
| 89 | */ |
| 90 | def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") { |
| 91 | def common = new com.mirantis.mk.Common() |
| 92 | def jenkinsUID = sh ( |
| 93 | script: 'id -u', |
| 94 | returnStdout: true |
| 95 | ).trim() |
| 96 | def jenkinsGID = sh ( |
| 97 | script: 'id -g', |
| 98 | returnStdout: true |
| 99 | ).trim() |
| 100 | |
| 101 | if (! revisionPostfix) { |
| 102 | revisionPostfix = "" |
| 103 | } |
| 104 | |
| 105 | def img = docker.image(image) |
| 106 | workspace = common.getWorkspace() |
| 107 | sh("""docker run -e DEBIAN_FRONTEND=noninteractive -e DEBFULLNAME='${gitName}' -e DEBEMAIL='${gitEmail}' -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -exc ' |
| 108 | which eatmydata || (apt-get update && apt-get install -y eatmydata) && |
| 109 | export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata && |
| 110 | export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so && |
| 111 | apt-get update && apt-get install -y build-essential git-buildpackage sudo && |
| 112 | groupadd -g ${jenkinsGID} jenkins && |
| 113 | useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins && |
| 114 | cd ${dir} && |
| 115 | sudo -H -u jenkins git config --global user.name "${gitName}" && |
| 116 | sudo -H -u jenkins git config --global user.email "${gitEmail}" && |
| 117 | [[ "${snapshot}" == "false" ]] || ( |
| 118 | VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` && |
| 119 | UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` && |
| 120 | REVISION=`echo \$VERSION | cut -d "-" -f 2` && |
| 121 | TIMESTAMP=`date +%Y%m%d%H%M` && |
| 122 | if [[ "`cat debian/source/format`" = *quilt* ]]; then |
| 123 | UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` && |
| 124 | UPSTREAM_REV=`git rev-parse --short origin/\$UPSTREAM_BRANCH` && |
| 125 | NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" && |
| 126 | NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix && |
| 127 | echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" && |
| 128 | sudo -H -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH && |
| 129 | sudo -H -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION |
| 130 | else |
| 131 | NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix |
| 132 | fi && |
| 133 | sudo -H -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution && |
| 134 | sudo -H -u jenkins git add -u debian/changelog && |
| 135 | sudo -H -u jenkins git commit -m "New snapshot version \$NEW_VERSION" |
| 136 | ) && |
| 137 | gbp buildpackage -nc --git-force-create --git-notify=false --git-ignore-branch --git-ignore-new --git-verbose --git-export-dir=../build-area -S -uc -us'""") |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Run lintian checks |
| 142 | * |
| 143 | * @param changes Changes file to test against |
| 144 | * @param profile Lintian profile to use (default debian) |
| 145 | * @param image Image name to use for build (default debian:sid) |
| 146 | */ |
| 147 | def runLintian(changes, profile="debian", image="debian:sid") { |
| 148 | def common = new com.mirantis.mk.Common() |
| 149 | def img = docker.image(image) |
| 150 | workspace = common.getWorkspace() |
| 151 | sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c ' |
| 152 | apt-get update && apt-get install -y lintian && |
| 153 | lintian -Ii -E --pedantic --profile=${profile} ${changes}'""") |
| 154 | } |