diff --git a/.github/gh-release-mirror.sh b/.github/gh-release-mirror.sh new file mode 100755 index 000000000..fec18aedf --- /dev/null +++ b/.github/gh-release-mirror.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +# Mirror the public releases of git.rfa.cz/tth-pub/meshcore-tth to the GitHub +# fork's Releases (TeTeHacko/MeshCore-TTH), called by pub-release.yml after +# each release. git.rfa.cz stays the primary source: auto-OTA, the docs and +# the release notes' links point there, and integrity rests on the signed +# SHA256SUMS, not on the host. +# +# Converges instead of copying "the one new release": every run makes the +# GitHub side equal to the newest $KEEP releases of Gitea -- creates what is +# missing, uploads assets a failed run left out, deletes GitHub releases that +# Gitea pruned (their tags stay). A GitHub outage is mended by the next run. +# +# A release is created only once GitHub has its commit (the export pushes it +# as branch `tth` in tth-upstream-sync.yml); until then it is skipped with a +# warning and the next run picks it up. +# +# GH_TOKEN=... .github/gh-release-mirror.sh # mirror +# .github/gh-release-mirror.sh --dry-run # print the plan only (no token needed) +# +# env: GH_TOKEN (fine-grained, Contents: write on GH_REPO), GITEA (default +# https://git.rfa.cz), SRC_REPO (tth-pub/meshcore-tth), GH_REPO +# (TeTeHacko/MeshCore-TTH), KEEP (5). +set -euo pipefail + +case "${1:-}" in +-h | --help) + sed -n '2,/^set -euo/p' "$0" | sed '$d; s/^# \{0,1\}//' + exit 0 + ;; +--dry-run) DRY=1 ;; +"") DRY=0 ;; +*) + echo "usage: $0 [--dry-run]" >&2 + exit 2 + ;; +esac + +GITEA=${GITEA:-https://git.rfa.cz} +SRC_REPO=${SRC_REPO:-tth-pub/meshcore-tth} +GH_REPO=${GH_REPO:-TeTeHacko/MeshCore-TTH} +KEEP=${KEEP:-5} +src="$GITEA/api/v1/repos/$SRC_REPO" +gh="https://api.github.com/repos/$GH_REPO" +up="https://uploads.github.com/repos/$GH_REPO" +if [ "$DRY" = 0 ] && [ -z "${GH_TOKEN:-}" ]; then + echo "GH_TOKEN not set: nothing mirrored" >&2 + exit 0 +fi + +# GitHub API call; the token goes through --config on stdin, never argv +ghapi() { + local auth="" + [ -n "${GH_TOKEN:-}" ] && auth="header = \"Authorization: Bearer $GH_TOKEN\"" + curl -sS --retry 3 --config - -H 'Accept: application/vnd.github+json' \ + -H 'X-GitHub-Api-Version: 2022-11-28' "$@" <<<"$auth" +} +enc() { jq -rn --arg s "$1" '$s|@uri'; } + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT + +# newest $KEEP of ours on Gitea, oldest first (GitHub orders by creation) +curl -sSf --retry 3 "$src/releases?limit=50" | + jq -c --argjson k "$KEEP" 'map(select(.tag_name | startswith("tth-") or test("[+]tth[.]"))) + | sort_by(.created_at) | reverse | .[:$k] | reverse | .[]' >"$tmp/src.jsonl" +newest=$(tail -1 "$tmp/src.jsonl" | jq -r .tag_name) +ghapi -f "$gh/releases?per_page=100" | jq -c '.[]' >"$tmp/gh.jsonl" + +while read -r rel; do + tag=$(jq -r .tag_name <<<"$rel") + ghrel=$(jq -c --arg t "$tag" 'select(.tag_name == $t)' "$tmp/gh.jsonl") + if [ -z "$ghrel" ]; then + sha=$(curl -sSf --retry 3 "$src/tags/$(enc "$tag")" | jq -r .commit.sha) + if [ "$(ghapi -o /dev/null -w '%{http_code}' "$gh/commits/$sha")" != 200 ]; then + echo "::warning::$tag: commit $sha not on GitHub yet, next run" + continue + fi + echo "create $tag ($sha)" + [ "$DRY" = 1 ] && continue + body=$( + echo "> **Zrcadlo** · mirror of $GITEA/$SRC_REPO/releases/tag/$(enc "$tag") -- primární zdroj" + echo "> (auto-OTA, podpisy \`SHA256SUMS\`) je git.rfa.cz." + echo + jq -r .body <<<"$rel" + ) + latest=false + [ "$tag" = "$newest" ] && latest=true + jq -n --arg t "$tag" --arg c "$sha" --arg n "$(jq -r .name <<<"$rel")" --arg b "$body" --arg l "$latest" \ + '{tag_name: $t, target_commitish: $c, name: $n, body: $b, make_latest: $l}' >"$tmp/new.json" + ghrel=$(ghapi -f -X POST -H 'Content-Type: application/json' --data-binary "@$tmp/new.json" "$gh/releases") + fi + id=$(jq -r .id <<<"$ghrel") + have=$(jq -r '.assets[]? | (.label // "" | select(. != "")) // .name' <<<"$ghrel" | sort) + n=0 + while read -r name url; do + grep -qxF "$name" <<<"$have" && continue + n=$((n + 1)) + [ "$DRY" = 1 ] && continue + curl -sSfL --retry 3 -o "$tmp/asset" "$url" + ghapi -f -X POST -H 'Content-Type: application/octet-stream' --data-binary "@$tmp/asset" \ + "$up/releases/$id/assets?name=$(enc "$name")&label=$(enc "$name")" >/dev/null + done < <(jq -r '.assets[] | "\(.name) \(.browser_download_url)"' <<<"$rel") + [ "$n" = 0 ] || echo "$tag: $n asset(s) $([ "$DRY" = 1 ] && echo "to upload" || echo uploaded)" +done <"$tmp/src.jsonl" + +# Gitea pruned it: the GitHub copy goes too (the tag stays, like the export's) +jq -r '.tag_name' "$tmp/src.jsonl" >"$tmp/keep" +jq -r 'select(.tag_name | startswith("tth-") or test("[+]tth[.]")) | "\(.id) \(.tag_name)"' "$tmp/gh.jsonl" | + while read -r id tag; do + grep -qxF "$tag" "$tmp/keep" && continue + echo "prune $tag" + [ "$DRY" = 1 ] || ghapi -f -X DELETE "$gh/releases/$id" >/dev/null + done diff --git a/.github/workflows/pub-release.yml b/.github/workflows/pub-release.yml index fc1bace04..d417425d5 100644 --- a/.github/workflows/pub-release.yml +++ b/.github/workflows/pub-release.yml @@ -551,3 +551,13 @@ jobs: curl -sSf --config - -X DELETE "$api/tags/$(jq -rn --arg s "$old_tag" '$s|@uri')" <<<"$auth" || true echo "pruned $old_tag" done + + # The same releases on the GitHub fork's Releases page (mirror; Gitea stays + # the source). Converging, so a run that failed here is mended by the next + # release. GH_MIRROR_TOKEN = fine-grained PAT, Contents: write on that repo. + - name: Mirror releases to GitHub + if: env.DRY != 'true' + env: + GH_TOKEN: ${{ secrets.GH_MIRROR_TOKEN }} + GH_REPO: TeTeHacko/MeshCore-TTH + run: bash .github/gh-release-mirror.sh diff --git a/README.md b/README.md index e4f86c893..32722b2d7 100644 --- a/README.md +++ b/README.md @@ -271,7 +271,8 @@ Každý release je automatický build nad upstreamovým `dev` (viz upozornění nahoře), ne stabilní verze. Nahrávejte ho jen na uzly, na které dosáhnete, a nový release vždy nejdřív na jeden z nich. -Soubory jsou v [Releases](https://git.rfa.cz/tth-pub/meshcore-tth/releases). +Soubory jsou v [Releases](https://git.rfa.cz/tth-pub/meshcore-tth/releases); totéž zrcadlí +[GitHub](https://github.com/TeTeHacko/MeshCore-TTH/releases) (`.github/gh-release-mirror.sh`). Release se jmenuje podle upstreamové verze a data, např. `v1.18.0+tth.20261112` (druhý téhož dne `.2`), a v popisu má změny od předchozího releasu. Pro každou desku a roli je jedna sada @@ -642,7 +643,8 @@ Every release is an automatic build on upstream `dev` (see the warning above), not a stable version. Flash it only on nodes you can reach, and each new release on one of them first. -Files are in [Releases](https://git.rfa.cz/tth-pub/meshcore-tth/releases). +Files are in [Releases](https://git.rfa.cz/tth-pub/meshcore-tth/releases), mirrored to +[GitHub](https://github.com/TeTeHacko/MeshCore-TTH/releases) (`.github/gh-release-mirror.sh`). A release is named after the upstream version and the date, e.g. `v1.18.0+tth.20261112` (a second one that day `.2`), and its notes list the changes since the previous release. One set per board and role