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 | } |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * Import gpg key |
| 158 | * |
| 159 | * @param privateKeyCredId Public key jenkins credential id |
| 160 | */ |
| 161 | def importGpgKey(privateKeyCredId) |
| 162 | { |
| 163 | def common = new com.mirantis.mk.Common() |
| 164 | def workspace = common.getWorkspace() |
| 165 | def privKey = common.getCredentials(privateKeyCredId, "key") |
| 166 | def private_key = privKey.privateKeySource.privateKey |
chnyda | 9ee49a4 | 2017-03-15 09:31:33 +0100 | [diff] [blame] | 167 | writeFile file:"${workspace}/private.key", text: private_key |
chnyda | 96f974d | 2017-03-15 18:01:15 +0100 | [diff] [blame] | 168 | sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key") |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * upload source package to launchpad |
| 173 | * |
| 174 | * @param ppaRepo ppa repository on launchpad |
| 175 | * @param dirPath repository containing the source packages |
| 176 | */ |
| 177 | |
| 178 | def uploadPpa(ppaRepo, dirPath, privateKeyCredId) { |
| 179 | |
| 180 | def common = new com.mirantis.mk.Common() |
| 181 | def workspace = common.getWorkspace() |
| 182 | def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username |
| 183 | |
| 184 | dir(dirPath) |
| 185 | { |
| 186 | def images = findFiles(glob: "*.orig*.tar.gz") |
| 187 | for (int i = 0; i < images.size(); ++i) { |
| 188 | def name = images[i].getName() |
| 189 | def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0) |
| 190 | def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0) |
| 191 | def orig_md5 = common.cutOrDie("md5sum ${name}", 0) |
| 192 | def orig_size = common.cutOrDie("ls -l ${name}", 4) |
| 193 | |
| 194 | def retval = sh(script: "wget --quiet -O orig-tmp https://launchpad.net/ubuntu/", returnStatus: true) |
| 195 | if (retval == 0) { |
| 196 | sh("mv orig-tmp ${name}") |
| 197 | def new_sha1 = common.cutOrDie("sha1sum ${name}", 0) |
| 198 | def new_sha256 = common.cutOrDie("sha256sum ${name}", 0) |
| 199 | def new_md5 = common.cutOrDie("md5sum ${name}", 0) |
| 200 | def new_size = common.cutOrDie("ls -l ${name}", 4) |
| 201 | |
| 202 | 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") |
| 203 | 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 *.changes") |
| 204 | } |
| 205 | |
chnyda | 85afa50 | 2017-03-16 17:09:13 +0100 | [diff] [blame] | 206 | sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *.changes") |
chnyda | a22760d | 2017-03-17 15:01:16 +0100 | [diff] [blame^] | 207 | sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *.changes") |
chnyda | 4e5ac79 | 2017-03-14 15:24:18 +0100 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |