|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# make bash behave |
| 4 | +set -euo pipefail |
| 5 | +IFS=$'\n\t' |
| 6 | + |
| 7 | +# constants |
| 8 | +stdout=1 |
| 9 | +stderr=2 |
| 10 | +success=0 |
| 11 | +failure=1 |
| 12 | +badusage=64 |
| 13 | +noinput=66 |
| 14 | + |
| 15 | +builddir=$(pwd) |
| 16 | + |
| 17 | +# outputs usage message on specified device before exiting with provided status |
| 18 | +usage() { |
| 19 | + cat << 'E_O_USAGE' >&"$1" |
| 20 | +usage: fetch_and_build_pgxn build_type |
| 21 | +
|
| 22 | + build_type: 'release' |
| 23 | +
|
| 24 | +fetch_and_build_pgxn builds a PGXN package using local build files. The build |
| 25 | +type 'release' builds the latest release tag. At present, the only supported |
| 26 | +build is a 'release' build of the 'citus' project. |
| 27 | +E_O_USAGE |
| 28 | + |
| 29 | + exit "${2}"; |
| 30 | +} |
| 31 | + |
| 32 | +if [ "$#" -ne 1 ]; then |
| 33 | + usage $stderr $badusage |
| 34 | +fi |
| 35 | + |
| 36 | +if [ "${1}" = '-h' ]; then |
| 37 | + usage $stdout $success |
| 38 | +fi |
| 39 | + |
| 40 | +# populate variables from packaging metadata file |
| 41 | +# shellcheck source=/dev/null |
| 42 | + |
| 43 | +parent_path=$(dirname "$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )") |
| 44 | +source "${parent_path}/pkgvars" |
| 45 | + |
| 46 | +# set default values for certain packaging variables |
| 47 | +declare pkglatest # to make shellcheck happy |
| 48 | +hubproj="${hubproj:-${pkgname}}" |
| 49 | + |
| 50 | +if [ -z "${pkglatest}" ]; then |
| 51 | + echo "$0: pkgvars file must specify a value for pkglatest" >&2 |
| 52 | + exit $noinput |
| 53 | +fi |
| 54 | + |
| 55 | +echo "header=\"Authorization: token ${GITHUB_TOKEN}\"" > ~/.curlrc |
| 56 | + |
| 57 | +repopath="citusdata/${hubproj}" |
| 58 | + |
| 59 | +case "${1}" in |
| 60 | + release) |
| 61 | + packageversion=${pkglatest%-*} |
| 62 | + releasetag="v${packageversion/'~'/-}" |
| 63 | + |
| 64 | + gitsha=$(curl -s "https://api.github.com/repos/${repopath}/git/refs/tags/${releasetag}" | \ |
| 65 | + jq -r '.object.sha') |
| 66 | + if [ "${gitsha}" == 'null' ]; then |
| 67 | + echo "$0: could not determine commit for git tag ${releasetag}" >&2 |
| 68 | + exit $failure |
| 69 | + fi |
| 70 | + |
| 71 | + verified=$(curl -sH 'Accept:application/vnd.github.cryptographer-preview+sha' \ |
| 72 | + "https://api.github.com/repos/${repopath}/git/tags/${gitsha}" | \ |
| 73 | + jq -r '.verification.verified') |
| 74 | + if [ "${verified}" != 'true' ]; then |
| 75 | + echo "$0: could not verify signature for git tag ${releasetag}" >&2 |
| 76 | + exit $failure |
| 77 | + fi |
| 78 | + ;; |
| 79 | + *) |
| 80 | + echo "$0: unknown build_type -- ${1}" >&2 |
| 81 | + usage $stderr $badusage |
| 82 | + ;; |
| 83 | +esac |
| 84 | + |
| 85 | +tarballpath="${builddir}/${pkgname}_${packageversion}.orig.tar.gz" |
| 86 | +packagepath="${builddir}/${pkgname}-${packageversion}" |
| 87 | +finalpath="${packagepath}.zip" |
| 88 | + |
| 89 | +curl -sL "https://api.github.com/repos/${repopath}/tarball/${gitsha}" \ |
| 90 | + -o "${tarballpath}" |
| 91 | + |
| 92 | +mkdir -p "${packagepath}" |
| 93 | +tar xf "${tarballpath}" -C "${packagepath}" --strip-components 1 |
| 94 | + |
| 95 | +cp "${builddir}/META.json" "${packagepath}/META.json" |
| 96 | + |
| 97 | +# remove extraneous slashes: zip doesn't like them |
| 98 | +zip -qr "${finalpath//\/\//\/}" "${packagepath//\/\//\/}" |
| 99 | + |
| 100 | +rm ~/.curlrc |
| 101 | + |
0 commit comments