modified version of jenkins debian glue (https://github.com/mika/jenkins-debian-glue) for devuan
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1169 lines
41 KiB

  1. #!/bin/bash
  2. if [ -z ${JENKINS_DEBIAN_GLUE_QUIET:-} ]; then
  3. set -x
  4. fi
  5. set -u
  6. # Debian bug #531885: cowbuilder build fails with restrictive umask
  7. umask 022
  8. # make sure cowbuilder/pbuilder/... are available
  9. PATH='/bin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin'
  10. # make sure qemu doesn't give us the "out of memory issue" when compiling
  11. export QEMU_RESERVED_VA=0x0
  12. echo "*** Starting $0 at $(date) ***"
  13. start_seconds=$(cut -d . -f 1 /proc/uptime)
  14. JENKINS_DEBIAN_GLUE_VERSION=$(dpkg --list jenkins-debian-glue 2>/dev/null | awk '/^ii/ {print $3}')
  15. if [ -n "${JENKINS_DEBIAN_GLUE_VERSION:-}" ] ; then
  16. echo "*** Running jenkins-debian-glue version $JENKINS_DEBIAN_GLUE_VERSION ***"
  17. fi
  18. HOST_ARCH="$(dpkg-architecture -qDEB_HOST_ARCH)"
  19. if [ -n "${HOST_ARCH:-}" ] ; then
  20. echo "*** Identified host architecture ${HOST_ARCH} ***"
  21. fi
  22. set_debootstrap() {
  23. if [ -n "${DEBOOTSTRAP:-}" ] ; then
  24. echo "*** Using provided ${DEBOOTSTRAP} as DEBOOTSTRAP ***"
  25. return 0
  26. fi
  27. if [ "${architecture:-}" = "${HOST_ARCH:-}" ] || \
  28. [ "${architecture:-}" = "all" ] ; then
  29. DEBOOTSTRAP="debootstrap"
  30. return 0
  31. fi
  32. # we can compile i386 packages on amd64, so don't use qemu-debootstrap there
  33. case "$HOST_ARCH" in
  34. amd64)
  35. if [ "${architecture:-}" = "i386" ] ; then
  36. DEBOOTSTRAP="debootstrap"
  37. return 0
  38. fi
  39. ;;
  40. esac
  41. # otherwise assume we're building for a foreign architecture
  42. if [ -x "$(which qemu-debootstrap)" ] ; then
  43. DEBOOTSTRAP="qemu-debootstrap"
  44. else
  45. echo "Error: qemu-debootstrap not available, please install qemu-user-static." >&2
  46. exit 1
  47. fi
  48. }
  49. checks_and_defaults() {
  50. # backwards compatibility, see PR#94
  51. if [ -z "${REPOSITORY:-}" ] ; then
  52. local repository_is_missing_in_env=true
  53. else
  54. local repository_is_missing_in_env=false
  55. fi
  56. if [ -r /etc/jenkins/debian_glue ] ; then
  57. . /etc/jenkins/debian_glue
  58. fi
  59. # backwards compatibility, see PR#94
  60. if [ -n "${REPOSITORY:-}" ] && $repository_is_missing_in_env ; then
  61. echo "*** WARNING: 'REPOSITORY' set in /etc/jenkins/debian_glue but should be DEFAULT_REPOSITORY ***"
  62. echo "*** WARNING: Setting DEFAULT_REPOSITORY to $REPOSITORY for backwards compatibility ***"
  63. echo "*** WARNING: Please replace REPOSITORY=... in /etc/jenkins/debian_glue with DEFAULT_REPOSITORY=... ***"
  64. DEFAULT_REPOSITORY="${REPOSITORY}"
  65. fi
  66. # make sure cowbuilder/pbuilder has access to the variable
  67. if [ -n "${DEB_KEEP_BUILD_ENV:-}" ] ; then
  68. export DEB_KEEP_BUILD_ENV
  69. fi
  70. if [ "${USE_EATMYDATA:-}" = true ] ; then
  71. export USE_EATMYDATA
  72. fi
  73. if [ "${USE_CCACHE:-}" = true ] ; then
  74. export USE_CCACHE
  75. fi
  76. if [ -z "${JOB_NAME:-}" ] ; then
  77. echo "Error: No JOB_NAME defined, please run it in jenkins." >&2
  78. exit 1
  79. fi
  80. if [ -z "${architecture:-}" ] ; then
  81. echo "*** No architecture defined. Consider running it with matrix configuration. ***"
  82. architecture="${HOST_ARCH}"
  83. echo "*** Falling back to default, using host architecture ${architecture}. ***"
  84. fi
  85. set_debootstrap
  86. if [ -z "${REPREPRO_OPTS:-}" ] ; then
  87. REPREPRO_OPTS='-v --waitforlock 1000'
  88. echo "*** REPREPRO_OPTS is unset, using default: $REPREPRO_OPTS ***"
  89. fi
  90. if [ -z "${DEFAULT_REPOSITORY:-}" ] ; then
  91. DEFAULT_REPOSITORY='/srv/repository'
  92. fi
  93. # REPOSITORY can overwrite DEFAULT_REPOSITORY, so define only if unset
  94. if [ -z "${REPOSITORY:-}" ] ; then
  95. REPOSITORY="${DEFAULT_REPOSITORY}"
  96. fi
  97. if [ -z "${PBUILDER_HOOKDIR:-}" ] ; then
  98. PBUILDER_HOOKDIR='/usr/share/jenkins-debian-glue/pbuilder-hookdir/'
  99. fi
  100. # Evaluate Freight default options - we use the system wide freight
  101. # directories and configuration, unless either $FREIGHT_REPOSITORY or
  102. # $FREIGHT_BASE are specified.
  103. if [ -z "${FREIGHT_REPOSITORY:-}" ] && [ -z "${FREIGHT_BASE:-}" ] ; then
  104. FREIGHT_VARLIB=/var/lib/freight
  105. FREIGHT_VARCACHE=/var/cache/freight
  106. FREIGHT_CONF=/etc/freight.conf
  107. elif [ -z "${FREIGHT_REPOSITORY:-}" ] && [ -n "${FREIGHT_BASE:-}" ] ; then
  108. FREIGHT_VARLIB=${FREIGHT_BASE}/default-source
  109. FREIGHT_VARCACHE=${FREIGHT_BASE}/default
  110. FREIGHT_CONF=${FREIGHT_BASE}/default.conf
  111. else
  112. # Default to /srv/freight unless specifed
  113. if [ -z "${FREIGHT_BASE:-}" ] ; then
  114. FREIGHT_BASE=/srv/freight
  115. fi
  116. FREIGHT_VARLIB=${FREIGHT_BASE}/${FREIGHT_REPOSITORY}-source
  117. FREIGHT_VARCACHE=${FREIGHT_BASE}/${FREIGHT_REPOSITORY}
  118. FREIGHT_CONF=${FREIGHT_BASE}/${FREIGHT_REPOSITORY}.conf
  119. fi
  120. }
  121. clean_workspace() {
  122. echo "*** The following files have been noticed in the workspace [$(pwd)]: ***"
  123. ls -la ./
  124. # echo "*** Cleaning workspace in $(pwd) to make sure we're building from scratch. ***"
  125. # rm -f ./* || true
  126. }
  127. # make sure we don't leave files for next run
  128. bailout() {
  129. [ -n "${1:-}" ] && EXIT="${1}" || EXIT=0
  130. [ -n "${2:-}" ] && echo "$2" >&2
  131. rm -f "${build_lockfile}"
  132. # if we have an aborted build we have to clean up left behinds,
  133. # but we have to make sure we only clean up the files from our run
  134. if [ -r "${update_lockfile_pid}" ] ; then
  135. rm -f "${update_lockfile_pid}"
  136. rm -f "${update_lockfile}"
  137. fi
  138. if [ "${SKIP_BINARY_REMOVAL:-}" = "true" ] ; then
  139. echo "*** Skipping binary removal as requested via SKIP_BINARY_REMOVAL=true. ***"
  140. else
  141. echo "*** Getting rid of files in $WORKSPACE/binaries/ to avoid problems in next run. ***"
  142. rm -f "$WORKSPACE"/binaries/*
  143. fi
  144. ${SUDO_CMD:-} rm -rf /tmp/apt-$$
  145. [ -n "$start_seconds" ] && SECONDS="$[$(cut -d . -f 1 /proc/uptime)-$start_seconds]" || SECONDS="unknown"
  146. echo "*** Finished execution of $0 at $(date) [running ${SECONDS} seconds] ***"
  147. exit $EXIT
  148. }
  149. identify_package_name() {
  150. # make sure we get rid of 'repos' and 'binaries' from Jenkins job name
  151. PACKAGE=${JOB_NAME%-repos*}
  152. PACKAGE=${PACKAGE%-binaries*}
  153. if [ -n "${PACKAGE:-}" ] ; then
  154. echo "*** Identified Debian package name $PACKAGE ***"
  155. else
  156. bailout 1 "Error: could not identify Debian package name based on job name ${JOB_NAME:-}."
  157. fi
  158. }
  159. set_base_path() {
  160. # when BASE_PATH is set in the build step then don't default to $WORKSPACE
  161. if [ -n "${BASE_PATH:-}" ] ; then
  162. echo "*** Using provided ${BASE_PATH} as BASE_PATH ***"
  163. else
  164. BASE_PATH="${WORKSPACE}"
  165. echo "*** Using \$WORKSPACE [$BASE_PATH] as default for BASE_PATH ***"
  166. fi
  167. }
  168. build_info() {
  169. if [ -n "${REPOS:-}" ] ; then
  170. echo "*** Using supplied repository name $REPOS ***"
  171. else
  172. REPOS="${JOB_NAME%-binaries*}"
  173. REPOS="${REPOS%-repos*}"
  174. if [ -z "${distribution:-}" ]; then
  175. echo "*** No repository supplied, using repository name $REPOS ***"
  176. else
  177. REPOS="${REPOS}-${distribution}"
  178. echo "*** No repository supplied but distribution has been set, using repository name $REPOS ***"
  179. fi
  180. fi
  181. }
  182. identify_sourcefile() {
  183. if [ -n "${sources:-}" ] ; then
  184. echo "*** WARNING: sources variable [$sources] is set, please use BASE_PATH variable instead ***"
  185. echo "*** If \$sources is unrelated to build-and-provide-package you can ignore this warning ***"
  186. fi
  187. echo "*** Identifying newest package version ***"
  188. newest_version=''
  189. for file in "${BASE_PATH}/"*.dsc ; do
  190. SOURCE_PACKAGE="$(awk '/^Source: / {print $2}' $file)"
  191. p="$(basename $file .dsc)"
  192. if [ "$p" = '*' ] ; then
  193. bailout 1 "Error: No source package found (forgot to configure source files deployment?)"
  194. fi
  195. cur_version="${p#*_}"
  196. if [ -z "${newest_version}" ] || dpkg --compare-versions "${cur_version}" gt "${newest_version}" ; then
  197. newest_version="${cur_version}"
  198. fi
  199. done
  200. echo "*** Found package version $newest_version ***"
  201. sourcefile="${BASE_PATH}/${SOURCE_PACKAGE}"_*"${newest_version}".dsc
  202. echo "*** Using $sourcefile (version: ${newest_version})"
  203. }
  204. dist_and_arch_settings() {
  205. if [ -z "${architecture:-}" ] || [ "${architecture:-}" = "all" ] ; then
  206. arch="${HOST_ARCH}"
  207. echo "*** No architecture set or architecture set to 'all', using system arch ${arch} ***"
  208. else
  209. arch="${architecture}"
  210. echo "*** architecture is set to ${architecture} ***"
  211. fi
  212. if [ -n "${distribution:-}" ] ; then
  213. local DIST="${distribution}"
  214. else
  215. # default to the currently running distribution to avoid hardcoding
  216. # a distribution which might not be supported by the running system
  217. local distribution=$(lsb_release --short --codename 2>/dev/null)
  218. [ -n "${distribution}" ] || distribution="sid" # fallback to "sid" iff lsb_release fails
  219. local DIST="$distribution"
  220. fi
  221. # if COWBUILDER_DIST is set it overrides distribution then
  222. if [ -n "${COWBUILDER_DIST:-}" ]; then
  223. echo "*** COWBUILDER_DIST is set to $COWBUILDER_DIST - using it for base.cow if it does not exist yet. ***"
  224. else
  225. echo "*** Using cowbuilder base for distribution ${DIST} ***"
  226. COWBUILDER_DIST="${DIST}"
  227. fi
  228. if [ -n "${COWBUILDER_BASE:-}" ] ; then
  229. echo "*** COWBUILDER_BASE is set to $COWBUILDER_BASE - using as cowbuilder base.cow ***"
  230. else
  231. COWBUILDER_BASE="/var/cache/pbuilder/base-${COWBUILDER_DIST}-${arch}.cow"
  232. echo "*** No COWBUILDER_BASE set, using $COWBUILDER_BASE as cowbuilder base.cow ***"
  233. fi
  234. local lockfiles="/var/run/lock/${COWBUILDER_DIST}-${arch}"
  235. build_lockfile="${lockfiles}.building.$$"
  236. update_lockfile="${lockfiles}.update"
  237. update_lockfile_pid="${lockfiles}.update.$$"
  238. }
  239. cowbuilder_init() {
  240. local pbuilderrc=$(mktemp)
  241. echo "# pbuilder config file generated by jenkins-debian-glue on $(date)" > "$pbuilderrc"
  242. # allow pbuilder networking
  243. if [ -n "${PBUILDER_USENETWORK:-}" ] ; then
  244. echo "USENETWORK=yes" >> "$pbuilderrc"
  245. fi
  246. # add libeatmydata to pbuilderrc
  247. if [ "${USE_EATMYDATA:-}" = true ] ; then
  248. echo 'EXTRAPACKAGES="$EXTRAPACKAGES eatmydata"' >> "$pbuilderrc"
  249. echo 'if [ -z "$LD_PRELOAD" ]; then' >> "$pbuilderrc"
  250. echo ' LD_PRELOAD=libeatmydata.so' >> "$pbuilderrc"
  251. echo 'else' >> "$pbuilderrc"
  252. echo ' LD_PRELOAD="$LD_PRELOAD":libeatmydata.so' >> "$pbuilderrc"
  253. echo 'fi' >> "$pbuilderrc"
  254. echo 'export LD_PRELOAD' >> "$pbuilderrc"
  255. fi
  256. # add ccache to pbuilderrc
  257. if [ "${USE_CCACHE:-}" = true ] ; then
  258. echo 'export CCACHEDIR="/var/cache/pbuilder/ccache"' >> "$pbuilderrc"
  259. fi
  260. use_eatmydata
  261. use_ccache
  262. # allow setting main pbuilder configuration file from outside, then append data
  263. # as needed without actually writing anything to user-provided $PBUILDER_CONFIG
  264. if [ -n "${PBUILDER_CONFIG:-}" ] ; then
  265. echo "*** PBUILDER_CONFIG is set, considering $PBUILDER_CONFIG for pbuilder config ***"
  266. if [ -r "${PBUILDER_CONFIG:-}" ] ; then
  267. echo "*** Adding content of $PBUILDER_CONFIG to pbuilder configfile ***"
  268. echo "# $PBUILDER_CONFIG added via jenkins-debian-glue:" >> "$pbuilderrc"
  269. cat $PBUILDER_CONFIG >> "$pbuilderrc"
  270. else
  271. echo "*** WARNING: File $PBUILDER_CONFIG could not be read, ignoring ***"
  272. fi
  273. fi
  274. if [ -n "${COMPONENTS:-}" ] ; then
  275. echo "*** COMPONENTS is set [$COMPONENTS], using for pbuilder configuration ***"
  276. echo "# COMPONENTS set by jenkins-debian-glue:" >> "$pbuilderrc"
  277. echo "COMPONENTS=\"${COMPONENTS}\"" >> "$pbuilderrc"
  278. else
  279. # workaround for Ubuntu problem, as cowdancer is available only in universe :(
  280. # https://bugs.launchpad.net/ubuntu/+source/cowdancer/+bug/237591
  281. # https://bugs.launchpad.net/ubuntu/+source/cowdancer/+bug/747053
  282. echo "*** COMPONENTS is not set, checking whether we need to enable Ubuntu workaround ***"
  283. if [ -n "${PBUILDER_CONFIG:-}" ] ; then
  284. echo "*** PBUILDER_CONFIG is set, not overwriting COMPONENTS for Ubuntu workaround ***"
  285. echo "*** NOTE: If you want to build for Ubuntu make sure COMPONENTS also includes 'universe' ***"
  286. else
  287. echo "*** PBUILDER_CONFIG is not set, continuing with checks for Ubuntu workaround ***"
  288. if ! [ -r "/usr/share/debootstrap/scripts/${COWBUILDER_DIST}" ] ; then
  289. echo "*** WARNING: distribution ${COWBUILDER_DIST} not supported by debootstrap, not considering Ubuntu workaround ***"
  290. else
  291. # debootstrap scripts for recent versions of Ubuntu are all pointing to gutsy, use that
  292. # to identify Ubuntu as the distribution we want to build instead of hardcoding all
  293. # the Ubuntu release names here
  294. if [ "$(readlink -f /usr/share/debootstrap/scripts/${COWBUILDER_DIST})" != '/usr/share/debootstrap/scripts/gutsy' ] ; then
  295. echo "*** Doesn't look like we're building for Ubuntu, not considering Ubuntu workaround ***"
  296. else
  297. echo "*** Building for Ubuntu detected, enabling universe repository component to work around cowdancer issue ***"
  298. echo "# Building for Ubuntu detected, enabling universe repository component to work around cowdancer issue:" >> "$pbuilderrc"
  299. echo 'COMPONENTS="main universe"' >> "$pbuilderrc"
  300. fi
  301. fi
  302. fi
  303. fi
  304. # ensure that we've access to Debian's archive keyring if we're
  305. # building for recent Debian releases on Ubuntu, see issue#130
  306. if lsb_release --id 2>/dev/null | grep -q Ubuntu ; then
  307. echo "*** Looks like we're building on Ubuntu, checking for distribution target ***"
  308. if [ "$(readlink -f /usr/share/debootstrap/scripts/${COWBUILDER_DIST})" != '/usr/share/debootstrap/scripts/sid' ] ; then
  309. echo "*** Doesn't look like we're building for Debian, not considering Debian archive keyring workaround ***"
  310. else
  311. if ! [ -r /usr/share/keyrings/debian-archive-keyring.gpg ] ; then
  312. echo "*** WARNING: /usr/share/keyrings/debian-archive-keyring.gpg does not exist. ***"
  313. echo "*** If building fails with 'E: Release signed by unknown key ...' please ensure package debian-archive-keyring is installed ***"
  314. else
  315. echo "*** Package debian-archive-keyring is present, enabling its usage for keyring ***"
  316. echo "DEBOOTSTRAPOPTS=(${DEBOOTSTRAPOPTS[@]:-} '--keyring' '/usr/share/keyrings/debian-archive-keyring.gpg' )" >> "$pbuilderrc"
  317. fi
  318. fi
  319. fi
  320. echo "*** Listing pbuilder configuration file as reference: ***"
  321. cat "$pbuilderrc"
  322. if ls "${COWBUILDER_BASE}.building."* >/dev/null 2>&1 ; then
  323. echo "*** Skipping update run because a build is in progress ***"
  324. return 0
  325. fi
  326. if ls "${update_lockfile}"* >/dev/null 2>&1 ; then
  327. echo "*** Update run already taking place, skipping ***"
  328. return 0
  329. fi
  330. (
  331. # if we cannot get the lock then somebody else is already running
  332. if ! flock --nonblock 9 ; then
  333. exit 1
  334. fi
  335. # in order to be able to clean up aborted runs we need to
  336. # mark that this file is ours
  337. touch "${update_lockfile_pid}"
  338. if [ ! -d "${COWBUILDER_BASE}" ]; then
  339. echo "*** Creating cowbuilder base $COWBUILDER_BASE for arch $arch and distribution $COWBUILDER_DIST ***"
  340. sudo DIST="${distribution:-}" ARCH="${architecture:-}" ${ADT:+ADT=$ADT} \
  341. cowbuilder --create --basepath "${COWBUILDER_BASE}" --distribution "${COWBUILDER_DIST}" \
  342. --debootstrap "${DEBOOTSTRAP}" --architecture "${architecture:-}" \
  343. --debootstrapopts --arch --debootstrapopts "$arch" \
  344. --debootstrapopts --variant=buildd --configfile="${pbuilderrc}" \
  345. --hookdir "${PBUILDER_HOOKDIR}"
  346. [ $? -eq 0 ] || exit 2
  347. else
  348. if [ "${SKIP_COWBUILDER_UPDATE:-}" = "true" ] ; then
  349. echo "*** Skipping cowbuilder update as requested via SKIP_COWBUILDER_UPDATE ***"
  350. else
  351. echo "*** Updating cowbuilder cow base ***"
  352. sudo DIST="${distribution:-}" ARCH="${architecture:-}" ${ADT:+ADT=$ADT} \
  353. cowbuilder --update --basepath "${COWBUILDER_BASE}" --configfile="${pbuilderrc}"
  354. [ $? -eq 0 ] || exit 3
  355. fi
  356. fi
  357. ) 9>"${update_lockfile}" ||
  358. {
  359. # depending on the exit code we have to distinguish between different failures
  360. case "$?" in
  361. 1)
  362. echo "*** Someone else is holding the lock file ${update_lockfile}, skipping create/update ***"
  363. return 0
  364. ;;
  365. 2)
  366. echo "*** Something went wrong with the creation of the cowbuilder environment. Cleaning up. ***"
  367. rm -rf "${COWBUILDER_BASE}"
  368. bailout 1 "Error: Failed to create cowbuilder base ${COWBUILDER_BASE}."
  369. ;;
  370. 3)
  371. bailout 1 "Error: Failed to update cowbuilder base ${COWBUILDER_BASE}."
  372. ;;
  373. esac
  374. }
  375. rm -f "${update_lockfile_pid}" "${update_lockfile}"
  376. [ -n "${pbuilderrc:-}" ] && rm -f "$pbuilderrc"
  377. }
  378. identify_build_type() {
  379. if [ -n "${DEBBUILDOPTS:-}" ] ; then
  380. echo "*** Using provided ${DEBBUILDOPTS} as DEBBUILDOPTS ***"
  381. echo "*** Please don't forget to set SKIP_ARCH_BUILD={true,false} as needed ***"
  382. return 0
  383. fi
  384. # also support 'export FORCE_BINARY_ONLY=$label' use case with builds
  385. if [ -z "${FORCE_BINARY_ONLY:-}" ] ; then
  386. echo "*** FORCE_BINARY_ONLY is unset, continuing with checks for build type ***"
  387. else
  388. echo "*** FORCE_BINARY_ONLY is set to ${FORCE_BINARY_ONLY} ***"
  389. case "${FORCE_BINARY_ONLY}" in
  390. all)
  391. DEBBUILDOPTS="-A"
  392. echo "*** FORCE_BINARY_ONLY set to 'all', building with DEBBUILDOPTS=${DEBBUILDOPTS} ***"
  393. ;;
  394. *)
  395. DEBBUILDOPTS="-B"
  396. echo "*** FORCE_BINARY_ONLY is NOT set to 'all', building with DEBBUILDOPTS=${DEBBUILDOPTS} ***"
  397. ;;
  398. esac
  399. SKIP_ARCH_BUILD=false
  400. echo "*** Setting SKIP_ARCH_BUILD=$SKIP_ARCH_BUILD to skip further arch builds. ***"
  401. return 0
  402. fi
  403. # defaults
  404. DEBBUILDOPTS="-sa"
  405. SKIP_ARCH_BUILD=false
  406. if [ "${architecture:-}" = "all" ] ; then
  407. echo "*** \$architecture is set to 'all', skipping further identify_build_type checks. ***"
  408. echo "*** Consider setting \$architecture to amd64, i386,... instead. ***"
  409. return 0
  410. fi
  411. if [ -z "${MAIN_ARCHITECTURE:-}" ] ; then
  412. if [ "${HOST_ARCH}" = "${architecture:-}" ] ; then
  413. echo "*** MAIN_ARCHITECTURE is unset. ***"
  414. echo "*** Host architecture [${HOST_ARCH}] matches \$architecture [${architecture:-}], using default ${DEBBUILDOPTS:-} buildoption ***"
  415. return 0
  416. else
  417. echo "*** MAIN_ARCHITECTURE is unset. ***"
  418. echo "*** Host architecture [${HOST_ARCH}] does not match \$architecture [${architecture:-}] ... ***"
  419. echo "*** ... setting binary only build and continuing with identify_build_type ***"
  420. DEBBUILDOPTS="-B"
  421. fi
  422. else
  423. if [ "${MAIN_ARCHITECTURE:-}" = "${architecture:-}" ] ;then
  424. echo "*** MAIN_ARCHITECTURE is set [${MAIN_ARCHITECTURE:-}]. ***"
  425. echo "*** MAIN_ARCHITECTURE matches \$architecture [${architecture:-}], using default ${DEBBUILDOPTS:-} buildoption ***"
  426. return 0
  427. else
  428. echo "*** MAIN_ARCHITECTURE [${MAIN_ARCHITECTURE:-}] does not match \$architecture [${architecture:-}], setting binary only build and continuing with identify_build_type ***"
  429. DEBBUILDOPTS="-B"
  430. fi
  431. fi
  432. local TMPDIR=$(mktemp -d)
  433. local old_dir=$(pwd)
  434. cd "$TMPDIR"
  435. for file in ${BASE_PATH}/${SOURCE_PACKAGE}_*.tar.* ; do
  436. if tar atf "$file" 2>/dev/null | egrep -q '^[^/]+/debian/control$' ; then
  437. # might be source/debian/control - so let's identify the path to debian/control
  438. local control_file=$(tar atf "$file" 2>/dev/null | egrep '^[^/]+/debian/control$')
  439. tar axf "$file" "$control_file" || bailout 1 "Error while looking at debian/control in source archive."
  440. if grep -q '^Architecture: all' "$control_file" ; then
  441. if grep -q '^Architecture: .*any' "$control_file" ; then
  442. echo "*** Package provides arch 'all' + 'any', enabling -B buildoption for this architecture. ***"
  443. # -B -> binary-only build, limited to architecture dependent packages
  444. DEBBUILDOPTS="-B"
  445. break
  446. else
  447. # only "Architecture: all", so no arch specific packages since
  448. # we aren't building for $MAIN_ARCHITECTURE
  449. SKIP_ARCH_BUILD=true
  450. break
  451. fi
  452. fi
  453. fi
  454. done
  455. cd "$old_dir"
  456. rm -rf "${TMPDIR}"
  457. }
  458. autopkg_run() {
  459. case "${ADT:-}" in
  460. skip)
  461. echo "*** Skipping external autopkgtests as ADT is set to 'skip' ***"
  462. return 0
  463. ;;
  464. internal)
  465. echo "*** Skipping external autopkgtests as ADT is set to 'internal' ***"
  466. return 0
  467. ;;
  468. external|all)
  469. echo "*** Executing external autopkgtests as ADT is set to $ADT ***"
  470. ;;
  471. *)
  472. echo "*** Skipping external autopkgtests as ADT is neither set to 'external' nor 'all' ***"
  473. return 0
  474. ;;
  475. esac
  476. if [ -z "${ADT_RUNNER:-}" ] ; then
  477. bailout 1 "*** Error: ADT_RUNNER is unset, external autopkgtests depend on according ADT_RUNNER configuration ***"
  478. fi
  479. dsc_file="${WORKSPACE}/"*"_${newest_version}.dsc"
  480. if ! grep -q '^Testsuite: autopkgtest' ${dsc_file} ; then
  481. echo "*** No 'Testsuite: autopkgtest' present in ${dsc_file}, skipping external autopkgtests ***"
  482. return 0
  483. fi
  484. if [ -n "${ADT_OPTIONS:-}" ] ; then
  485. echo "*** Using provided ADT_OPTIONS $ADT_OPTIONS ***"
  486. else
  487. # since autopkgtest 3.16 the --tmp-dir option is gone, make sure
  488. # we've --output-dir available though before using it
  489. if adt-run --help | grep -q -- --output-dir 2>/dev/null ; then
  490. local adt_output_option='--output-dir'
  491. else
  492. local adt_output_option='--tmp-dir'
  493. fi
  494. ADT_OPTIONS="$adt_output_option adt-external/out --summary adt-external/summary"
  495. rm -rf adt-external
  496. mkdir -p adt-external
  497. echo "*** Using default ADT_OPTIONS $ADT_OPTIONS ***"
  498. fi
  499. changes_file="${WORKSPACE}/"*"_${newest_version}_${arch}.changes"
  500. echo "*** Executing 'adt-run --changes $changes_file ${ADT_OPTIONS:-} --- $ADT_RUNNER' ***"
  501. adt-run --changes $changes_file ${ADT_OPTIONS:-} --- $ADT_RUNNER || bailout $?
  502. }
  503. use_ccache() {
  504. if [ "${USE_CCACHE:-}" = 'true' ] ; then
  505. echo "*** USE_CCACHE is set to true, enabling ccache support ***"
  506. echo 'CCACHEDIR="/var/cache/pbuilder/ccache"' >> "$pbuilderrc"
  507. fi
  508. }
  509. enable_eatmydata() {
  510. echo 'EXTRAPACKAGES="$EXTRAPACKAGES eatmydata"' >> "$pbuilderrc"
  511. echo 'export LD_PRELOAD="${LD_PRELOAD:+$LD_PRELOAD:}libeatmydata.so"' >> "$pbuilderrc"
  512. }
  513. use_eatmydata() {
  514. if [ "${USE_EATMYDATA:-}" = 'false' ] ; then
  515. echo "*** eatmydata is disabled via USE_EATMYDATA [$USE_EATMYDATA] ***"
  516. return 0
  517. fi
  518. if [ "${USE_EATMYDATA:-}" = 'true' ] ; then
  519. echo "*** eatmydata is enabled via USE_EATMYDATA [$USE_EATMYDATA], forcing usage (skipping host/distribution checks) ***"
  520. enable_eatmydata
  521. else
  522. if ! dpkg-query --show --showformat='${Status}' eatmydata | grep -q '^install' ; then
  523. echo "*** eatmydata missing on host system, not considering for usage (force via USE_EATMYDATA=true) ***"
  524. else
  525. if ! dpkg --compare-versions "$(dpkg-query --show --showformat='${Version}' eatmydata)" gt '26-2' ; then
  526. echo "*** eatmydata version on host needs to be newer than v26-2 (>=82-6 is recommended), skipping eatmydata ***"
  527. else
  528. echo "*** eatmydata is present on host system, now checking distribution support ***"
  529. case "${COWBUILDER_DIST}" in
  530. etch|lenny|squeeze|wheezy)
  531. echo "*** Debian release $COWBUILDER_DIST doesn't provide eatmydata >=82-6, skipping eatmydata ***"
  532. ;;
  533. warty|hoary|breezy|dapper|edgy|feisty|gutsy|hardy|intrepid|jaunty|karmi|lucid|maverick|natty|oneiric|quantal|raring|saucy|utopic|precise|trusty)
  534. echo "*** Ubuntu release $COWBUILDER_DIST doesn't provide eatmydata >=82-6, skipping eatmydata ***"
  535. ;;
  536. *)
  537. echo "*** Distribution ${COWBUILDER_DIST} should provide recent eatmydata support, enabling eatmydata ***"
  538. enable_eatmydata
  539. ;;
  540. esac
  541. fi
  542. fi
  543. fi
  544. }
  545. autopkgtest_results() {
  546. if [ -n "${SKIP_AUTOPKGTEST_RESULTS:-}" ] ; then
  547. echo "** Skipping autopkgtest_results as requested via SKIP_AUTOPKGTEST_RESULTS ***"
  548. return 0
  549. fi
  550. mkdir -p adt
  551. if [ -r autopkgtest.summary ] ; then
  552. mv autopkgtest.summary adt/summary
  553. else # do not fail if no autopkgtest run took place
  554. touch adt/summary
  555. fi
  556. }
  557. cowbuilder_run() {
  558. echo "*** cowbuilder build phase for arch $architecture ***"
  559. mkdir -p "$WORKSPACE"/binaries/ /tmp/apt-$$
  560. local BINDMOUNTS="/tmp/apt-$$ ${USER_BINDMOUNTS:-}"
  561. # make sure we build arch specific packages only when necessary
  562. identify_build_type
  563. if $SKIP_ARCH_BUILD ; then
  564. autopkgtest_results
  565. bailout 0 "Nothing to do, architecture all binary packages only for non-primary architecture."
  566. fi
  567. # For release builds use release repo to satisfy dependencies
  568. if [ -n "${release:-}" ] && [ "$release" != "none" ] && [ "$release" != "trunk" ] && \
  569. [ "${release}" != '${release}' ] ; then
  570. if [ -n "${RELEASE_REPOSITORY:-}" ]; then
  571. local REPOSITORY="${RELEASE_REPOSITORY}"
  572. else
  573. local REPOSITORY="${REPOSITORY}/release/${release}"
  574. fi;
  575. if [ -d "${REPOSITORY}/dists/${release}" ]; then
  576. BINDMOUNTS="$BINDMOUNTS $REPOSITORY"
  577. local components="$(awk -F': ' '/^Components:/ { print $2 }' \
  578. "${REPOSITORY}/dists/${release}/Release")"
  579. # Check if keyring is provided so the repository can be verified.
  580. if [ -n "${REPOSITORY_KEYRING:-}" ]; then
  581. local trusted=
  582. else
  583. # If no keyring is provided, just assume that the repository is
  584. # trustworthy. This option appeared in apt 0.8.16~exp3 which is not
  585. # available in Squeeze.
  586. local trusted="[trusted=yes]"
  587. fi
  588. cat > /tmp/apt-$$/release.list <<EOF
  589. deb ${trusted} file://${REPOSITORY} ${release} ${components}
  590. EOF
  591. fi
  592. if [ -n "${REPOSITORY_KEYRING:-}" ]; then
  593. cp -a "${REPOSITORY_KEYRING}" /tmp/apt-$$/keyring.gpg
  594. fi
  595. fi
  596. # Adding extra repository for resolving dependencies
  597. if [ -n "${REPOSITORY_EXTRA:-}" ]; then
  598. echo ${REPOSITORY_EXTRA} | tr ',' '\n' > /tmp/apt-$$/extra.list
  599. fi
  600. if [ -n "${REPOSITORY_EXTRA_KEYS:-}" ]; then
  601. OIFS="$IFS"
  602. IFS=',' read -a array <<< "${REPOSITORY_EXTRA_KEYS}"
  603. for key in "${array[@]}" ; do
  604. curl -O "${key}"
  605. gpg --no-default-keyring --keyring /tmp/apt-$$/keyring.gpg --import "${key##*/}"
  606. done
  607. unset key
  608. IFS="$OIFS"
  609. fi
  610. local pbuilderrc=$(mktemp)
  611. echo "# pbuilder config file generated by jenkins-debian-glue on $(date)" > "$pbuilderrc"
  612. # allow pbuilder networking
  613. if [ -n "${PBUILDER_USENETWORK:-}" ] ; then
  614. echo "USENETWORK=yes" >> "$pbuilderrc"
  615. fi
  616. # add libeatmydata to pbuilderrc
  617. if [ "${USE_EATMYDATA:-}" = true ] ; then
  618. echo 'EXTRAPACKAGES="$EXTRAPACKAGES eatmydata"' >> "$pbuilderrc"
  619. echo 'if [ -z "$LD_PRELOAD" ]; then' >> "$pbuilderrc"
  620. echo ' LD_PRELOAD=libeatmydata.so' >> "$pbuilderrc"
  621. echo 'else' >> "$pbuilderrc"
  622. echo ' LD_PRELOAD="$LD_PRELOAD":libeatmydata.so' >> "$pbuilderrc"
  623. echo 'fi' >> "$pbuilderrc"
  624. echo 'export LD_PRELOAD' >> "$pbuilderrc"
  625. fi
  626. # add ccache to pbuilderrc
  627. if [ "${USE_CCACHE:-}" = true ] ; then
  628. echo 'export CCACHEDIR="/var/cache/pbuilder/ccache"' >> "$pbuilderrc"
  629. fi
  630. use_eatmydata
  631. use_ccache
  632. # allow setting main pbuilder configuration file from outside, then append data
  633. # as needed without actually writing anything to user-provided $PBUILDER_CONFIG
  634. if [ -n "${PBUILDER_CONFIG:-}" ] ; then
  635. echo "*** PBUILDER_CONFIG is set, considering $PBUILDER_CONFIG for pbuilder config ***"
  636. if [ -r "${PBUILDER_CONFIG:-}" ] ; then
  637. echo "*** Adding content of $PBUILDER_CONFIG to pbuilder configfile ***"
  638. cat $PBUILDER_CONFIG >> "$pbuilderrc"
  639. else
  640. echo "*** File $PBUILDER_CONFIG could not be read, ignoring ***"
  641. fi
  642. fi
  643. counter=3600
  644. while ls "${update_lockfile}."* >/dev/null 2>&1 && [ $counter -gt 0 ] ; do
  645. echo "*** Update (or creation) of ${COWBUILDER_BASE} is in progress, waiting up to $counter seconds ***"
  646. sleep 1
  647. counter=$(( counter - 1 ))
  648. done
  649. if [ $counter -eq 0 ] ; then
  650. bailout 1 "Error: ran into timeout because parallel create/update operation for ${COWBUILDER_BASE} didn't finish in time."
  651. fi
  652. touch "${build_lockfile}"
  653. case "$architecture" in
  654. i386)
  655. linux32 sudo DIST="${distribution:-}" ARCH="${architecture:-}" ${ADT:+ADT=$ADT} \
  656. cowbuilder --buildresult "$WORKSPACE"/binaries/ \
  657. --build $sourcefile \
  658. --basepath "${COWBUILDER_BASE}" --debbuildopts "${DEBBUILDOPTS:-}" \
  659. --hookdir "${PBUILDER_HOOKDIR}" --bindmounts "$BINDMOUNTS" --configfile="${pbuilderrc}"
  660. [ $? -eq 0 ] || bailout 1 "Error: Failed to build with cowbuilder."
  661. ;;
  662. amd64|all|*)
  663. sudo DIST="${distribution:-}" ARCH="${architecture:-}" ${ADT:+ADT=$ADT} \
  664. cowbuilder --buildresult "$WORKSPACE"/binaries/ \
  665. --build $sourcefile \
  666. --basepath "${COWBUILDER_BASE}" --debbuildopts "${DEBBUILDOPTS:-}" \
  667. --hookdir "${PBUILDER_HOOKDIR}" --bindmounts "$BINDMOUNTS" --configfile="${pbuilderrc}"
  668. [ $? -eq 0 ] || bailout 1 "Error: Failed to build with cowbuilder."
  669. ;;
  670. *)
  671. bailout 1 "Error: Unsupported architecture: $architecture"
  672. ;;
  673. esac
  674. rm -f "${build_lockfile}"
  675. [ -n "${pbuilderrc:-}" ] && rm -f "$pbuilderrc"
  676. }
  677. # replacement for dcmd, sadly available only in the devscripts package
  678. list_deb_files() {
  679. if [ "$#" -lt 1 ] ; then
  680. echo "Error: list_deb_files function needs a file name as argument." >&2
  681. return 1
  682. fi
  683. local files
  684. for arg in "$@" ; do
  685. if ! [ -r "$arg" ] ; then
  686. echo "Error: could not read $arg" >&2
  687. continue
  688. fi
  689. # cmdline based on usage in dcmd, we're interested only in .deb files though
  690. sed --regexp-extended -n 's,^ [0-9a-f]{32} [0-9]+ ((([a-zA-Z0-9_.-]+/)?[a-zA-Z0-9_.-]+|-) ([a-zA-Z]+|-) )?(.*.deb)$,\5,p' "$arg"
  691. done
  692. }
  693. remove_packages() {
  694. if [ -n "${SKIP_REMOVAL:-}" ] ; then
  695. echo "*** Skipping removal of existing packages as requested via SKIP_REMOVAL ***"
  696. return 0
  697. fi
  698. echo "*** Removing source package version from repository ***"
  699. ${SUDO_CMD:-} reprepro -A source -b "${REPOSITORY}" ${REPREPRO_OPTS} remove "${REPOS}" "${SOURCE_PACKAGE}"
  700. echo "*** Removing previous binary package versions from repository ***"
  701. for p in $(list_deb_files "${WORKSPACE}/binaries/"*"_${newest_version}_${arch}.changes") ; do
  702. file="$(basename $p)"
  703. binpackage="${file%%_*}"
  704. binary_list="${binary_list:-} ${binpackage}"
  705. skip=false # don't skip any package(s) unless it's listed in SKIP_PACKAGE_FROM_REMOVAL
  706. if [ -n "${SKIP_PACKAGE_FROM_REMOVAL:-}" ] ; then
  707. echo "*** SKIP_PACKAGE_FROM_REMOVAL is set [${SKIP_PACKAGE_FROM_REMOVAL}]"
  708. for package in $SKIP_PACKAGE_FROM_REMOVAL ; do
  709. if echo "${package}" | grep -q "${binpackage}" ; then
  710. skip=true
  711. fi
  712. done
  713. fi
  714. if $skip ; then
  715. echo "*** Package '$binpackage' listed in SKIP_PACKAGE_FROM_REMOVAL - skipping removal therefore ***"
  716. elif echo "$file" | egrep -q '_all.deb$'; then
  717. # note: "removesrc" would remove foreign arch files (of different builds)
  718. echo "*** Removing existing package ${binpackage} from repository ${REPOS} (arch all) ***"
  719. ${SUDO_CMD:-} reprepro -b "${REPOSITORY}" ${REPREPRO_OPTS} remove "${REPOS}" "${binpackage}"
  720. else
  721. echo "*** Removing existing package ${binpackage} from repository ${REPOS} for arch ${arch} ***"
  722. ${SUDO_CMD:-} reprepro -A "${arch}" -b "${REPOSITORY}" ${REPREPRO_OPTS} remove "${REPOS}" "${binpackage}"
  723. fi
  724. done
  725. }
  726. remove_missing_binary_packages() {
  727. if [ -n "${SKIP_REMOVAL:-}" ] ; then
  728. echo "*** Skipping removal of existing packages as requested via SKIP_REMOVAL ***"
  729. return 0
  730. fi
  731. echo "*** Checking for missing binary packages to be considered for removal ***"
  732. # In a binary-only build we don't get any arch-all (*_all.deb) packages and
  733. # therefore they won't be listed in the changes file. As a result they would
  734. # be reported as missing from the build and to be considered for removal.
  735. # As we don't want to remove the arch-all package e.g. from the amd64 repos
  736. # in the i386 run we've to skip the removal procedure then.
  737. case "${DEBBUILDOPTS:-}" in
  738. *-B*)
  739. echo "*** Skipping removal of missing binaries as being a binary-only build ***"
  740. return 0
  741. ;;
  742. esac
  743. for p in $(${SUDO_CMD:-} reprepro -b "${REPOSITORY}" ${REPREPRO_OPTS} --list-format '${package}\n' listmatched "${REPOS}" '*' | sort -u); do
  744. echo " $binary_list " | grep -q " $p " || missing_packages="${missing_packages:-} $p"
  745. done
  746. if echo "${missing_packages:-}" | grep -q '.' ; then
  747. echo "*** Binary package(s) found, missing in build version: ${missing_packages:-} ***"
  748. for p in $missing_packages ; do
  749. skip=false # don't skip any package(s) unless it's listed in SKIP_PACKAGE_FROM_REMOVAL
  750. if [ -n "${SKIP_PACKAGE_FROM_REMOVAL:-}" ] ; then
  751. echo "*** SKIP_PACKAGE_FROM_REMOVAL is set [${SKIP_PACKAGE_FROM_REMOVAL}]"
  752. for package in $SKIP_PACKAGE_FROM_REMOVAL ; do
  753. if echo "${package}" | grep -q "${p}" ; then
  754. skip=true
  755. fi
  756. done
  757. fi
  758. if $skip ; then
  759. echo "*** Package '$p' listed in SKIP_PACKAGE_FROM_REMOVAL - skipping removal therefore ***"
  760. else
  761. echo "*** Removing $p from $REPOS to avoid out-of-date data ***"
  762. ${SUDO_CMD:-} reprepro -b "${REPOSITORY}" ${REPREPRO_OPTS} remove "${REPOS}" "${p}"
  763. fi
  764. done
  765. fi
  766. }
  767. reprepro_wrapper() {
  768. if ! [ -d "$REPOSITORY" ] ; then
  769. bailout 1 "Error: repository ${REPOSITORY} does not exist."
  770. fi
  771. ${SUDO_CMD:-} generate-reprepro-codename "${REPOS}"
  772. remove_packages
  773. remove_missing_binary_packages
  774. archall=false
  775. case $architecture in
  776. all) archall=true
  777. architecture='*' # support as file expansion in reprepro cmdline
  778. ;;
  779. esac
  780. echo "*** Including packages in repository $REPOS ***"
  781. ${SUDO_CMD:-} reprepro -b "${REPOSITORY}" ${REPREPRO_OPTS} \
  782. --ignore=wrongdistribution --ignore=uploaders \
  783. include "${REPOS}" "${WORKSPACE}/binaries/"*"_${newest_version}"_${architecture}.changes
  784. [ $? -eq 0 ] || bailout 1 "Error: Failed to include binary package in $REPOS repository."
  785. }
  786. dput_wrapper() {
  787. command -v dput || bailout 1 "Error: dput not found."
  788. archall=false
  789. case $architecture in
  790. all)
  791. archall=true
  792. architecture='*' # support as file expansion in cmdline
  793. ;;
  794. esac
  795. echo "*** Including packages in repository $REPOS ***"
  796. ${SUDO_CMD:-} dput -U -u "${DPUT_HOST:-}" "${WORKSPACE}/binaries/"*"_${newest_version}"_${architecture}.changes
  797. [ $? -eq 0 ] || bailout 1 "Error: Failed to upload binary package to $DPUT_HOST dput host."
  798. }
  799. freight_ensure_repo() {
  800. echo "*** Creating freight directory structure ***"
  801. ${SUDO_CMD:-} mkdir -p "${FREIGHT_VARCACHE}" "${FREIGHT_VARLIB}"
  802. if [ ! -f "$FREIGHT_CONF" ] ; then
  803. echo "*** Creating freight repository configuration in $FREIGHT_CONF ***"
  804. ${SUDO_CMD:-} sh -c "cat > ${FREIGHT_CONF}" <<EOF
  805. # Generated by jenkins-debian-glue
  806. #
  807. # Directories for the Freight library and Freight cache.
  808. # Your web server's document root should point to \$VARCACHE.
  809. VARLIB="${FREIGHT_VARLIB:-/var/lib/freigh}"
  810. VARCACHE="${FREIGHT_VARCACHE:-/var/cache/freight}"
  811. # Default 'Origin' and 'Label' fields for 'Release' files.
  812. ORIGIN="Freight"
  813. LABEL="Freight"
  814. # Cache the control files after each run (on), or regenerate them every
  815. # time (off).
  816. CACHE="off"
  817. # GPG key to sign repositories, derived from jenkins-debian-glue's \$KEY_ID setting
  818. GPG="${KEY_ID:-}"
  819. # Whether to follow symbolic links in \$VARLIB to produce extra components
  820. # in the cache directory (on) or not (off).
  821. SYMLINKS="off"
  822. EOF
  823. fi
  824. [ -f "$FREIGHT_CONF" ] || bailout 1 "Error: Failed to create freight configuration in $FREIGHT_CONF"
  825. }
  826. freight_wrapper() {
  827. freight_ensure_repo
  828. echo "*** Including packages via freight in repository ${FREIGHT_VARLIB}/${REPOS} ***"
  829. ${SUDO_CMD:-} freight add -v -c "$FREIGHT_CONF" "${WORKSPACE}/binaries/"*"_${newest_version}"*"deb" "apt/${REPOS}"
  830. [ $? -eq 0 ] || bailout 1 "Error: Failed to add binary package to repository."
  831. echo "*** Generating freight cache ***"
  832. ${SUDO_CMD:-} freight cache -v -c "$FREIGHT_CONF"
  833. [ $? -eq 0 ] || bailout 1 "Error: Failed to generate freight cache for ${FREIGHT_VARCACHE}."
  834. }
  835. trunk_release() {
  836. # setting TRUNK_RELEASE=true enables release-trunk repository,
  837. # to always get a copy of the package(s) to a central place
  838. if [ -z "${TRUNK_RELEASE:-}" ] ; then
  839. echo "*** TRUNK_RELEASE is not enabled ***"
  840. elif [ "${IGNORE_RELEASE_TRUNK:-}" = "true" ] ; then
  841. echo "*** IGNORE_RELEASE_TRUNK is set, ignoring request to add package(s) to $TRUNK_RELEASE repos ***"
  842. else
  843. echo "*** TRUNK_RELEASE is enabled ($TRUNK_RELEASE) ***"
  844. ${SUDO_CMD:-} generate-reprepro-codename "$TRUNK_RELEASE"
  845. ${SUDO_CMD:-} reprepro -b "${REPOSITORY}" ${REPREPRO_OPTS} --ignore=wrongdistribution copymatched "$TRUNK_RELEASE" "$REPOS" '*'
  846. [ $? -eq 0 ] || bailout 1 "Error: Failed to copy packages from ${REPOS} to ${TRUNK_RELEASE}."
  847. fi
  848. }
  849. # Get rid of *unused* files to avoid uploading unreferenced files to incoming
  850. # directory. When generating the source package on jessie this might result in
  851. # a *.tar.xz file. But when building the binary package for an older release
  852. # like wheezy this might generate a *.tar.gz file. When directly copying those
  853. # files into reprepro's incoming directory (e.g. for release builds) the
  854. # *unused* tarball will be kept around, so let's avoid that.
  855. drop_unused_debfiles() {
  856. if [ "${DROP_UNUSED_DEBFILES:-}" = "false" ] ; then
  857. echo "*** DROP_UNUSED_DEBFILES is disabled, not removing unreferenced artifact files. ***"
  858. return 0
  859. fi
  860. if ! [ -x "$(which dcmd)" ] ; then
  861. echo "Error: dcmd executable not available, please install devscripts package." >&2
  862. exit 1
  863. fi
  864. (
  865. cd "${WORKSPACE}/binaries/"
  866. for file in ./* ; do
  867. filename="$(basename "${file}")" # get rid of trailing ./
  868. if ! dcmd ./*.changes | grep -q -- "${filename}" ; then
  869. echo "*** Removing file ${filename} ***"
  870. rm -f "./${filename}"
  871. fi
  872. done
  873. )
  874. }
  875. release_repos() {
  876. echo "*** Environment variable 'release' is set, running through release steps. ***"
  877. if [ -n "${RELEASE_REPOSITORY:-}" ]; then
  878. local REPOSITORY="${RELEASE_REPOSITORY}"
  879. else
  880. local REPOSITORY="${REPOSITORY}/release/${release}"
  881. fi;
  882. mkdir -p "${REPOSITORY}/incoming/${release}"
  883. mkdir -p "${REPOSITORY}/conf"
  884. if [ "${REMOVE_FROM_RELEASE:-}" = 'true' ]; then
  885. echo "*** REMOVE_FROM_RELEASE is set, trying to remove package(s) from release repository"
  886. REPOS="${release}" remove_packages
  887. fi
  888. # get rid of files that aren't mentioned in the changes files before copying to incoming directory
  889. drop_unused_debfiles
  890. cp "${WORKSPACE}/binaries/"* "${REPOSITORY}/incoming/${release}/"
  891. [ $? -eq 0 ] || bailout 1 "Error: Failed to copy binary packages to release directory."
  892. REPOSITORY=$REPOSITORY generate-reprepro-codename "${release}"
  893. # lock access to file to avoid duplicate entries when two build-and-provide-package
  894. # runs happen at the very same time with regard to conf/incoming setup
  895. (
  896. flock --timeout 5 9 || bailout 1 "Error: could not lock file ${REPOSITORY}/conf/incoming, giving up."
  897. if ! grep -q "^Name: $release$" "${REPOSITORY}/conf/incoming" 2>/dev/null ; then
  898. cat >> "${REPOSITORY}/conf/incoming" << EOF
  899. Name: $release
  900. IncomingDir: incoming/$release
  901. TempDir: tmp
  902. LogDir: log
  903. MorgueDir: ${REPOSITORY}/morgue
  904. Default: $release
  905. Cleanup: unused_files on_deny on_error
  906. EOF
  907. fi
  908. ) 9>/var/lock/jdg-build-and-provide-incoming."$(id -un)" || bailout 1 "Error while setting up incoming repository."
  909. local old_dir=$(pwd)
  910. cd "${REPOSITORY}/incoming/${release}"
  911. ${SUDO_CMD:-} reprepro -b "${REPOSITORY}" ${REPREPRO_OPTS} --ignore=wrongdistribution \
  912. processincoming "${release}" "$(basename ${WORKSPACE}/binaries/*.changes)"
  913. local RC=$?
  914. cd "$old_dir"
  915. if [ $RC -ne 0 ] ; then
  916. bailout 1 "Error: Failed to execute processincoming for release ${release}."
  917. fi
  918. }
  919. deploy_to_releases() {
  920. # support usage of same source package to build binaries for different distributions
  921. if [ -n "${ADJUST_DISTRIBUTION_ONTHEFLY:-}" ] ; then
  922. echo "*** ADJUST_DISTRIBUTION_ONTHEFLY is set, setting Distribution in changes file as requested to ${distribution} ***"
  923. sed -i "s/Distribution: .*/Distribution: ${distribution}/" "${WORKSPACE}/binaries/"*"_${newest_version}"_${architecture}.changes
  924. fi
  925. if [ -n "${USE_FREIGHT:-}" ] ; then
  926. freight_wrapper
  927. # Freight is currently not able to manage release or trunk release repos,
  928. # so this is the stage where we exit in that case.
  929. return 0
  930. fi
  931. if [ -n "${USE_DPUT:-}" ] ; then
  932. dput_wrapper
  933. return 0
  934. fi
  935. if [ -n "${release:-}" ] && [ "$release" != "none" ] && [ "$release" != "trunk" ] && \
  936. # '${release}' is a hidden bomb: when provided through predefined parameters
  937. # from an upstream jenkins job (like foo-binaries receiving the parameters
  938. # from foo-source) but the job (foo-binaries) gets triggered manually (without
  939. # setting the predefined parameters therefore) then ${release} is set to
  940. # '${release}' instead of being empty
  941. [ "${release}" != '${release}' ] ; then
  942. release_repos
  943. else
  944. reprepro_wrapper
  945. trunk_release
  946. fi
  947. }
  948. # make them available for the Jenkin's 'Archiving artifacts'
  949. binaries_to_workspace() {
  950. echo "*** Moving binaries files to workspace. ***"
  951. mv "${WORKSPACE}/binaries/"* "${WORKSPACE}/"
  952. rmdir "${WORKSPACE}/binaries/"
  953. }
  954. # main execution
  955. trap bailout SIGHUP SIGINT SIGQUIT SIGABRT SIGKILL SIGALRM SIGTERM
  956. checks_and_defaults
  957. clean_workspace
  958. identify_package_name
  959. set_base_path
  960. build_info
  961. identify_sourcefile
  962. dist_and_arch_settings
  963. # do not run in repos job?
  964. if [ -n "${PROVIDE_ONLY:-}" ] ; then
  965. echo "*** Config variable 'PROVIDE_ONLY' is set, ignoring request to run cowbuilder. ***"
  966. else
  967. cowbuilder_init
  968. cowbuilder_run
  969. fi
  970. # do not run in binaries job?
  971. if [ -n "${BUILD_ONLY:-}" ] ; then
  972. echo "*** Config variable 'BUILD_ONLY' is set, ignoring request to use local repository. ***"
  973. else
  974. deploy_to_releases
  975. fi
  976. binaries_to_workspace
  977. autopkg_run
  978. autopkgtest_results
  979. if [ -n "${POST_BUILD_HOOK:-}" ] ; then
  980. echo "*** Found environment variable POST_BUILD_HOOK, set to ${POST_BUILD_HOOK:-} ***"
  981. sh ${POST_BUILD_HOOK:-}
  982. fi
  983. bailout 0
  984. # vim:foldmethod=marker ts=2 ft=sh ai expandtab sw=2