wrolpi/release/release-watch.sh
Roland 30550f4c5b
release: address Greptile review + add opaque notify hook
Greptile (PR #448):
- C1 (P1): release-watch.sh compared the GitHub tag (minus 'v') against the
  manifest's .version (from version.txt content). If a tag and its version.txt
  ever diverged, the guard never matched and it rebuilt every hour forever.
  Compare the manifest's .tag against the release tag directly -- exact and
  immune to version-string formatting. Drops the now-unused normalize().
- C2 (P2): document that .tag is the built git ref (release tag for automated
  builds, branch for manual) and that consumers use .version for display.
- C3 (P2): document that newest-by-publish-date is the intended trigger and
  that prereleases are deliberately included (every release is -beta), so we
  do not filter on .prerelease as suggested.

Also add an optional completion notify hook: release.sh invokes
$WROLPI_RELEASE_NOTIFY with a success/failure message (no-op if unset, skipped
on dry-run). The public script names no path or tool; the real notifier stays
on the host (commented Environment= placeholder in the service unit).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-30 14:23:19 -06:00

94 lines
3.8 KiB
Bash
Executable file

#!/bin/bash
# Poll GitHub for a newly-published WROLPi release and build it.
#
# This is the *outbound* poller that replaces an inbound webhook: GitHub never
# connects to this machine. Once an hour (via the systemd timer) we ask the
# public GitHub API for the newest release, compare its version to whatever is
# already published in latest.json on the CDN, and if they differ we run
# release.sh to build and publish it. latest.json is the source of truth for
# "already built" -- there is no local bookkeeping for success.
#
# A local state file is used only to throttle retries of a release that fails
# to build, so a permanently-broken release does not rebuild every hour
# forever.
#
# ./release-watch.sh # check once and act
#
# Honors $S3CFG (passed through to release.sh) and these optional env vars:
# STATE_DIR where to keep the lock + failure marker (default /var/lib/wrolpi-release)
# MAX_RETRIES how many times to auto-retry a failing tag (default 3)
set -euo pipefail
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
REPO="lrnselfreliance/wrolpi"
CDN_BASE="https://wrolpi.nyc3.cdn.digitaloceanspaces.com"
STATE_DIR="${STATE_DIR:-/var/lib/wrolpi-release}"
MAX_RETRIES="${MAX_RETRIES:-3}"
LOCK="${STATE_DIR}/build.lock"
FAIL_STATE="${STATE_DIR}/failed-tag"
mkdir -p "${STATE_DIR}"
log() { echo "[release-watch] $*"; }
# Newest non-draft release tag. GitHub's /releases list is ordered by creation
# date, so [0] is the most recently *published* release -- exactly the trigger
# we want. Prereleases (-beta) are intentionally included (every WROLPi release
# is a prerelease), so we deliberately do NOT filter on `.prerelease`. Read-only
# and unauthenticated; the 60 req/hr public limit is irrelevant at hourly cadence.
LATEST_TAG=$(curl -fsSL -H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${REPO}/releases" \
| jq -r '[.[] | select(.draft == false)][0].tag_name // empty')
[ -n "${LATEST_TAG}" ] || { log "No releases found; nothing to do."; exit 0; }
# What is already built? release.sh records the git ref it built in the
# manifest's `.tag`. Compare that directly against the release tag: it is an
# exact match and immune to any divergence between a tag name and the
# version.txt content it carries. (Comparing versions could loop forever -- a
# build that succeeds but whose .version never equals the tag would rebuild
# every hour.) A missing manifest (404) means "nothing built yet".
PUBLISHED_TAG=$(curl -fsSL "${CDN_BASE}/latest.json" 2>/dev/null \
| jq -r '.tag // empty' || true)
if [ "${PUBLISHED_TAG}" = "${LATEST_TAG}" ]; then
log "Up to date (published ${LATEST_TAG}); nothing to do."
exit 0
fi
log "New release ${LATEST_TAG} (published: ${PUBLISHED_TAG:-none})."
# Skip a tag that has already failed MAX_RETRIES times -- needs a human.
if [ -f "${FAIL_STATE}" ]; then
read -r failed_tag failed_count < "${FAIL_STATE}" || true
if [ "${failed_tag}" = "${LATEST_TAG}" ] && [ "${failed_count:-0}" -ge "${MAX_RETRIES}" ]; then
log "ERROR: ${LATEST_TAG} has failed ${failed_count} times; not auto-retrying."
log "Investigate, then run release.sh manually or remove ${FAIL_STATE}."
exit 1
fi
fi
# Only one build at a time. If the timer fires mid-build, just exit.
exec 9>"${LOCK}"
if ! flock -n 9; then
log "A build is already running; skipping this check."
exit 0
fi
log "Building ${LATEST_TAG}..."
if "${SCRIPT_DIR}/release.sh" -b "${LATEST_TAG}"; then
log "Published ${LATEST_TAG}."
rm -f "${FAIL_STATE}"
else
rc=$?
count=1
if [ -f "${FAIL_STATE}" ]; then
read -r ft fc < "${FAIL_STATE}" || true
[ "${ft}" = "${LATEST_TAG}" ] && count=$(( ${fc:-0} + 1 ))
fi
echo "${LATEST_TAG} ${count}" > "${FAIL_STATE}"
log "ERROR: build of ${LATEST_TAG} failed (attempt ${count}, exit ${rc})."
exit "${rc}"
fi