#!/usr/bin/env bash # KVT fork: publish the AUR package kvt-rusty-teams-bin for a release tag on git.kvt.nl. # # Same approach and secrets as BlueGit's publish_aur (git.timfalken.com/timfa/bluegit): # AUR_SSH_PRIVATE_KEY or AUR_SSH_PRIVATE_KEY_B64 the AUR account's SSH key # AUR_GIT_AUTHOR_NAME, AUR_GIT_AUTHOR_EMAIL name and e-mail in the AUR commit # Without a key the script says so and exits 0: the release itself is not blocked. # # Usage: packaging/kvt/publish-aur.sh [tag] (default: $CI_COMMIT_TAG) # packaging/kvt/publish-aur.sh --render PKGBUILD only, for tests set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PKG="${AUR_PKG:-kvt-rusty-teams-bin}" RELEASES="https://git.kvt.nl/api/v1/projects/ict/rusty-teams/releases" # pkgver may not contain '-': v0.6.1-kvt.2 becomes 0.6.1.kvt.2 (vercmp still orders it). pkgver_of_tag() { local version="${1#v}" [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.]+)?$ ]] || { echo "tag is not v..[-suffix]: $1" >&2 return 1 } printf '%s\n' "${version//-/.}" } render() { local tag="$1" sha="$2" out="$3" pkgver [[ "$sha" =~ ^[0-9a-f]{64}$ ]] || { echo "sha256 must be 64 hex chars" >&2; return 1; } pkgver="$(pkgver_of_tag "$tag")" mkdir -p "$out" sed -e "s/^pkgver=.*/pkgver=${pkgver}/" \ -e "s/^pkgrel=.*/pkgrel=1/" \ -e "s/^_tag=.*/_tag=${tag}/" \ -e "s/^sha256sums=.*/sha256sums=('${sha}')/" \ "$HERE/aur/PKGBUILD" > "$out/PKGBUILD" } # makepkg refuses to run as root; CI jobs are root. as_builder() { if [[ "$(id -u)" -eq 0 ]]; then id aurbuild >/dev/null 2>&1 || useradd --system --create-home --shell /bin/bash aurbuild chown -R aurbuild:aurbuild "$1" (cd "$1" && runuser -u aurbuild -- "${@:2}") else (cd "$1" && "${@:2}") fi } if [[ "${1:-}" == "--render" ]]; then render "${2:?tag}" "${3:?sha256}" "${4:?out-dir}" exit 0 fi TAG="${1:-${CI_COMMIT_TAG:-}}" [[ -n "$TAG" ]] || { echo "No tag: run this in a tag pipeline or pass the tag." >&2; exit 1; } if [[ -z "${AUR_SSH_PRIVATE_KEY:-}" && -z "${AUR_SSH_PRIVATE_KEY_B64:-}" ]]; then echo "AUR: geen AUR_SSH_PRIVATE_KEY of AUR_SSH_PRIVATE_KEY_B64 gezet; $PKG wordt overgeslagen." echo "Zet die (beschermde) geheimen op ict/rusty-teams en bescherm het tagpatroon v*." exit 0 fi for cmd in git ssh curl sha256sum makepkg sed; do command -v "$cmd" >/dev/null || { echo "Missing command: $cmd" >&2; exit 1; } done VERSION="${TAG#v}" TARBALL="rusty-teams-${VERSION}-linux-x86_64.tar.gz" WORKDIR="$(mktemp -d "${TMPDIR:-/tmp}/kvt-aur-XXXXXX")" trap 'rm -rf "$WORKDIR"' EXIT chmod 755 "$WORKDIR" echo "Downloading $TARBALL from the $TAG release" curl -fsSL --retry 3 "$RELEASES/$TAG/assets/$TARBALL" -o "$WORKDIR/$TARBALL" SHA="$(sha256sum "$WORKDIR/$TARBALL" | awk '{print $1}')" "$HERE/aur-ssh-setup.sh" export GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=yes -o BatchMode=yes" dest="$WORKDIR/$PKG" remote="ssh://aur@aur.archlinux.org/${PKG}.git" echo "Cloning $remote" if ! git clone -q "$remote" "$dest"; then echo "AUR repo $PKG does not exist yet; it is created on the first push" mkdir -p "$dest" git -C "$dest" init -q git -C "$dest" checkout -q -b master git -C "$dest" remote add origin "$remote" fi git config --global --add safe.directory '*' render "$TAG" "$SHA" "$dest" # A real makepkg run: downloads the tarball again, checks the sum and packages it. build="$WORKDIR/build" mkdir -p "$build" cp "$dest/PKGBUILD" "$build/" as_builder "$build" makepkg --nodeps --noconfirm --force as_builder "$build" makepkg --printsrcinfo > "$dest/.SRCINFO" cd "$dest" git config user.name "${AUR_GIT_AUTHOR_NAME:-KVT release bot}" git config user.email "${AUR_GIT_AUTHOR_EMAIL:-hephaestus@noreply.kvt.nl}" git add PKGBUILD .SRCINFO if git rev-parse --verify -q HEAD >/dev/null && git diff --cached --quiet; then echo "AUR repo already up to date: $PKG $TAG" exit 0 fi git commit -q -m "${TAG}" git push -q origin HEAD:master echo "AUR updated: $PKG $(pkgver_of_tag "$TAG")"