diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 29fde15..6264548 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -58,6 +58,13 @@ # This workflow still never writes to a branch. It tags and publishes, so `main` # needs no push exception. # +# ── And then the uom site ──────────────────────────────────────────────────── +# +# A published release is rolled onto the uom site by the `deploy-uom` job at the +# bottom, the module's twin of `website`'s `build-images.yml` deploy. Dispatching +# with `redeploy = true` publishes nothing and rolls the newest release out +# again — for a deploy that was skipped because the site's core was too old. +# # Prerequisites (Settings → Actions → Secrets on RunicGateway/Module-uo): # REGISTRY_TOKEN — Gitea access token with `write:repository`, to push the tag # and create the release. @@ -77,6 +84,10 @@ on: description: 'Bump level when version is blank: patch | minor | major' required: false default: 'patch' + redeploy: + description: 'true = publish nothing; just roll the uom site onto the newest release' + required: false + default: 'false' concurrency: group: release-module-uo @@ -90,6 +101,10 @@ jobs: release: runs-on: ubuntu-latest timeout-minutes: 30 + # Read by `deploy-uom` below: whether there is something to roll out, and which. + outputs: + deploy: ${{ steps.plan.outputs.deploy }} + version: ${{ steps.plan.outputs.version }} steps: # Full history: the plan step reads every tag and every subject since the # newest one, and a shallow clone has neither. @@ -108,6 +123,7 @@ jobs: EVENT: ${{ github.event_name }} IN_VERSION: ${{ github.event.inputs.version }} IN_BUMP: ${{ github.event.inputs.bump }} + IN_REDEPLOY: ${{ github.event.inputs.redeploy }} run: | set -euo pipefail mkdir -p dist @@ -119,6 +135,27 @@ jobs: RANGE="${LAST_TAG:+${LAST_TAG}..}HEAD" echo "module.json declares ${DECLARED}; newest tag is ${LAST_TAG:-}" + # The redeploy button: publish nothing, hand the newest tag to + # `deploy-uom`. For a deploy that was skipped (core too old) or failed, + # once whatever stopped it is fixed — a plain re-run of the workflow + # would find that version already released and deploy nothing. + if [ "${EVENT:-}" = workflow_dispatch ] && [ "${IN_REDEPLOY:-false}" = true ]; then + if [ -z "$CURRENT" ]; then + echo "::error::Redeploy asked for, but there is no release to deploy." + exit 1 + fi + echo "dispatch: redeploying ${LAST_TAG} to the uom site, publishing nothing" + { + echo "version=${CURRENT}" + echo "tag=${LAST_TAG}" + echo "release=false" + echo "reuse_tag=false" + echo "bump=none" + echo "deploy=true" + } >> "$GITHUB_OUTPUT" + exit 0 + fi + SUBJECTS="$(git log --no-merges --format='%s' $RANGE || true)" BODIES="$(git log --no-merges --format='%B' $RANGE || true)" @@ -263,6 +300,7 @@ jobs: echo "release=${RELEASE}" >> "$GITHUB_OUTPUT" echo "reuse_tag=${REUSE_TAG}" >> "$GITHUB_OUTPUT" echo "bump=${BUMP}" >> "$GITHUB_OUTPUT" + echo "deploy=${RELEASE}" >> "$GITHUB_OUTPUT" echo "==> release=${RELEASE} version=${VERSION} bump=${BUMP} declared=${DECLARED} last_tag=${LAST_TAG:-}" # Before anything is built or tagged, so a repo without secrets fails @@ -442,3 +480,211 @@ jobs: -F "attachment=@dist/${f}" >/dev/null echo " uploaded ${f}" done + + # ── Roll the uom site onto the release ──────────────────────────────────── + # + # The website's own `build-images.yml` rolls the uom site onto every new core + # image from this same runner; this does the same for the uo module. It does + # not unpack anything itself: it points the site's MODULES declaration at the + # new release and recreates the app container, and core's own start-up + # (website `server/src/modules/declared.js`) fetches it, verifies the sha256 in + # the manifest and unpacks it — the path every operator's install takes. + # + # 1. Ask the RUNNING core whether it can load this release (the manifest's + # coreApi against its MODULE_API_VERSION). If not, warn and change + # nothing: the version already there keeps running until the site's core + # catches up, and then the Redeploy button (Run workflow, redeploy = true) + # rolls it out. + # 2. Rewrite the `uo@…` entry of the MODULES line in .env, in place (other + # entries and every other line are kept; the file keeps its owner and + # mode). No MODULES line yet, as on a site where uo was installed from + # Admin → Modules, appends one. Only id@version is ever printed — this + # file holds the site's secrets. + # 3. Recreate only the app container (not the db, not the bot) and wait for + # core to log `registered module "uo" v`. + # 4. If it does not, put the entry back to the version that was running and + # recreate again, so a bad release leaves the site where it was, and fail + # the job either way. + # + # `needs: release` with the job's own `deploy` output: only a run that + # published a release (or a redeploy dispatch) gets here, and only after its + # assets are uploaded. + # + # Runner: `uom-deploy-runner`, the one `website`'s deploy job uses, on the + # production host. It needs Docker Compose and write access to the site's + # .env. The site directory defaults to the one `website`'s deploy job uses; + # set the Actions variable UOM_SITE_DIR to move it. + deploy-uom: + needs: release + if: needs.release.outputs.deploy == 'true' && github.ref == 'refs/heads/main' + runs-on: uom-deploy-runner + timeout-minutes: 20 + env: + SITE_DIR: ${{ vars.UOM_SITE_DIR || '/home/perry/website' }} + VERSION: ${{ needs.release.outputs.version }} + steps: + - name: Check the running core, declare the release, restart the app + shell: bash + run: | + set -euo pipefail + cd "$SITE_DIR" + + manifest() { echo "https://${GITEA_HOST}/${REPO}/releases/download/v$1/module-uo-$1.json"; } + NEW="uo@${VERSION}=$(manifest "$VERSION")" + + test -f .env || { echo "::error::${SITE_DIR}/.env is missing."; exit 1; } + test -w .env || { echo "::error::The runner cannot write ${SITE_DIR}/.env."; exit 1; } + COUNT="$(grep -cE '^[[:space:]]*MODULES[[:space:]]*=' .env || true)" + if [ "${COUNT:-0}" -gt 1 ]; then + echo "::error::.env sets MODULES ${COUNT} times. Leave one line, then press Redeploy." + exit 1 + fi + + # ── 1. Can the running core load it? ───────────────────────────── + # Asked inside the app container: it has core's own version and range + # check, and the network path core itself will use to fetch the bundle. + # Prints one line: `ok|skip `. + if ! CHECK="$(docker compose exec -T app node -e ' + const [url, want] = process.argv.slice(1) + const fs = require("fs") + const path = require("path") + const { satisfies } = require("/app/server/src/modules/semver") + const { MODULE_API_VERSION: core } = require("/app/server/src/modules/version") + let prev = "none" + try { + const dir = process.env.MODULES_DIR || "/app/modules" + prev = JSON.parse(fs.readFileSync(path.join(dir, "uo", "module.json"), "utf8")).version || "none" + } catch {} + fetch(url).then(async (r) => { + if (!r.ok) throw new Error(`the manifest answered HTTP ${r.status}`) + const m = await r.json() + if (m.id !== "uo" || m.version !== want) throw new Error(`the manifest is ${m.id}@${m.version}, not uo@${want}`) + console.log(`${satisfies(core, m.coreApi) ? "ok" : "skip"} ${core} ${prev} ${m.coreApi}`) + }).catch((e) => { console.log(`error ${e.message}`); process.exit(1) }) + ' "$(manifest "$VERSION")" "$VERSION")"; then + echo "::error::Could not check the running site: ${CHECK:-the app container did not answer (is it running?)}" + exit 1 + fi + read -r VERDICT CORE PREV RANGE <<< "$CHECK" + echo "uom site: core API ${CORE}, uo ${PREV} on the volume; release v${VERSION} needs ${RANGE}" + + if [ "$VERDICT" = skip ]; then + echo "::warning::uo v${VERSION} needs core API ${RANGE} and the uom site runs ${CORE}. Nothing changed; uo ${PREV} keeps running. Once the site's core is updated, run Release with redeploy = true." + exit 0 + fi + if [ "$PREV" = "$VERSION" ] && grep -qF "$NEW" .env; then + echo "uo v${VERSION} is already declared and on the volume. Nothing to do." + exit 0 + fi + + # MODULES set under `environment:` in docker-compose.yml wins over .env, + # so editing .env would change nothing. Catch it before touching anything. + ENV_MODULES="$(docker compose exec -T app printenv MODULES 2>/dev/null || true)" + if [ -n "$ENV_MODULES" ] && [ "${COUNT:-0}" -eq 0 ]; then + echo "::error::The app has MODULES set but .env has no MODULES line, so it comes from docker-compose.yml's environment:, which wins over .env. Move it into .env, then press Redeploy." + exit 1 + fi + + # Core logs `registered module` at info. With a quieter LOG_LEVEL that + # line never reaches `docker compose logs`, so success is judged by the + # volume instead (failures are logged at error and still show). + LOG_LEVEL_NOW="$(docker compose exec -T app printenv LOG_LEVEL 2>/dev/null || true)" + case "$(echo "${LOG_LEVEL_NOW:-info}" | tr '[:upper:]' '[:lower:]')" in + warn|error) QUIET=true; echo "The app runs with LOG_LEVEL=${LOG_LEVEL_NOW}; checking the volume rather than the info log." ;; + *) QUIET=false ;; + esac + + # ── 2. Point MODULES at it ─────────────────────────────────────── + ORIG="$(mktemp)"; TMP="$(mktemp)" + trap 'rm -f "$ORIG" "$TMP"' EXIT + cat .env > "$ORIG" + + # Rewrites the uo entry of the original file's MODULES line (or appends + # the line), then writes the result over .env in place with `cat >`, so + # the file keeps its inode, owner and mode. If that write does not land + # intact (a full disk), the original goes back before the job fails. + # Plain `[ \t]`, not `[[:space:]]`: older mawk, Ubuntu's default awk, + # has no character classes in regex literals. + set_entry() { + awk -v entry="$1" -v sq="'" ' + /^[ \t]*MODULES[ \t]*=/ { + v = $0 + sub(/^[ \t]*MODULES[ \t]*=[ \t]*/, "", v) + sub(/[ \t]+#.*$/, "", v) + gsub(/"/, "", v); gsub(sq, "", v) + n = split(v, parts, /[, \t]+/) + out = "" + for (i = 1; i <= n; i++) if (parts[i] != "" && parts[i] !~ /^uo@/) out = out parts[i] "," + print "MODULES=" out entry + found = 1 + next + } + { print } + END { if (!found) print "MODULES=" entry } + ' "$ORIG" > "$TMP" + test -s "$TMP" || { echo "::error::Rewriting .env produced an empty file; .env left as it was."; exit 1; } + if ! { cat "$TMP" > .env && cmp -s "$TMP" .env; }; then + cat "$ORIG" > .env || true + echo "::error::Writing .env failed; the original was put back. Nothing was restarted by this step." + exit 1 + fi + } + # id@version of each declared entry — never the URLs, never the rest of the file. + declared() { + local d + d="$(grep -E '^[[:space:]]*MODULES[[:space:]]*=' "$1" \ + | grep -oE '[A-Za-z0-9_.-]+@[^=[:space:],]+=' | tr -d '=' | paste -sd' ' - || true)" + echo "${d:-(none)}" + } + + # ── 3. Recreate the app and wait for core to say so ───────────── + # Only uo's own lines and MODULES errors are ever printed from the app log. + uo_lines() { grep -E 'module "uo"|declared module "uo|MODULES' <<< "$1" | tail -n 20 || true; } + restart_and_wait() { + local want="$1" since logs on_volume settled=0 + since="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + docker compose up -d --no-deps --force-recreate app + for _ in $(seq 1 60); do + sleep 5 + logs="$(docker compose logs --no-color --since "$since" app 2>&1 || true)" + if grep -qE 'could not resolve declared module "uo@|module "uo" (failed to|was not started)|MODULES: ' <<< "$logs"; then + uo_lines "$logs" + return 1 + fi + # Anchored after the version, so v1.2.1 is not satisfied by v1.2.10. + if grep -qE "registered module \"uo\" v${want//./\\.}([^0-9A-Za-z.+-]|\$)" <<< "$logs"; then return 0; fi + if [ "$QUIET" = true ]; then + on_volume="$(docker compose exec -T app node -p 'require((process.env.MODULES_DIR || "/app/modules") + "/uo/module.json").version' 2>/dev/null || true)" + # The load fails, if it fails, right after the unpack. Give it + # three quiet polls on the new version before calling it good. + if [ "$on_volume" = "$want" ]; then settled=$((settled + 1)); else settled=0; fi + if [ "$settled" -ge 3 ]; then return 0; fi + fi + done + echo "No word from the app about uo v${want} after 5 minutes. Its uo lines:" + uo_lines "$logs" + return 1 + } + + echo "MODULES before: $(declared "$ORIG")" + set_entry "$NEW" + echo "MODULES after: $(declared .env)" + if restart_and_wait "$VERSION"; then + echo "uo v${VERSION} is registered on the uom site." + exit 0 + fi + + # ── 4. It did not come up: put back what was running ───────────── + if [ "$PREV" = none ]; then + echo "::error::uo v${VERSION} did not come up, and there was no earlier uo to roll back to." + exit 1 + fi + echo "Rolling the uom site back to uo v${PREV}." + set_entry "uo@${PREV}=$(manifest "$PREV")" + echo "MODULES now: $(declared .env)" + if restart_and_wait "$PREV"; then + echo "::error::uo v${VERSION} did not come up on the uom site. Rolled back to v${PREV}, which is running." + else + echo "::error::uo v${VERSION} did not come up, and neither did the rollback to v${PREV}. The site is up without uo; see Admin → Modules." + fi + exit 1 diff --git a/README.md b/README.md index 6dded11..4069890 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,17 @@ For a change with nothing releasable behind it — a widened `coreApi`, a new mo run the **Release** workflow by hand (Actions → Release → Run workflow). Leave `version` blank to bump the newest tag by `bump` (default `patch`), or type an exact version to publish that. +**Every release then rolls onto the uom site** — the `deploy-uom` job, the module's twin of the +website's own deploy, on the same `uom-deploy-runner`. It asks the running core whether it can load +the release (`coreApi` against `MODULE_API_VERSION`); if it cannot, it warns and changes nothing. +Otherwise it rewrites the `uo@…` entry of the `MODULES` line in the site's `.env` (appending the line +if there is none; other entries are kept), recreates only the app container, and waits for core to +log `registered module "uo" v`. If that does not come, it declares the version that was +running before, recreates again, and fails. **Run workflow** with `redeploy = true` publishes nothing +and rolls the newest release out again — for a deploy that was skipped until the site's core caught +up. The site directory defaults to `/home/perry/website`; the Actions variable `UOM_SITE_DIR` +overrides it. + Each release carries: | Asset | What it is |