Files
archie/scripts/archie-update-check
T
sam 0b408017e0 feat(release): one release stream, one version, one page per version
CHANGELOG.archied.md and CHANGELOG.archie.md were two version streams that
drifted: a release that moved both components had two numbers, two pages and
two tags, and a reader had to visit two files to learn what shipped.

The merged history holds 65 versions from 64 archied and 34 archie sections --
33 that both components released, 31 only archied, one only archie -- so 98
published pages become 65. A version's date is the newest of its sections'
dates, and a component whose own date differs carries it on its label, because
the history has two dates and the merge must not invent one. `## [Unreleased]`
is not a release: it gets no page, and it stays at the top of the file.

A level-two heading is where the merge could silently drop a section, so the
merge is verified by content rather than by the generator: section counts per
source, the merged version set against the exact union, and every source `###`
title present both as a substring and, after stripping the component prefix, as
an identical heading. Counting `##` headings rather than releases overcounts by
one here, which is how the empty `[Unreleased]` block was caught.

- newsgen reads one file and writes `docs/news/<version>.md`, the index, the
  canonical `releases.json`, and `docs/news/redirects.json`. The redirect table
  is what keeps the retired `/news/<component>/<version>/` URLs alive; it is
  generated beside the pages so it cannot drift from them.
- release.sh takes one `--version`, keeps commit and tag (the Woodpecker
  pipeline that will own them does not exist yet), and exits 3 when the commit
  set justifies no release, so a pipeline can finish green without tagging.
- The consumers of the two files follow: the deploy workflow's tag gate and
  release-notes extraction, the zip contents, the update check's changelog
  links, the packaged-path note, the docs skill, and newsgen's tests.

The per-component detail survives as labelled sections inside each release, so
a reader still learns whether archied or the agent changed; what goes away is
the separate numbers and the separate pages.
2026-09-22 17:18:06 +10:00

188 lines
8.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# archie-update-check -- update discovery adapter for archied's /update command.
#
# Writes a releaseupdate.Snapshot JSON document to stdout. archied runs this
# via CommandCatalog (internal/releaseupdate/command.go), which never invokes
# a shell, so this file must be directly executable.
#
# "Installed" for the daemon is the release the running binary was ACTUALLY
# built from, read via `archied -version`. The agent value in that output is
# only the runtime version the daemon release was cut with; it is not the
# managed image that an installer later rebuilt. The installer therefore owns
# a sidecar beside BIN_DIR, archie-agent.version, and that is authoritative.
# Older installations without the sidecar use the agent line as a compatibility
# fallback. An existing malformed sidecar is reported as unknown, not replaced
# by a fabricated version. Build-stamp git-describe suffixes are stripped for
# comparison (1.7.0-4-g0736708 -> 1.7.0).
#
# "Available" is the newest release TAG, resolved without any dependency on a
# local checkout. Two independent sources, in order:
#
# 1. the forge API (tags + releases), which also reports whether the tag has
# a PUBLISHED artifact -- the thing an artifact install path downloads;
# 2. `git ls-remote` against the source remote, which needs no API and no
# working tree.
#
# The tag is the authority for "an update exists"; the artifact is reported
# separately in Reference, so a tagged-but-unpublished release is still
# announced (it is installable via the source-build fallback) instead of being
# silently hidden.
#
# A tag source that fails to resolve is a FAILURE TO CHECK, not an absence.
# An empty Available means "this component is at the newest release we could
# resolve", so it must never be the result of a probe that failed. When no
# authoritative source answers at all, this script exits non-zero and the
# /update command says it could not check -- rather than rendering a failed
# lookup as "Archie is up to date".
set -uo pipefail
readonly REPO_SLUG="${ARCHIE_SOURCE_SLUG:-samcharles93/archie-core}"
readonly API_BASE="${ARCHIE_FORGE_API:-https://api.github.com}"
readonly SOURCE_URL="${ARCHIE_SOURCE_URL:-https://github.com/${REPO_SLUG}.git}"
readonly BIN="${ARCHIE_BIN:-$HOME/.local/bin/archied}"
readonly BIN_DIR="${ARCHIE_BIN_DIR:-$(dirname "$BIN")}"
readonly AGENT_VERSION_FILE="${ARCHIE_AGENT_VERSION_FILE:-$BIN_DIR/archie-agent.version}"
readonly CURL_TIMEOUT="${ARCHIE_FORGE_TIMEOUT:-20}"
readonly TOKEN="${ARCHIE_FORGE_TOKEN:-}"
# base 1.7.0-4-g0736708 -> 1.7.0
base() { printf '%s\n' "${1%%-*}"; }
# installed_for reads the version this binary reports for a named component.
# archied stamps both its own version and the runtime version the release was
# cut with, and -version prints one line per component -- so the installed
# runtime is what THIS RELEASE says it was built against, not a stray binary
# on the host. releaseupdate/service.go documents why a host-side archie-agent
# binary cannot answer this: every archie-agent process is task-scoped and
# ephemeral, observed only through a completed taskrun.Response.
installed_for() {
"$BIN" -version 2>/dev/null | awk -v n="$1" '$1 == n { print $2; exit }'
}
curl_api() {
if [ -n "$TOKEN" ]; then
curl -fsS --max-time "$CURL_TIMEOUT" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.github+json" \
"$1" 2>/dev/null
else
curl -fsS --max-time "$CURL_TIMEOUT" \
-H "Accept: application/vnd.github+json" \
"$1" 2>/dev/null
fi
}
# newest_tag_git resolves the newest tag for a component prefix from the git
# remote. No local checkout, no working-tree state, no fetch -- `ls-remote`
# queries the remote directly, so this works on a host that has never cloned
# anything.
newest_tag_git() {
local component="$1" tags
command -v git >/dev/null 2>&1 || return 1
tags="$(git ls-remote --tags --refs "$SOURCE_URL" "$component/v*" 2>/dev/null)" || return 1
[ -n "$tags" ] || return 1
printf '%s\n' "$tags" | sed 's|.*refs/tags/||' | sed "s|^$component/v||" | sort -V | tail -1
}
# newest_tag_api is the API equivalent, used when git is unavailable.
newest_tag_api() {
local component="$1" page payload found total
for page in 1 2 3; do
payload="$(curl_api "${API_BASE}/repos/${REPO_SLUG}/tags?per_page=100&page=${page}")" || return 1
[ -n "$payload" ] || return 1
found="$(printf '%s\n' "$payload" \
| grep -o "\"name\":[[:space:]]*\"${component}/v[^\"]*\"" \
| sed 's|.*"'"${component}"'/v||; s|"$||' | sort -V | tail -1)"
if [ -n "$found" ]; then
printf '%s\n' "$found"
return 0
fi
total="$(printf '%s\n' "$payload" | grep -c '"name"')"
[ "${total:-0}" -lt 100 ] && break
done
return 1
}
# artifact_state reports whether a tagged release is PUBLISHED with assets,
# which decides whether an artifact install can use it or the source-build
# fallback has to. Purely descriptive: a failure here never fabricates a
# state, it reports "unknown-artifact".
artifact_state() {
local tag="$1" payload count
command -v jq >/dev/null 2>&1 || { printf 'unknown-artifact'; return 0; }
payload="$(curl_api "${API_BASE}/repos/${REPO_SLUG}/releases/tags/${tag}")" || { printf 'no-release'; return 0; }
[ -n "$payload" ] || { printf 'no-release'; return 0; }
count="$(printf '%s\n' "$payload" | jq -r '[.assets[] | select(.name != "")] | length' 2>/dev/null)"
case "${count:-}" in
'' | *[!0-9]*) printf 'unknown-artifact' ;;
0) printf 'no-artifact' ;;
*) printf '%s-assets' "$count" ;;
esac
}
esc() { printf '%s' "${1:-}" | sed 's/\\/\\\\/g; s/"/\\"/g'; }
daemon_installed="$(base "$(installed_for archied || true)")"
agent_installed=""
if [ -f "$AGENT_VERSION_FILE" ]; then
# A sidecar is updater state: accept only one released semantic version and
# deliberately do not fall back if it is empty or malformed.
sidecar_version="$(awk 'NF { if (seen++) { bad=1; next } gsub(/^[[:space:]]+|[[:space:]]+$/, "", $0); print } END { if (bad) exit 1 }' "$AGENT_VERSION_FILE" 2>/dev/null || true)"
if [[ "$sidecar_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$ ]]; then
agent_installed="$(base "$sidecar_version")"
fi
else
# Compatibility for hosts installed before the sidecar existed.
agent_installed="$(base "$(installed_for archie-agent || true)")"
fi
daemon_available="$(newest_tag_git archied || true)"
agent_available="$(newest_tag_git archie || true)"
tag_source="git-tags"
if [ -z "$daemon_available" ] && [ -z "$agent_available" ]; then
daemon_available="$(newest_tag_api archied || true)"
agent_available="$(newest_tag_api archie || true)"
tag_source="forge-api-tags"
fi
if [ -z "$daemon_available" ] && [ -z "$agent_available" ]; then
# Neither source answered. Report the failure; do not exit 0 with two empty
# components, which the caller renders as "up to date".
echo "archie-update-check: could not resolve any release tag from ${SOURCE_URL} or ${API_BASE}/repos/${REPO_SLUG}/tags" >&2
exit 1
fi
daemon_artifacts=""
[ -n "$daemon_available" ] && daemon_artifacts="$(artifact_state "archied/v${daemon_available}")"
agent_artifacts=""
[ -n "$agent_available" ] && agent_artifacts="$(artifact_state "archie/v${agent_available}")"
reference_for() {
local component="$1" version="$2" artifacts="$3"
[ -z "$version" ] && { printf ''; return 0; }
printf '%s artifact=%s (%s)' "$tag_source" "$artifacts" "$component"
}
cat <<EOF
{
"Components": [
{
"ID": "daemon",
"Label": "Gateway",
"Installed": "$(esc "$daemon_installed")",
"Available": "$(esc "$daemon_available")",
"Reference": "$(esc "$(reference_for archie-core "$daemon_available" "$daemon_artifacts")")",
"Changelog": "https://github.com/${REPO_SLUG}/blob/main/CHANGELOG.md"
},
{
"ID": "agent",
"Label": "Runtime",
"Installed": "$(esc "$agent_installed")",
"Available": "$(esc "$agent_available")",
"Reference": "$(esc "$(reference_for archie-runtime "$agent_available" "$agent_artifacts")")",
"Changelog": "https://github.com/${REPO_SLUG}/blob/main/CHANGELOG.md"
}
],
"Deferred": false
}
EOF