blob: 62b552f889a001dde876324641639031e60a534a [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2
3/**
4 *
5 * Debian functions
6 *
7 */
8
9def 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 */
23def buildBinary(file, image="debian:sid", extraRepoUrl=null, extraRepoKeyUrl=null) {
24 def common = new com.mirantis.mk.Common()
Filip Pytloun81c864d2017-03-21 15:19:30 +010025 def jenkinsUID = common.getJenkinsUid()
26 def jenkinsGID = common.getJenkinsGid()
Jakub Josef79ecec32017-02-17 14:36:28 +010027 def pkg = file.split('/')[-1].split('_')[0]
chnyda1cf6f0d2017-06-02 11:01:04 +020028 def dockerLib = new com.mirantis.mk.Docker()
29 def imageArray = image.split(":")
30 def os = imageArray[0]
31 def dist = imageArray[1]
Jakub Josefda4eb702018-02-02 15:40:04 +010032 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}:latest", image)
chnyda1cf6f0d2017-06-02 11:01:04 +020033 def workspace = common.getWorkspace()
Jakub Josef13214312018-02-06 16:34:14 +010034 def debug = env.getEnvironment().containsKey("DEBUG") && env["DEBUG"].toBoolean() ? "true" : ""
Jakub Josef79ecec32017-02-17 14:36:28 +010035
chnyda8ad962e2017-06-02 12:24:15 +020036 img.inside("-u root:root" ) {
chnydaa3603b42017-06-02 12:36:08 +020037 sh("""bash -c 'cd ${workspace} && (which eatmydata || (apt-get update && apt-get install -y eatmydata)) &&
Jakub Josef13214312018-02-06 16:34:14 +010038 export DEBUG="${debug}" &&
Jakub Josef79ecec32017-02-17 14:36:28 +010039 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
40 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
41 [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list &&
42 [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || (
43 which curl || (apt-get update && apt-get install -y curl) &&
44 curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add -
45 ) &&
Filip Pytloun81c864d2017-03-21 15:19:30 +010046 apt-get update && apt-get install -y build-essential devscripts equivs sudo &&
47 groupadd -g ${jenkinsGID} jenkins &&
48 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
Filip Pytloun78b91832017-06-30 13:16:31 +020049 chown -R ${jenkinsUID}:${jenkinsGID} /home/jenkins &&
Jakub Josef6bebf162017-05-10 14:21:00 +020050 [ ! -f pre_build_script.sh ] || bash ./pre_build_script.sh &&
Filip Pytlounff82fc02017-03-27 12:17:05 +020051 sudo -H -E -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
Filip Pytloun2f7302a2017-06-30 14:59:30 +020052 mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control &&
chnydaa3603b42017-06-02 12:36:08 +020053 sudo -H -E -u jenkins debuild --no-lintian -uc -us -b'""")
chnyda1cf6f0d2017-06-02 11:01:04 +020054 }
55
56
Jakub Josef79ecec32017-02-17 14:36:28 +010057}
58
59/*
60 * Build source package from directory
61 *
62 * @param dir Tree to build
63 * @param image Image name to use for build (default debian:sid)
64 * @param snapshot Generate snapshot version (default false)
65 */
Oleg Iurchenko68e17b02018-01-02 12:24:49 +020066def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="", remote="origin/") {
Jakub Josef79ecec32017-02-17 14:36:28 +010067 def isGit
68 try {
69 sh("test -d ${dir}/.git")
70 isGit = true
71 } catch (Exception e) {
72 isGit = false
73 }
74
75 if (isGit == true) {
Oleg Iurchenko68e17b02018-01-02 12:24:49 +020076 buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix, remote)
Jakub Josef79ecec32017-02-17 14:36:28 +010077 } else {
78 buildSourceUscan(dir, image)
79 }
80}
81
82/*
83 * Build source package, fetching upstream code using uscan
84 *
85 * @param dir Tree to build
86 * @param image Image name to use for build (default debian:sid)
87 */
88def buildSourceUscan(dir, image="debian:sid") {
89 def common = new com.mirantis.mk.Common()
chnyda1cf6f0d2017-06-02 11:01:04 +020090 def dockerLib = new com.mirantis.mk.Docker()
91 def imageArray = image.split(":")
92 def os = imageArray[0]
93 def dist = imageArray[1]
Jakub Josefda4eb702018-02-02 15:40:04 +010094 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}:latest", image)
chnyda1cf6f0d2017-06-02 11:01:04 +020095 def workspace = common.getWorkspace()
96
chnyda8ad962e2017-06-02 12:24:15 +020097 img.inside("-u root:root" ) {
chnyda12f3b3f2017-06-02 12:19:38 +020098 sh("""cd ${workspace} && apt-get update && apt-get install -y build-essential devscripts &&
chnyda1cf6f0d2017-06-02 11:01:04 +020099 cd ${dir} && uscan --download-current-version &&
100 dpkg-buildpackage -S -nc -uc -us""")
101 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100102}
103
104/*
105 * Build source package using git-buildpackage
106 *
107 * @param dir Tree to build
108 * @param image Image name to use for build (default debian:sid)
109 * @param snapshot Generate snapshot version (default false)
110 */
Oleg Iurchenko68e17b02018-01-02 12:24:49 +0200111def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitName='Jenkins', gitEmail='jenkins@dummy.org', revisionPostfix="", remote="origin/") {
Jakub Josef79ecec32017-02-17 14:36:28 +0100112 def common = new com.mirantis.mk.Common()
Filip Pytloun81c864d2017-03-21 15:19:30 +0100113 def jenkinsUID = common.getJenkinsUid()
114 def jenkinsGID = common.getJenkinsGid()
Jakub Josef79ecec32017-02-17 14:36:28 +0100115
116 if (! revisionPostfix) {
117 revisionPostfix = ""
118 }
119
chnyda1cf6f0d2017-06-02 11:01:04 +0200120 def workspace = common.getWorkspace()
121 def dockerLib = new com.mirantis.mk.Docker()
122 def imageArray = image.split(":")
123 def os = imageArray[0]
124 def dist = imageArray[1]
Jakub Josefda4eb702018-02-02 15:40:04 +0100125 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}:latest", image)
chnyda1cf6f0d2017-06-02 11:01:04 +0200126
chnyda8ad962e2017-06-02 12:24:15 +0200127 img.inside("-u root:root") {
chnyda1cf6f0d2017-06-02 11:01:04 +0200128
129 withEnv(["DEBIAN_FRONTEND=noninteractive", "DEBFULLNAME='${gitName}'", "DEBEMAIL='${gitEmail}'"]) {
chnydaa3603b42017-06-02 12:36:08 +0200130 sh("""bash -c 'cd ${workspace} && (which eatmydata || (apt-get update && apt-get install -y eatmydata)) &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100131 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
132 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
Jakub Josef5de05f62017-05-30 15:25:55 +0200133 apt-get update && apt-get install -y build-essential git-buildpackage dpkg-dev sudo &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100134 groupadd -g ${jenkinsGID} jenkins &&
135 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
Filip Pytloun78b91832017-06-30 13:16:31 +0200136 chown -R ${jenkinsUID}:${jenkinsGID} /home/jenkins &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100137 cd ${dir} &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200138 sudo -H -E -u jenkins git config --global user.name "${gitName}" &&
139 sudo -H -E -u jenkins git config --global user.email "${gitEmail}" &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100140 [[ "${snapshot}" == "false" ]] || (
Alexander Noskov2b6b4be2017-09-13 17:32:44 +0400141 VERSION=`dpkg-parsechangelog --count 1 --show-field Version` &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100142 UPSTREAM_VERSION=`echo \$VERSION | cut -d "-" -f 1` &&
143 REVISION=`echo \$VERSION | cut -d "-" -f 2` &&
144 TIMESTAMP=`date +%Y%m%d%H%M` &&
145 if [[ "`cat debian/source/format`" = *quilt* ]]; then
146 UPSTREAM_BRANCH=`(grep upstream-branch debian/gbp.conf || echo master) | cut -d = -f 2 | tr -d " "` &&
Oleg Iurchenko68e17b02018-01-02 12:24:49 +0200147 UPSTREAM_REV=`git rev-parse --short ${remote}\$UPSTREAM_BRANCH` &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100148 NEW_UPSTREAM_VERSION="\$UPSTREAM_VERSION+\$TIMESTAMP.\$UPSTREAM_REV" &&
Alexander Noskov2b6b4be2017-09-13 17:32:44 +0400149 NEW_UPSTREAM_VERSION_TAG=`echo \$NEW_UPSTREAM_VERSION | sed 's/.*://'` &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100150 NEW_VERSION=\$NEW_UPSTREAM_VERSION-\$REVISION$revisionPostfix &&
Alexander Noskov2b6b4be2017-09-13 17:32:44 +0400151 echo "Generating new upstream version \$NEW_UPSTREAM_VERSION_TAG" &&
Oleg Iurchenko68e17b02018-01-02 12:24:49 +0200152 sudo -H -E -u jenkins git tag \$NEW_UPSTREAM_VERSION_TAG ${remote}\$UPSTREAM_BRANCH &&
Alexander Noskov2b6b4be2017-09-13 17:32:44 +0400153 sudo -H -E -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION_TAG
Jakub Josef79ecec32017-02-17 14:36:28 +0100154 else
155 NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix
156 fi &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200157 sudo -H -E -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
158 sudo -H -E -u jenkins git add -u debian/changelog &&
159 sudo -H -E -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
Jakub Josef79ecec32017-02-17 14:36:28 +0100160 ) &&
chnydaa3603b42017-06-02 12:36:08 +0200161 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 '""")
chnyda1cf6f0d2017-06-02 11:01:04 +0200162 }
163 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100164}
165
166/*
167 * Run lintian checks
168 *
169 * @param changes Changes file to test against
170 * @param profile Lintian profile to use (default debian)
171 * @param image Image name to use for build (default debian:sid)
172 */
173def runLintian(changes, profile="debian", image="debian:sid") {
174 def common = new com.mirantis.mk.Common()
chnyda1cf6f0d2017-06-02 11:01:04 +0200175 def workspace = common.getWorkspace()
176 def dockerLib = new com.mirantis.mk.Docker()
177 def imageArray = image.split(":")
178 def os = imageArray[0]
179 def dist = imageArray[1]
Jakub Josefda4eb702018-02-02 15:40:04 +0100180 def img = dockerLib.getImage("tcpcloud/debian-build-${os}-${dist}:latest", image)
chnyda8ad962e2017-06-02 12:24:15 +0200181 img.inside("-u root:root") {
chnyda12f3b3f2017-06-02 12:19:38 +0200182 sh("""cd ${workspace} && apt-get update && apt-get install -y lintian &&
chnyda1cf6f0d2017-06-02 11:01:04 +0200183 lintian -Ii -E --pedantic --profile=${profile} ${changes}""")
184 }
Jakub Josef79ecec32017-02-17 14:36:28 +0100185}
chnyda4e5ac792017-03-14 15:24:18 +0100186
187/*
188 * Import gpg key
189 *
190 * @param privateKeyCredId Public key jenkins credential id
191 */
192def importGpgKey(privateKeyCredId)
193{
194 def common = new com.mirantis.mk.Common()
195 def workspace = common.getWorkspace()
196 def privKey = common.getCredentials(privateKeyCredId, "key")
197 def private_key = privKey.privateKeySource.privateKey
chnydac6846452017-03-21 16:50:43 +0100198 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
199 def retval = sh(script: "export GNUPGHOME=${workspace}/.gnupg; gpg --list-secret-keys | grep ${gpg_key_id}", returnStatus: true)
200 if (retval) {
201 writeFile file:"${workspace}/private.key", text: private_key
202 sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key")
203 }
chnyda4e5ac792017-03-14 15:24:18 +0100204}
205
206/*
207 * upload source package to launchpad
208 *
209 * @param ppaRepo ppa repository on launchpad
210 * @param dirPath repository containing the source packages
211 */
212
213def uploadPpa(ppaRepo, dirPath, privateKeyCredId) {
214
215 def common = new com.mirantis.mk.Common()
216 def workspace = common.getWorkspace()
217 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
218
219 dir(dirPath)
220 {
221 def images = findFiles(glob: "*.orig*.tar.gz")
222 for (int i = 0; i < images.size(); ++i) {
223 def name = images[i].getName()
224 def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0)
225 def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0)
226 def orig_md5 = common.cutOrDie("md5sum ${name}", 0)
227 def orig_size = common.cutOrDie("ls -l ${name}", 4)
228
chnyda5d1e97f2017-03-17 15:53:47 +0100229 def retval = sh(script: "wget --quiet -O orig-tmp https://launchpad.net/ubuntu/+archive/primary/+files/${name}", returnStatus: true)
chnyda4e5ac792017-03-14 15:24:18 +0100230 if (retval == 0) {
231 sh("mv orig-tmp ${name}")
232 def new_sha1 = common.cutOrDie("sha1sum ${name}", 0)
233 def new_sha256 = common.cutOrDie("sha256sum ${name}", 0)
234 def new_md5 = common.cutOrDie("md5sum ${name}", 0)
235 def new_size = common.cutOrDie("ls -l ${name}", 4)
236
237 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")
chnyda3c93ff62017-03-23 10:11:36 +0100238 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")
chnyda4e5ac792017-03-14 15:24:18 +0100239 }
chnyda4e5ac792017-03-14 15:24:18 +0100240 }
chnyda34e5c942017-03-22 18:06:06 +0100241 sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *_source.changes")
242 sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *_source.changes")
chnyda4e5ac792017-03-14 15:24:18 +0100243 }
Filip Pytlounfbbd1682017-03-17 22:48:04 +0100244}