|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# make bash behave |
| 4 | +set -euo pipefail |
| 5 | +# In one branch we execute commands inside One Branch steps, since One Branch does not allow executing docker inside |
| 6 | +# docker. Additionally, Onebranch needs containers not to close so we make it hang for OneBranch to be able to |
| 7 | +# execute commands. |
| 8 | +if [ "${CONTAINER_BUILD_RUN_ENABLED:-""}" == "" ]; then |
| 9 | + echo "INFO: Image working in waiting mode. Not executing build script" |
| 10 | + tail -f /dev/null |
| 11 | +fi |
| 12 | + |
| 13 | +IFS=$'\n\t' |
| 14 | + |
| 15 | +# constants |
| 16 | +stdout=1 |
| 17 | +stderr=2 |
| 18 | +success=0 |
| 19 | +failure=1 |
| 20 | +badusage=64 |
| 21 | +noinput=66 |
| 22 | + |
| 23 | +nextversion='0.0.0' |
| 24 | +builddir=$(pwd) |
| 25 | + |
| 26 | +# outputs usage message on specified device before exiting with provided status |
| 27 | +usage() { |
| 28 | + cat << 'E_O_USAGE' >&"$1" |
| 29 | +usage: fetch_and_build_deb build_type |
| 30 | +
|
| 31 | + build_type: 'release', 'nightly', or a valid git reference |
| 32 | +
|
| 33 | +fetch_and_build_deb builds Debian packages using local build files. The build |
| 34 | +type 'release' builds the latest release tag, 'nightly' builds a nightly from |
| 35 | +the latest 'master' commit, and any other type is interpreted as a git ref to |
| 36 | +facilitate building one-off packages for customers. |
| 37 | +E_O_USAGE |
| 38 | + |
| 39 | + exit "${2}"; |
| 40 | +} |
| 41 | + |
| 42 | +# sets the next version variable used during non-release builds |
| 43 | +setnextversion() { |
| 44 | + baseversion=$(echo "$1" | tr '~' '-' | tr '_' '-' | cut -d- -f1) |
| 45 | + baseversion="${baseversion%.citus}" |
| 46 | + nextversion=$(echo "$baseversion" | perl -pe 's/^(\d+\.)(\d+)(\.\d+)$/$1.($2+1).".0"/e') |
| 47 | +} |
| 48 | + |
| 49 | +if [ "$#" -ne 1 ]; then |
| 50 | + usage $stderr $badusage |
| 51 | +fi |
| 52 | + |
| 53 | +if [ "${1}" = '-h' ]; then |
| 54 | + usage $stdout $success |
| 55 | +fi |
| 56 | + |
| 57 | +# populate variables from packaging metadata file |
| 58 | +# shellcheck source=/dev/null |
| 59 | +source /buildfiles/pkgvars |
| 60 | +# Read PostgreSQL versions from file. |
| 61 | +# supported-postgres file is derived file from postgres-matrix.yml file by citus_package. |
| 62 | +# If file does not exist, skip populating file |
| 63 | +[[ -f "/buildfiles/supported-postgres" ]] && source /buildfiles/supported-postgres |
| 64 | + |
| 65 | +# Fetch pkgname, hubproj, nightlyref, versioning from pkgvars file |
| 66 | +declare pkglatest # to make shellcheck happy |
| 67 | +pkgname="${deb_pkgname:-${pkgname}}" |
| 68 | +hubproj="${hubproj:-${pkgname}}" |
| 69 | +nightlyref="${nightlyref:-master}" |
| 70 | +versioning="${versioning:-simple}" |
| 71 | +if [[ "${pkglatest}" == *"beta"* ]]; then |
| 72 | + release_type="beta" |
| 73 | +else |
| 74 | + release_type="stable" |
| 75 | +fi |
| 76 | +# Fetch pg release and nightly versions from supported-postgres file which is originated from postgres-matrix.yml file |
| 77 | +# If this file is not found, releasepg and nightlypg parameters from pkgvars are used for defining pg versions |
| 78 | +pg_release_versions="${release_versions:-${releasepg}}" |
| 79 | +pg_nightly_versions="${nightly_versions:-${nightlypg}}" |
| 80 | + |
| 81 | + |
| 82 | +echo "Postgres versions:" |
| 83 | +echo "Release Versions: ${pg_release_versions}" |
| 84 | +echo "Nightly Versions: ${pg_nightly_versions}" |
| 85 | + |
| 86 | +if [ -z "${pkglatest}" ]; then |
| 87 | + echo "$0: pkgvars file must specify a value for pkglatest" >&2 |
| 88 | + exit $noinput |
| 89 | +fi |
| 90 | + |
| 91 | +echo "header=\"Authorization: token ${GITHUB_TOKEN}\"" > ~/.curlrc |
| 92 | + |
| 93 | +export NAME |
| 94 | +NAME=$(determine_name) |
| 95 | + |
| 96 | +export EMAIL |
| 97 | +EMAIL=$(determine_email) |
| 98 | + |
| 99 | +cp -R /buildfiles/debian "${builddir}" |
| 100 | +repopath="citusdata/${hubproj}" |
| 101 | + |
| 102 | +case "${1}" in |
| 103 | + release) |
| 104 | + packageversion=${pkglatest%-*} |
| 105 | + releasetag="v${packageversion/'~'/-}" |
| 106 | + releasetag="${releasetag%.citus}" |
| 107 | + |
| 108 | + echo "Executing release build for tag ${releasetag}" |
| 109 | + |
| 110 | + gitsha=$(curl -s "https://api.github.com/repos/${repopath}/git/refs/tags/${releasetag}" | \ |
| 111 | + jq -r '.object.sha') |
| 112 | + if [ "${gitsha}" == 'null' ]; then |
| 113 | + echo "$0: could not determine commit for git tag ${releasetag}" >&2 |
| 114 | + exit $failure |
| 115 | + fi |
| 116 | + |
| 117 | + verified=$(curl -sH 'Accept:application/vnd.github.cryptographer-preview+sha' \ |
| 118 | + "https://api.github.com/repos/${repopath}/git/tags/${gitsha}" | \ |
| 119 | + jq -r '.verification.verified') |
| 120 | + if [ "${verified}" != 'true' ]; then |
| 121 | + echo "$0: could not verify signature for git tag ${releasetag}" >&2 |
| 122 | + exit $failure |
| 123 | + fi |
| 124 | + |
| 125 | + echo "${pg_release_versions}" | tr ',' '\n' > "${builddir}/debian/pgversions" |
| 126 | + ;; |
| 127 | + *) |
| 128 | + if [ "${1}" == 'nightly' ]; then |
| 129 | + ref=${nightlyref} |
| 130 | + infix='git' |
| 131 | + else |
| 132 | + ref=${1} |
| 133 | + infix='pre' |
| 134 | + fi |
| 135 | + |
| 136 | + setnextversion "${pkglatest}" |
| 137 | + |
| 138 | + set +e |
| 139 | + gitsha=$(curl -sfH 'Accept:application/vnd.github.v3.sha' \ |
| 140 | + "https://api.github.com/repos/${repopath}/commits/${ref}") |
| 141 | + if [ "${?}" -ne 0 ]; then |
| 142 | + echo "$0: could not determine commit for git ref ${ref}" >&2 |
| 143 | + exit $failure |
| 144 | + fi |
| 145 | + set -e |
| 146 | + |
| 147 | + timestamp=$(date +'%Y%m%d') |
| 148 | + |
| 149 | + packagesuffix="${infix}.${timestamp}.${gitsha:0:7}" |
| 150 | + packageversion="${nextversion}.citus~${packagesuffix}" |
| 151 | + export CONF_EXTRA_VERSION="+${packagesuffix}" |
| 152 | + |
| 153 | + echo "${pg_nightly_versions}" | tr ',' '\n' > "${builddir}/debian/pgversions" |
| 154 | + ;; |
| 155 | +esac |
| 156 | + |
| 157 | +tarballpath="${builddir}/${pkgname}_${packageversion}.orig.tar.gz" |
| 158 | +packagepath="${builddir}/${packageversion}" |
| 159 | + |
| 160 | +curl -sL "https://api.github.com/repos/${repopath}/tarball/${gitsha}" \ |
| 161 | + -o "${tarballpath}" |
| 162 | + |
| 163 | +mkdir -p "${packagepath}" |
| 164 | +tar xf "${tarballpath}" -C "${packagepath}" --strip-components 1 |
| 165 | + |
| 166 | +# git metadata needs to be setup to initialize submodules |
| 167 | +# in repos which rely on git submodules |
| 168 | +if [[ -f "${packagepath}/.gitmodules" ]]; then |
| 169 | + setup_submodules "${packagepath}" |
| 170 | +fi |
| 171 | + |
| 172 | +# add our email/name to debian control file as uploader if not a release |
| 173 | +if [ "${1}" != 'release' ]; then |
| 174 | + sed -i -E "/^Uploaders:/s/ .+$/ ${NAME} <${EMAIL}>/" "${builddir}/debian/control.in" |
| 175 | +fi |
| 176 | + |
| 177 | +cp -R "${builddir}/debian" "${packagepath}/debian" |
| 178 | + |
| 179 | +cd "${packagepath}" |
| 180 | + |
| 181 | +case "${1}" in |
| 182 | + release) |
| 183 | + # add minor/major version to package name if using fancy versioning |
| 184 | + if [ "${versioning}" == 'fancy' ]; then |
| 185 | + declare release_prefix='' |
| 186 | + suffix=$(echo "${packageversion}" | grep -oE '^[0-9]+\.[0-9]+') |
| 187 | + if [ "${release_type}" == 'stable' ]; then |
| 188 | + release_prefix=${suffix} |
| 189 | + else |
| 190 | + release_prefix="${release_type}-${suffix}" |
| 191 | + fi |
| 192 | + sed -i "/^Package:/ s/$/-${release_prefix}/" debian/control.in |
| 193 | + sed -i "/postgresql-%v-${pkgname}/ s/$/-${release_prefix}/" debian/rules |
| 194 | + fi |
| 195 | + ;; |
| 196 | + nightly) |
| 197 | + msg="Nightly package. Built from ${nightlyref} " |
| 198 | + msg+=$(date +'on %l:%M %p (%Z) on %A, %B %Y' | tr -s ' ') |
| 199 | + dch -v "${packageversion}-1" -D experimental -u low "${msg}" |
| 200 | + ;; |
| 201 | + *) |
| 202 | + msg="Custom package. Built from ${gitsha:0:7} " |
| 203 | + msg+=$(date +'on %l:%M %p (%Z) on %A, %B %Y' | tr -s ' ') |
| 204 | + dch -v "${packageversion}-1" -D UNRELEASED -u low "${msg}" |
| 205 | + ;; |
| 206 | +esac |
| 207 | + |
| 208 | + |
| 209 | +pg_buildext updatecontrol |
| 210 | + |
| 211 | +procs="$(nproc)" |
| 212 | +mjobs="$(expr $procs + 1)" |
| 213 | + |
| 214 | +DEB_BUILD_OPTIONS="parallel=${mjobs}" debuild \ |
| 215 | + --prepend-path /usr/local/bin \ |
| 216 | + --preserve-envvar CONF_EXTRA_VERSION \ |
| 217 | + --preserve-envvar UNENCRYPTED_PACKAGE \ |
| 218 | + --preserve-envvar PACKAGE_ENCRYPTION_KEY \ |
| 219 | + --preserve-envvar MSRUSTUP_PAT \ |
| 220 | + --preserve-envvar CRATES_IO_MIRROR_FEED_TOKEN \ |
| 221 | + -uc -us -B --lintian-opts --profile debian --allow-root |
| 222 | + |
| 223 | +## Rename all *.ddeb files to *.deb since we upload debug packages with .deb suffix |
| 224 | +# .ddeb suffix is automatically chosen when naming debug packages for Ubuntu |
| 225 | +# releases. For this reason, we rename all *.ddeb files to *.deb since we upload |
| 226 | +# debug packages with .deb suffix. |
| 227 | +for file in ../*.ddeb; do |
| 228 | + if [ -e "${file}" ]; then |
| 229 | + echo "Renaming ${file} to ${file%.ddeb}.deb" |
| 230 | + mv "${file}" "${file%.ddeb}.deb"; |
| 231 | + else |
| 232 | + echo "There are no files with ddeb extension to rename." |
| 233 | + fi |
| 234 | +done |
| 235 | +## Copy all deb packages out of docker image |
| 236 | +cp ../*.deb /packages |
0 commit comments