Cotton code
178 lines · 7.9 KB · ShellRaw
#!/usr/bin/env bash# Vendor the CI runner binary into native/forgejo-runner.##   apps/cotton/scripts/fetch-forgejo-runner.sh [--arch arm64|x64] [--dest PATH]#                                               [--pin PATH] [--dry-run]## WHICH RUNNER, AND WHY COTTON BUILDS IT ITSELF. The Mac runs# `forgejo-runner` — the SAME program the Linux box runs, at the same major# version, registered against the same Forgejo.## It used to be `gitea-runner`, chosen because Forgejo publishes NO darwin# binary: `forgejo-runner` releases linux-amd64 and linux-arm64 and nothing# else, while gitea published darwin-arm64 and darwin-amd64 for every release.# The protocol is shared, so that looked free. **It was not.** On the first# real run, gitea-runner 3.5.0 sent the JOB'S OWN TOKEN as credentials when it# cloned `actions/checkout@v4` from the actions host# (`https://code.forgejo.org`), and Forgejo refuses a foreign credential:##     Unable to clone https://code.forgejo.org/actions/checkout refs/heads/v4:#     authentication required: Credentials are incorrect or have expired.## Every Mac job failed at its first step while the Linux box's forgejo-runner# fetched the same action from the same host in the same run. So the Mac runs# forgejo-runner too, and the missing darwin build is one CottonDB MAKES:# cross-compiled from the `v13.0.0` tag with the tag's own Makefile, attached# to the `mac-runner-v13.0.0` release as the archive of record, and served from# cottondb.com because the repo is private and a release asset answers# `303 → /user/login` to a machine with no session. The recipe — the Go# toolchain, the ldflags, the two digests, the publish lane — is# the procedure “Hold a repository's bytes in Cotton, and move its origin off the git host” § "Rebuilding the Mac's forgejo-runner".## THE VERSION IS PINNED in `scripts/forgejo-runner.json` with the digest of the# exact bytes that were built, and the digest is checked BEFORE the binary is# put anywhere anything will run it. A download that does not match is not# installed at all: a runner is a program that executes this repository's# workflows with the person's own login environment, which is the last thing to# take on trust. The same digest is checked on the way IN# (the runner publisher refuses to publish bytes that miss the pin),# so the pin is honoured on both sides of the hand-off.## THE RAW BINARY, not an archive. macOS ships no `xz`/`unxz` (checked: neither# is on a stock 27.0 install), and there is nothing to gain: the published# object is the plain executable with its own digest, so "check the archive# before opening it" becomes "check the program before installing it", which is# the same rule with one fewer step in it.## A pin file, an up-to-date check and a refusal. `build-app.sh` copies what# lands here into `Contents/Helpers/forgejo-runner`, where it is signed as# NESTED CODE by `sign-app.sh`'s Helpers glob — no second signing rule.set -euo pipefailAPP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"PIN="$APP_DIR/scripts/forgejo-runner.json"ARCH=""DEST=""DRY_RUN=0while [ $# -gt 0 ]; do  case "$1" in    --arch)      [ $# -ge 2 ] || { echo "[forgejo-runner] --arch needs arm64 or x64" >&2; exit 2; }      ARCH="$2"; shift 2 ;;    --dest)      [ $# -ge 2 ] || { echo "[forgejo-runner] --dest needs a path" >&2; exit 2; }      DEST="$2"; shift 2 ;;    # A pin file OTHER than the committed one. The guard (this script's test)    # hands one whose digest it knows, so the refusal and the install are    # both proven against a stubbed `curl` and no network. Nothing in the app's lanes passes it.    --pin)      [ $# -ge 2 ] || { echo "[forgejo-runner] --pin needs a path" >&2; exit 2; }      PIN="$2"; shift 2 ;;    --dry-run) DRY_RUN=1; shift ;;    *)      echo "usage: fetch-forgejo-runner.sh [--arch arm64|x64] [--dest PATH] [--pin PATH] [--dry-run]" >&2      exit 2 ;;  esacdoneif [ -z "$ARCH" ]; then  case "$(uname -m)" in    arm64) ARCH=arm64 ;;    x86_64) ARCH=x64 ;;    *) echo "[forgejo-runner] unknown architecture $(uname -m)" >&2; exit 1 ;;  esacficase "$ARCH" in  arm64|x64) ;;  *) echo "[forgejo-runner] no pinned asset for architecture '$ARCH' — arm64 or x64" >&2; exit 1 ;;esac# `--dry-run` is allowed anywhere: it reads a JSON file and prints.## VENDORING INTO THE APP is a macOS job — `build-app.sh` and `release-app.sh`# both call this with no `--dest`, and putting a darwin executable in# `apps/cotton/native/` on a Linux box is a bundle nobody can sign. An explicit# `--dest` is a caller that said where, which is what the digest guard (this# script's test) does — and that guard runs in# the `checks` lane, which runs on the Linux CI box. Refusing it there would have# turned the ONE safety property this script has red on every PR.if [ "$DRY_RUN" -eq 0 ] && [ -z "$DEST" ] && [ "$(uname -s)" != "Darwin" ]; then  echo "[forgejo-runner] this vendors a macOS binary for a macOS app" >&2  echo "[forgejo-runner] pass --dest to fetch one somewhere of your own" >&2  exit 1fi[ -f "$PIN" ] || { echo "[forgejo-runner] no pin at $PIN" >&2; exit 1; }read_pin() {  python3 -c 'import json,syspin = json.load(open(sys.argv[1]))keys = sys.argv[2:]node = pinfor key in keys:    node = node[key]print(node)' "$PIN" "$@"}VERSION="$(read_pin version)"BINARY="$(read_pin binary)"URL="$(read_pin assets "$ARCH" url)"WANT="$(read_pin assets "$ARCH" sha256)"DESTINATION="${DEST:-$APP_DIR/native/$BINARY}"if [ "$DRY_RUN" -eq 1 ]; then  # Everything the real run would do, and not one byte over the network. This  # is what CI and a reader check the pin with.  echo "[forgejo-runner] dry run — nothing is downloaded"  echo "[forgejo-runner]   version     v$VERSION"  echo "[forgejo-runner]   arch        darwin-$ARCH"  echo "[forgejo-runner]   url         $URL"  echo "[forgejo-runner]   sha256      $WANT"  echo "[forgejo-runner]   destination $DESTINATION"  exit 0fi# READ THE VERSION, THEN MATCH IT — never `| grep -q`. `grep -q` exits on its# first match and SIGPIPEs the writer, and under `pipefail` the dead writer's# status is the pipeline's, so the up-to-date case reads as "not up to date"# and re-downloads on every build. (The guard in# `scripts/__tests__/mac-shipping.test.ts` holds it to that.)HAVE=""# `if`, not `[ … ] && …`: under `set -e` a false test at the END of a line# ends the script, and the no-runner-yet case is every first build.if [ -x "$DESTINATION" ]; then HAVE="$("$DESTINATION" --version 2>/dev/null || true)"; fiif [ -n "$HAVE" ] && printf '%s\n' "$HAVE" | grep -F "v$VERSION" >/dev/null; then  echo "[forgejo-runner] up to date — v$VERSION darwin-$ARCH"  exit 0fiSCRATCH="$(mktemp -d)"trap 'rm -rf "$SCRATCH"' EXITecho "[forgejo-runner] downloading v$VERSION darwin-$ARCH"curl -fsSL --proto '=https' --tlsv1.2 -o "$SCRATCH/$BINARY" "$URL"# EITHER SPELLING OF THE SAME SUM. macOS ships `shasum`, most Linuxes ship# `sha256sum`, and a missing one is a NAMED refusal rather than an empty `GOT`# that fails the compare below for the wrong reason.if command -v shasum >/dev/null 2>&1; then  GOT="$(shasum -a 256 "$SCRATCH/$BINARY" | awk '{print $1}')"elif command -v sha256sum >/dev/null 2>&1; then  GOT="$(sha256sum "$SCRATCH/$BINARY" | awk '{print $1}')"else  echo "[forgejo-runner] this machine has neither shasum nor sha256sum, so the" >&2  echo "[forgejo-runner] download cannot be checked — nothing was installed." >&2  exit 1fiif [ "$GOT" != "$WANT" ]; then  echo "[forgejo-runner] the download does not match the pinned digest." >&2  echo "[forgejo-runner]   expected $WANT" >&2  echo "[forgejo-runner]   actual   $GOT" >&2  echo "[forgejo-runner] nothing was installed — a runner executes this repository's" >&2  echo "[forgejo-runner] workflows, and one whose bytes are not the pinned ones is not it." >&2  exit 1fimkdir -p "$(dirname "$DESTINATION")"mv "$SCRATCH/$BINARY" "$DESTINATION"chmod +x "$DESTINATION"echo "[forgejo-runner] ${DESTINATION#"$APP_DIR/"} — v$VERSION darwin-$ARCH"