#!/bin/sh # Publish what `make release` built as a release on Gitea, which is where every Ham Lab # looks for a newer build (Settings → Updates, and `update.url` in config.json). # # make release && scripts/gitea-release.sh [--force] # # The token needs write:repository and is read from $HAMLAB_PUBLISH_TOKEN, else from # ~/.config/hamlab-publish-token. It is never printed. # # A release is a deliberate act, not part of `make deploy`: deploying pushes this build to # the machines this hub owns, while publishing offers it to every install everywhere. set -eu REPO=${HAMLAB_REPO:-https://git.bigham.dev/api/v1/repos/mitchell/hamlab} DIST=${DIST:-dist} FORCE=0 [ "${1:-}" != "--force" ] || FORCE=1 die() { printf '%s\n' "$*" >&2; exit 1; } [ -f "$DIST/VERSION" ] || die "no $DIST/VERSION — run: make release" VERSION=$(head -n 1 "$DIST/VERSION" | tr -cd 'A-Za-z0-9._+-') [ -n "$VERSION" ] || die "$DIST/VERSION is empty" TOKEN=${HAMLAB_PUBLISH_TOKEN:-} if [ -z "$TOKEN" ] && [ -r "$HOME/.config/hamlab-publish-token" ]; then TOKEN=$(cat "$HOME/.config/hamlab-publish-token") fi [ -n "$TOKEN" ] || die "no publish token: set HAMLAB_PUBLISH_TOKEN or write ~/.config/hamlab-publish-token (Gitea → Settings → Applications, scope write:repository)" api() { # method path [curl args...] m=$1; p=$2; shift 2 curl -sS -X "$m" -H "Authorization: token $TOKEN" "$REPO$p" "$@" } SHA=$(git rev-parse HEAD) echo "publishing $VERSION ($(echo "$SHA" | cut -c1-8)) to $REPO" # An existing release for this version is replaced only when asked: re-publishing a version # with different binaries is how an install ends up with something nobody can reproduce. existing=$(api GET "/releases/tags/$VERSION" | python3 -c 'import json,sys try: d = json.load(sys.stdin) except Exception: d = {} print(d.get("id") or "")' 2>/dev/null || true) if [ -n "$existing" ]; then [ "$FORCE" = 1 ] || die "$VERSION is already published (release $existing). Re-publish with --force." echo " replacing release $existing" api DELETE "/releases/$existing" -o /dev/null fi notes=$(git log -1 --pretty=%B | head -20) payload=$(python3 -c 'import json,sys; print(json.dumps({"tag_name": sys.argv[1], "target": sys.argv[2], "name": sys.argv[1], "body": sys.argv[3], "draft": False, "prerelease": False}))' "$VERSION" "$SHA" "$notes") id=$(printf '%s' "$payload" | api POST /releases -H 'Content-Type: application/json' -d @- | python3 -c 'import json,sys d = json.load(sys.stdin) if not d.get("id"): sys.exit("gitea refused the release: %s" % d.get("message", d)) print(d["id"])') [ -n "$id" ] || die "could not create the release" echo " release $id created" n=0 for f in "$DIST"/hamlab-linux-* "$DIST/SHA256SUMS" "$DIST/install.sh" "$DIST/VERSION"; do [ -f "$f" ] || continue name=$(basename "$f") api POST "/releases/$id/assets?name=$name" -F "attachment=@$f" -o /dev/null n=$((n + 1)) echo " + $name" done echo "published $VERSION with $n assets" echo " every Ham Lab pointed at this repo will offer it in Settings → Updates"