blob: 8fec2ca12f857c6a196575633cf3f4bb7dbd41c2 [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()
27
Jakub Josef79ecec32017-02-17 14:36:28 +010028 def pkg = file.split('/')[-1].split('_')[0]
29 def img = docker.image(image)
30
31 workspace = common.getWorkspace()
32 sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
33 which eatmydata || (apt-get update && apt-get install -y eatmydata) &&
34 export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH:+"\$LD_LIBRARY_PATH:"}/usr/lib/libeatmydata &&
35 export LD_PRELOAD=\${LD_PRELOAD:+"\$LD_PRELOAD "}libeatmydata.so &&
36 [[ -z "${extraRepoUrl}" && "${extraRepoUrl}" != "null" ]] || echo "${extraRepoUrl}" >/etc/apt/sources.list.d/extra.list &&
37 [[ -z "${extraRepoKeyUrl}" && "${extraRepoKeyUrl}" != "null" ]] || (
38 which curl || (apt-get update && apt-get install -y curl) &&
39 curl --insecure -ss -f "${extraRepoKeyUrl}" | apt-key add -
40 ) &&
Filip Pytloun81c864d2017-03-21 15:19:30 +010041 apt-get update && apt-get install -y build-essential devscripts equivs sudo &&
42 groupadd -g ${jenkinsGID} jenkins &&
43 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
Jakub Josef6bebf162017-05-10 14:21:00 +020044 [ ! -f pre_build_script.sh ] || bash ./pre_build_script.sh &&
Filip Pytlounff82fc02017-03-27 12:17:05 +020045 sudo -H -E -u jenkins dpkg-source -x ${file} build-area/${pkg} && cd build-area/${pkg} &&
Jakub Josef79ecec32017-02-17 14:36:28 +010046 mk-build-deps -t "apt-get -o Debug::pkgProblemResolver=yes -y" -i debian/control
Filip Pytlounff82fc02017-03-27 12:17:05 +020047 sudo -H -E -u jenkins debuild --no-lintian -uc -us -b'""")
Jakub Josef79ecec32017-02-17 14:36:28 +010048}
49
50/*
51 * Build source package from directory
52 *
53 * @param dir Tree to build
54 * @param image Image name to use for build (default debian:sid)
55 * @param snapshot Generate snapshot version (default false)
56 */
57def buildSource(dir, image="debian:sid", snapshot=false, gitEmail='jenkins@dummy.org', gitName='Jenkins', revisionPostfix="") {
58 def isGit
59 try {
60 sh("test -d ${dir}/.git")
61 isGit = true
62 } catch (Exception e) {
63 isGit = false
64 }
65
66 if (isGit == true) {
67 buildSourceGbp(dir, image, snapshot, gitEmail, gitName, revisionPostfix)
68 } else {
69 buildSourceUscan(dir, image)
70 }
71}
72
73/*
74 * Build source package, fetching upstream code using uscan
75 *
76 * @param dir Tree to build
77 * @param image Image name to use for build (default debian:sid)
78 */
79def buildSourceUscan(dir, image="debian:sid") {
80 def common = new com.mirantis.mk.Common()
81 def img = docker.image(image)
82 workspace = common.getWorkspace()
83 sh("""docker run -e DEBIAN_FRONTEND=noninteractive -v ${workspace}:${workspace} -w ${workspace} --rm=true --privileged ${image} /bin/bash -c '
84 apt-get update && apt-get install -y build-essential devscripts &&
85 cd ${dir} && uscan --download-current-version &&
86 dpkg-buildpackage -S -nc -uc -us'""")
87}
88
89/*
90 * Build source package using git-buildpackage
91 *
92 * @param dir Tree to build
93 * @param image Image name to use for build (default debian:sid)
94 * @param snapshot Generate snapshot version (default false)
95 */
Filip Pytlounff82fc02017-03-27 12:17:05 +020096def buildSourceGbp(dir, image="debian:sid", snapshot=false, gitName='Jenkins', gitEmail='jenkins@dummy.org', revisionPostfix="") {
Jakub Josef79ecec32017-02-17 14:36:28 +010097 def common = new com.mirantis.mk.Common()
Filip Pytloun81c864d2017-03-21 15:19:30 +010098 def jenkinsUID = common.getJenkinsUid()
99 def jenkinsGID = common.getJenkinsGid()
Jakub Josef79ecec32017-02-17 14:36:28 +0100100
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 &&
Jakub Josef5de05f62017-05-30 15:25:55 +0200111 apt-get update && apt-get install -y build-essential git-buildpackage dpkg-dev sudo &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100112 groupadd -g ${jenkinsGID} jenkins &&
113 useradd -s /bin/bash --uid ${jenkinsUID} --gid ${jenkinsGID} -m jenkins &&
114 cd ${dir} &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200115 sudo -H -E -u jenkins git config --global user.name "${gitName}" &&
116 sudo -H -E -u jenkins git config --global user.email "${gitEmail}" &&
Jakub Josef79ecec32017-02-17 14:36:28 +0100117 [[ "${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" &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200128 sudo -H -E -u jenkins git tag \$NEW_UPSTREAM_VERSION origin/\$UPSTREAM_BRANCH &&
129 sudo -H -E -u jenkins git merge -X theirs \$NEW_UPSTREAM_VERSION
Jakub Josef79ecec32017-02-17 14:36:28 +0100130 else
131 NEW_VERSION=\$VERSION+\$TIMESTAMP.`git rev-parse --short HEAD`$revisionPostfix
132 fi &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200133 sudo -H -E -u jenkins gbp dch --auto --multimaint-merge --ignore-branch --new-version=\$NEW_VERSION --distribution `lsb_release -c -s` --force-distribution &&
134 sudo -H -E -u jenkins git add -u debian/changelog &&
135 sudo -H -E -u jenkins git commit -m "New snapshot version \$NEW_VERSION"
Jakub Josef79ecec32017-02-17 14:36:28 +0100136 ) &&
Filip Pytlounff82fc02017-03-27 12:17:05 +0200137 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 '""")
Jakub Josef79ecec32017-02-17 14:36:28 +0100138}
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 */
147def 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}
chnyda4e5ac792017-03-14 15:24:18 +0100155
156/*
157 * Import gpg key
158 *
159 * @param privateKeyCredId Public key jenkins credential id
160 */
161def 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
chnydac6846452017-03-21 16:50:43 +0100167 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
168 def retval = sh(script: "export GNUPGHOME=${workspace}/.gnupg; gpg --list-secret-keys | grep ${gpg_key_id}", returnStatus: true)
169 if (retval) {
170 writeFile file:"${workspace}/private.key", text: private_key
171 sh(script: "gpg --no-tty --allow-secret-key-import --homedir ${workspace}/.gnupg --import ./private.key")
172 }
chnyda4e5ac792017-03-14 15:24:18 +0100173}
174
175/*
176 * upload source package to launchpad
177 *
178 * @param ppaRepo ppa repository on launchpad
179 * @param dirPath repository containing the source packages
180 */
181
182def uploadPpa(ppaRepo, dirPath, privateKeyCredId) {
183
184 def common = new com.mirantis.mk.Common()
185 def workspace = common.getWorkspace()
186 def gpg_key_id = common.getCredentials(privateKeyCredId, "key").username
187
188 dir(dirPath)
189 {
190 def images = findFiles(glob: "*.orig*.tar.gz")
191 for (int i = 0; i < images.size(); ++i) {
192 def name = images[i].getName()
193 def orig_sha1 = common.cutOrDie("sha1sum ${name}", 0)
194 def orig_sha256 = common.cutOrDie("sha256sum ${name}", 0)
195 def orig_md5 = common.cutOrDie("md5sum ${name}", 0)
196 def orig_size = common.cutOrDie("ls -l ${name}", 4)
197
chnyda5d1e97f2017-03-17 15:53:47 +0100198 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 +0100199 if (retval == 0) {
200 sh("mv orig-tmp ${name}")
201 def new_sha1 = common.cutOrDie("sha1sum ${name}", 0)
202 def new_sha256 = common.cutOrDie("sha256sum ${name}", 0)
203 def new_md5 = common.cutOrDie("md5sum ${name}", 0)
204 def new_size = common.cutOrDie("ls -l ${name}", 4)
205
206 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 +0100207 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 +0100208 }
chnyda4e5ac792017-03-14 15:24:18 +0100209 }
chnyda34e5c942017-03-22 18:06:06 +0100210 sh("export GNUPGHOME=${workspace}/.gnupg; debsign --re-sign -k ${gpg_key_id} *_source.changes")
211 sh("export GNUPGHOME=${workspace}/.gnupg; dput -f \"ppa:${ppaRepo}\" *_source.changes")
chnyda4e5ac792017-03-14 15:24:18 +0100212 }
Filip Pytlounfbbd1682017-03-17 22:48:04 +0100213}