BlueGit

timfa posprint

Files

posprint / scripts / publish-aur.sh

publish-aur.sh

publish-aur.sh 4.6 KiB Download Raw
1#!/usr/bin/env bash
2set -euo pipefail
3
4ROOT="$(cd "$(dirname "$0")/.." && pwd)"
5VERSION="${1:-}"
6TMPDIR="${TMPDIR:-/tmp}"
7WORKDIR="$(mktemp -d "$TMPDIR/posprint-aur-XXXXXX")"
8cleanup() { rm -rf "$WORKDIR"; }
9trap cleanup EXIT
10
11cd "$ROOT"
12
13if [[ -z "$VERSION" ]]; then
14 VERSION="$(grep '^version = ' Cargo.toml | sed -E 's/.*"([^"]+)"/\1/')"
15fi
16# Tags may be "0.5.1" or "v0.5.1"; BlueGit archive URLs and pkgver use the bare version.
17VERSION="${VERSION#v}"
18
19for cmd in git makepkg sed sha256sum curl; do
20 command -v "$cmd" >/dev/null || { echo "Missing command: $cmd" >&2; exit 1; }
21done
22if [[ "$(id -u)" -eq 0 ]]; then
23 command -v runuser >/dev/null || { echo "Missing command: runuser" >&2; exit 1; }
24 command -v useradd >/dev/null || { echo "Missing command: useradd" >&2; exit 1; }
25fi
26
27SOURCE_ARCHIVE_URL="${POSPRINT_SOURCE_ARCHIVE_URL:-https://git.timfalken.com/api/v1/projects/timfa/posprint/repository/tags/${VERSION}/archive}"
28BIN_TARBALL_NAME="posprint-${VERSION}-x86_64.tar.gz"
29# Durable BlueGit release-asset URL (not job-artifact permalinks).
30BIN_PACKAGE_URL="${POSPRINT_RELEASE_ASSET_URL:-https://git.timfalken.com/timfa/posprint/-/releases/${VERSION}/assets/${BIN_TARBALL_NAME}}"
31# AUR_DRY_RUN=1: build PKGBUILD/.SRCINFO and commit locally, but never push.
32AUR_DRY_RUN="${AUR_DRY_RUN:-0}"
33
34generate_srcinfo() {
35 # makepkg refuses root. mktemp parents are 0700 — aurbuild must be able to traverse
36 # $WORKDIR or [[ -d $BUILDDIR ]] fails and mkdir then errors.
37 if [[ "$(id -u)" -eq 0 ]]; then
38 local makepkg_user="${AUR_MAKEPKG_USER:-aurbuild}"
39 local build_tmp
40 if ! id "$makepkg_user" >/dev/null 2>&1; then
41 useradd --system --create-home --shell /bin/bash "$makepkg_user"
42 fi
43 chmod 755 "$WORKDIR"
44 chown -R "$makepkg_user:$makepkg_user" "$PWD"
45 build_tmp="$(runuser -u "$makepkg_user" -- mktemp -d)"
46 # Belt-and-suspenders: force BUILDDIR into a dir aurbuild definitely owns.
47 runuser -u "$makepkg_user" -- env BUILDDIR="$build_tmp" makepkg --printsrcinfo > .SRCINFO
48 rm -rf "$build_tmp"
49 chown -R root:root "$PWD"
50 else
51 makepkg --printsrcinfo > .SRCINFO
52 fi
53}
54
55clone_or_init_aur_repo() {
56 local pkgbase="$1"
57 local dest="$2"
58 local remote="ssh://aur@aur.archlinux.org/${pkgbase}.git"
59
60 echo "Cloning AUR repo: $remote"
61 if git clone "$remote" "$dest"; then
62 return 0
63 fi
64
65 echo "AUR repo ${pkgbase} does not exist yet; creating on first push"
66 mkdir -p "$dest"
67 git -C "$dest" init
68 git -C "$dest" checkout -b master
69 git -C "$dest" remote add origin "$remote"
70}
71
72publish_package() {
73 local pkgbase="$1"
74 local template_dir="$2"
75 local archive_sha="$3"
76 local dest="$WORKDIR/${pkgbase}"
77
78 if [[ ! -f "$ROOT/$template_dir/PKGBUILD" ]]; then
79 echo "Missing $template_dir/PKGBUILD template" >&2
80 exit 1
81 fi
82
83 clone_or_init_aur_repo "$pkgbase" "$dest"
84 cd "$dest"
85
86 git config user.name "${AUR_GIT_AUTHOR_NAME:-posprint release bot}"
87 git config user.email "${AUR_GIT_AUTHOR_EMAIL:-noreply@example.invalid}"
88
89 cp "$ROOT/$template_dir/PKGBUILD" PKGBUILD
90 sed -i "s/^pkgver=.*/pkgver=${VERSION}/" PKGBUILD
91 sed -i "s/^pkgrel=.*/pkgrel=1/" PKGBUILD
92 sed -i "s|^sha256sums=.*|sha256sums=('${archive_sha}')|" PKGBUILD
93
94 generate_srcinfo
95
96 if git diff --quiet -- PKGBUILD .SRCINFO 2>/dev/null && git rev-parse --verify HEAD >/dev/null 2>&1; then
97 echo "AUR repo already up to date: ${pkgbase} ${VERSION}"
98 cd "$ROOT"
99 return 0
100 fi
101
102 git add PKGBUILD .SRCINFO
103 if git rev-parse --verify HEAD >/dev/null 2>&1; then
104 git commit -m "upstream release ${VERSION}"
105 else
106 git commit -m "initial import ${VERSION}"
107 fi
108 if [[ "$AUR_DRY_RUN" == "1" ]]; then
109 echo "DRY RUN: would push ${pkgbase} ${VERSION} to AUR (not pushed)"
110 git --no-pager show --stat HEAD
111 else
112 git push -u origin master
113 fi
114
115 echo "AUR updated: ${pkgbase} ${VERSION}"
116 cd "$ROOT"
117}
118
119echo "Checking source tarball: $SOURCE_ARCHIVE_URL"
120TMP_SOURCE="$WORKDIR/source.tar.gz"
121curl -fsSL "$SOURCE_ARCHIVE_URL" -o "$TMP_SOURCE"
122SOURCE_SHA="$(sha256sum "$TMP_SOURCE" | awk '{print $1}')"
123publish_package "posprint" "packaging/aur" "$SOURCE_SHA"
124
125BIN_TARBALL="$ROOT/dist/${BIN_TARBALL_NAME}"
126if [[ -f "$BIN_TARBALL" ]]; then
127 echo "Using local binary tarball: $BIN_TARBALL"
128else
129 echo "Binary tarball not in dist/; downloading $BIN_PACKAGE_URL"
130 mkdir -p "$ROOT/dist"
131 curl -fsSL "$BIN_PACKAGE_URL" -o "$BIN_TARBALL"
132fi
133if [[ ! -s "$BIN_TARBALL" ]]; then
134 echo "Missing binary tarball $BIN_TARBALL (build:linux-arch must produce dist/${BIN_TARBALL_NAME})" >&2
135 exit 1
136fi
137BIN_SHA="$(sha256sum "$BIN_TARBALL" | awk '{print $1}')"
138publish_package "posprint-bin" "packaging/aur-bin" "$BIN_SHA"