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() |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 25 | def jenkinsUID = common.getJenkinsUid() |
| 26 | def jenkinsGID = common.getJenkinsGid() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 27 | def pkg = file.split('/')[-1].split('_')[0] |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 28 | def dockerLib = new com.mirantis.mk.Docker() |
| 29 | def imageArray = image.split(":") |
| 30 | def os = imageArray[0] |
| 31 | def dist = imageArray[1] |
| 32 | def img = dockerLib.getImage("debian-build-${os}-${dist}", image) |
| 33 | def workspace = common.getWorkspace() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 34 | |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 35 | img.inside("-v ${workspace}:${workspace} -w ${workspace} --privileged" ) { |
| 36 | sh("""which eatmydata || (apt-get update && apt-get install -y eatmydata) && |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 37 | export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata && |
| 38 | export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so && |
| 39 | [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list && |
| 40 | [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || ( |
| 41 | which curl || (apt-get update && apt-get install -y curl) && |
| 42 | curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add - |
| 43 | ) && |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 44 | apt-get update && apt-get install -y build-essential devscripts equivs sudo && |
| 45 | groupadd -g ${jenkinsGID} jenkins && |
| 46 | useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins && |
Jakub Josef | 6bebf16 | 2017-05-10 14:21:00 +0200 | [diff] [blame] | 47 | [ ! -f pre_build_script.sh ] || bash ./pre_build_script.sh && |
Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 48 | sudo -H -E -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} && |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 49 | mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 50 | sudo -H -E -u jenkins debuild --no-lintian -uc -us -b""") |
| 51 | } |
| 52 | |
| 53 | |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Build source package from directory |
| 58 | * |
| 59 | * @param dir Tree to build |
| 60 | * @param image Image name to use for build (default debian:sid) |
| 61 | * @param snapshot Generate snapshot version (default false) |
| 62 | */ |
| 63 | def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") { |
| 64 | def isGit |
| 65 | try { |
| 66 | sh("test -d ${dir}/.git") |
| 67 | isGit = true |
| 68 | } catch (Exception e) { |
| 69 | isGit = false |
| 70 | } |
| 71 | |
| 72 | if (isGit == true) { |
| 73 | buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix) |
| 74 | } else { |
| 75 | buildSourceUscan(dir, image) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Build source package, fetching upstream code using uscan |
| 81 | * |
| 82 | * @param dir Tree to build |
| 83 | * @param image Image name to use for build (default debian:sid) |
| 84 | */ |
| 85 | def buildSourceUscan(dir, image="debian:sid") { |
| 86 | def common = new com.mirantis.mk.Common() |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 87 | def dockerLib = new com.mirantis.mk.Docker() |
| 88 | def imageArray = image.split(":") |
| 89 | def os = imageArray[0] |
| 90 | def dist = imageArray[1] |
| 91 | def img = dockerLib.getImage("debian-build-${os}-${dist}", image) |
| 92 | def workspace = common.getWorkspace() |
| 93 | |
| 94 | img.inside("-v ${workspace}:${workspace} -w ${workspace} --privileged" ) { |
| 95 | sh("""apt-get update && apt-get install -y build-essential devscripts && |
| 96 | cd ${dir} && uscan --download-current-version && |
| 97 | dpkg-buildpackage -S -nc -uc -us""") |
| 98 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Build source package using git-buildpackage |
| 103 | * |
| 104 | * @param dir Tree to build |
| 105 | * @param image Image name to use for build (default debian:sid) |
| 106 | * @param snapshot Generate snapshot version (default false) |
| 107 | */ |
Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 108 | def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitName='Jenkins', gitEmail='jenkins@dummy.org', revisionPostfix="") { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 109 | def common = new com.mirantis.mk.Common() |
Filip Pytloun | 81c864d | 2017-03-21 15:19:30 +0100 | [diff] [blame] | 110 | def jenkinsUID = common.getJenkinsUid() |
| 111 | def jenkinsGID = common.getJenkinsGid() |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 112 | |
| 113 | if (! revisionPostfix) { |
| 114 | revisionPostfix = "" |
| 115 | } |
| 116 | |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 117 | def workspace = common.getWorkspace() |
| 118 | def dockerLib = new com.mirantis.mk.Docker() |
| 119 | def imageArray = image.split(":") |
| 120 | def os = imageArray[0] |
| 121 | def dist = imageArray[1] |
| 122 | def img = dockerLib.getImage("debian-build-${os}-${dist}", image) |
| 123 | |
| 124 | img.inside("-v ${workspace}:${workspace} -w ${workspace} --privileged") { |
| 125 | |
| 126 | withEnv(["DEBIAN_FRONTEND=noninteractive", "DEBFULLNAME='${gitName}'", "DEBEMAIL='${gitEmail}'"]) { |
| 127 | sh("""which eatmydata || (apt-get update && apt-get install -y eatmydata) && |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 128 | export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata && |
| 129 | export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so && |
Jakub Josef | 5de05f6 | 2017-05-30 15:25:55 +0200 | [diff] [blame] | 130 | apt-get update && apt-get install -y build-essential git-buildpackage dpkg-dev sudo && |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 131 | groupadd -g ${jenkinsGID} jenkins && |
| 132 | useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins && |
| 133 | cd ${dir} && |
Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 134 | sudo -H -E -u jenkins git config --global user.name "${gitName}" && |
| 135 | sudo -H -E -u jenkins git config --global user.email "${gitEmail}" && |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 136 | [[ "${snapshot}" == "false" ]] || ( |
| 137 | VERSION=`dpkg-parsechangelog --count 1 | grep Version: | sed "s,Version: ,,g"` && |
| 138 | UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` && |
| 139 | REVISION=`echo \$VERSION | cut -d "-" -f 2` && |
| 140 | TIMESTAMP=`date +%Y%m%d%H%M` && |
| 141 | if [[ "`cat debian/source/format`" = *quilt* ]]; then |
| 142 | UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` && |
| 143 | UPSTREAM_REV=`git rev-parse --short origin/\$UPSTREAM_BRANCH` && |
| 144 | NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" && |
| 145 | NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix && |
| 146 | echo "Generating new upstream version \$NEW_UPSTREAM_VERSION" && |
Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 147 | sudo -H -E -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH && |
| 148 | sudo -H -E -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 149 | else |
| 150 | NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix |
| 151 | fi && |
Filip Pytloun | ff82fc0 | 2017-03-27 12:17:05 +0200 | [diff] [blame] | 152 | sudo -H -E -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution && |
| 153 | sudo -H -E -u jenkins git add -u debian/changelog && |
| 154 | sudo -H -E -u jenkins git commit -m "New snapshot version \$NEW_VERSION" |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 155 | ) && |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 156 | sudo -H -E -u jenkins gbp buildpackage -nc --git-force-create --git-notify=false --git-ignore-branch --git-ignore-new --git-verbose --git-export-dir=../build-area -sa -S -uc -us """) |
| 157 | } |
| 158 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Run lintian checks |
| 163 | * |
| 164 | * @param changes Changes file to test against |
| 165 | * @param profile Lintian profile to use (default debian) |
| 166 | * @param image Image name to use for build (default debian:sid) |
| 167 | */ |
| 168 | def runLintian(changes, profile="debian", image="debian:sid") { |
| 169 | def common = new com.mirantis.mk.Common() |
chnyda | 1cf6f0d | 2017-06-02 11:01:04 +0200 | [diff] [blame^] | 170 | def workspace = common.getWorkspace() |
| 171 | def dockerLib = new com.mirantis.mk.Docker() |
| 172 | def imageArray = image.split(":") |
| 173 | def os = imageArray[0] |
| 174 | def dist = imageArray[1] |
| 175 | def img = dockerLib.getImage("debian-build-${os}-${dist}", image) |
| 176 | img.inside("-v ${workspace}:${workspace} -w ${workspace} --privileged") { |
| 177 | sh("""apt-get update && apt-get install -y lintian && |
| 178 | lintian -Ii -E --pedantic --profile=${profile} ${changes}""") |
| 179 | } |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 180 | } |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * Import gpg key |
| 184 | * |
| 185 | * @param privateKeyCredId Public key jenkins credential id |
| 186 | */ |
| 187 | def importGpgKey(privateKeyCredId) |
| 188 | { |
| 189 | def common = new com.mirantis.mk.Common() |
| 190 | def workspace = common.getWorkspace() |
| 191 | def privKey = common.getCredentials(privateKeyCredId, "key") |
| 192 | def private_key = privKey.privateKeySource.privateKey |
chnyda | c684645 | 2017-03-21 16:50:43 +0100 | [diff] [blame] | 193 | def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username |
| 194 | def retval = sh(script: "export GNUPGHOME=${workspace}/.gnupg; gpg --list-secret-keys | grep ${gpg_key_id}", returnStatus: true) |
| 195 | if (retval) { |
| 196 | writeFile file:"${workspace}/private.key", text: private_key |
| 197 | sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key") |
| 198 | } |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
| 202 | * upload source package to launchpad |
| 203 | * |
| 204 | * @param ppaRepo ppa repository on launchpad |
| 205 | * @param dirPath repository containing the source packages |
| 206 | */ |
| 207 | |
| 208 | def uploadPpa(ppaRepo, dirPath, privateKeyCredId) { |
| 209 | |
| 210 | def common = new com.mirantis.mk.Common() |
| 211 | def workspace = common.getWorkspace() |
| 212 | def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username |
| 213 | |
| 214 | dir(dirPath) |
| 215 | { |
| 216 | def images = findFiles(glob: "*.orig*.tar.gz") |
| 217 | for (int i = 0; i < images.size(); ++i) { |
| 218 | def name = images[i].getName() |
| 219 | def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0) |
| 220 | def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0) |
| 221 | def orig_md5 = common.cutOrDie("md5sum ${name}", 0) |
| 222 | def orig_size = common.cutOrDie("ls -l ${name}", 4) |
| 223 | |
chnyda | 5d1e97f | 2017-03-17 15:53:47 +0100 | [diff] [blame] | 224 | def retval = sh(script: "wget --quiet -O orig-tmp https://launchpad.net/ubuntu/+archive/primary/+files/${name}", returnStatus: true) |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 225 | if (retval == 0) { |
| 226 | sh("mv orig-tmp ${name}") |
| 227 | def new_sha1 = common.cutOrDie("sha1sum ${name}", 0) |
| 228 | def new_sha256 = common.cutOrDie("sha256sum ${name}", 0) |
| 229 | def new_md5 = common.cutOrDie("md5sum ${name}", 0) |
| 230 | def new_size = common.cutOrDie("ls -l ${name}", 4) |
| 231 | |
| 232 | sh("sed -i -e s,$orig_sha1,$new_sha1,g -e s,$orig_sha256,$new_sha256,g -e s,$orig_size,$new_size,g -e s,$orig_md5,$new_md5,g *.dsc") |
chnyda | 3c93ff6 | 2017-03-23 10:11:36 +0100 | [diff] [blame] | 233 | sh("sed -i -e s,$orig_sha1,$new_sha1,g -e s,$orig_sha256,$new_sha256,g -e s,$orig_size,$new_size,g -e s,$orig_md5,$new_md5,g *_source.changes") |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 234 | } |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 235 | } |
chnyda | 34e5c94 | 2017-03-22 18:06:06 +0100 | [diff] [blame] | 236 | sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *_source.changes") |
| 237 | sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *_source.changes") |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 238 | } |
Filip Pytloun | fbbd168 | 2017-03-17 22:48:04 +0100 | [diff] [blame] | 239 | } |