harden pipeline: hard timeouts+retries on all network git/curl ops; fail-loud on inconclusive tag check; push frozen per-version branch (raw URL ref); keep tag as idempotency marker

This commit is contained in:
zeb
2026-11-14 22:31:52 -08:00
parent f116aa82b1
commit 6350af3249

View File

@@ -11,6 +11,12 @@ name: build-agent
# A version already tagged in this repo is never rebuilt (idempotent).
# Only the newest upstream semver tag is ever auto-published; the TRMM server
# always tells agents to fetch the newest version, so older ones are not served.
#
# Hardened: every network git/curl op has a hard `timeout` + retries, and
# GIT_TERMINAL_PROMPT=0 everywhere, so a stalled connection (observed: a
# ~13-min server-side stall in the Caddy/gitea path) fails fast with a visible
# error + ntfy alert instead of hanging forever. Failures self-heal on the
# next cron tick or an on-demand publish push.
on:
push:
@@ -32,34 +38,64 @@ jobs:
API="https://git.izebra.net"
REPO="izebra_projects/agent-builds"
UP="https://github.com/amidaware/rmmagent"
TOK="$(cat /data/releases-token)"
# token via git's env config, not argv (house pattern)
authgit() { GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraHeader \
GIT_CONFIG_VALUE_0="Authorization: token ***" git "$@"; }
# prefer the per-run gitea token (house pattern); fall back to the /data file PAT
TOK="${{ gitea.token }}"
[ -n "$TOK" ] || TOK="$(cat /data/releases-token 2>/dev/null || true)"
if [ -z "$TOK" ]; then
echo "no gitea token available (gitea.token empty and /data/releases-token missing)"
echo "failed" > /tmp/agentbuilds-status
exit 1
fi
# token via git's env config, not argv (house pattern);
# GIT_TERMINAL_PROMPT=0 => a 401 fails fast instead of hanging on a prompt;
# `timeout` => a stalled connection fails fast instead of hanging forever
authgit() { timeout "$1" env GIT_TERMINAL_PROMPT=0 GIT_CONFIG_COUNT=1 \
GIT_CONFIG_KEY_0=http.extraHeader GIT_CONFIG_VALUE_0="Authorization: token ***" \
git "${@:2}"; }
W="$(mktemp -d /tmp/agentbuilds.XXXXXX)"
trap 'st=$(cat /tmp/agentbuilds-status 2>/dev/null); [ -n "$st" ] || echo failed > /tmp/agentbuilds-status; rm -rf "$W"' EXIT
rm -f /tmp/agentbuilds-status /tmp/agentbuilds-published
# --- newest upstream semver tag ---
VER="$(git ls-remote --tags "$UP" | awk '{print $2}' \
| grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | sort -uV | tail -1 || true)"
# --- newest upstream semver tag (3 attempts x 120s cap: github hiccups) ---
VER=""
for i in 1 2 3; do
VER="$(GIT_TERMINAL_PROMPT=0 timeout 120 git ls-remote --tags "$UP" 2>/dev/null \
| awk '{print $2}' | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | sort -uV | tail -1 || true)"
[ -n "$VER" ] && break
echo "upstream tag scan attempt $i failed; retrying in 60s"
sleep 60
done
if [ -z "$VER" ]; then
echo "no upstream version found after 3 attempts; nothing to do"
echo "noop" > /tmp/agentbuilds-status
echo "no upstream version found; nothing to do"
exit 0
fi
echo "newest upstream version: $VER"
# --- idempotency: already published here? ---
if authgit ls-remote --tags "$API/$REPO.git" | grep -qE "refs/tags/${VER}\$"; then
# --- idempotency: already published here? (2 attempts: gitea stalls observed) ---
PUBLISHED=""
for i in 1 2; do
PUBLISHED="$(authgit 90 ls-remote --tags "$API/$REPO.git" 2>/dev/null \
| grep -E "refs/tags/${VER}\$" || true)"
[ -n "$PUBLISHED" ] && break
echo "gitea tag check attempt $i inconclusive; retrying in 45s"
sleep 45
done
if [ -n "$PUBLISHED" ]; then
echo "noop" > /tmp/agentbuilds-status
echo "$VER already published (tag present) - nothing to do"
exit 0
fi
# a second inconclusive gitea check is NOT proof of absence: fail loudly
if ! authgit 90 ls-remote --tags "$API/$REPO.git" >/dev/null 2>&1; then
echo "could not verify existing tags after retries; refusing to rebuild (gitea unreachable/stalled)"
echo "failed" > /tmp/agentbuilds-status
exit 1
fi
# --- build every target (CGO off: pure cross-compile, no qemu) ---
mkdir -p "$W/dist"
git clone --quiet --depth 1 --branch "$VER" "$UP" "$W/src"
GIT_TERMINAL_PROMPT=0 timeout 300 git clone --quiet --depth 1 --branch "$VER" "$UP" "$W/src"
(
cd "$W/src"
for t in linux/amd64 linux/arm64 linux/arm linux/386 \
@@ -68,14 +104,14 @@ jobs:
EXE=""; [ "$GOOS" = "windows" ] && EXE=".exe"
echo "=== go build $GOOS/$GOARCH ==="
CGO_ENABLED=0 GOOS="$GOOS" GOARCH="$GOARCH" \
go build -ldflags "-s -w" \
timeout 900 go build -ldflags "-s -w" \
-o "$W/dist/tacticalagent-$VER-$GOOS-$GOARCH$EXE" .
done
)
( cd "$W/dist" && sha256sum tacticalagent-* > SHA256SUMS )
# --- publish into this repo (public; agents fetch the raw URLs) ---
authgit clone --quiet "$API/$REPO.git" "$W/repo"
authgit 180 clone --quiet "$API/$REPO.git" "$W/repo"
cd "$W/repo"
git config user.name "agent-builds"
git config user.email "agent-builds@izebra.net"
@@ -83,20 +119,23 @@ jobs:
cp "$W/dist"/tacticalagent-* "dist/$VER"/
cp "$W/dist/SHA256SUMS" "dist/$VER/"
{
echo "# agent-builds: published versions (the tag is the git ref used in raw URLs)"
echo "# agent-builds: published versions (the per-version branch is the git ref used in raw URLs)"
[ -f dist/INDEX ] && grep -v '^#' dist/INDEX
printf '%s %s\n' "$VER" "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
} > dist/INDEX
git add -A
git commit -m "publish $VER (built from upstream amidaware/rmmagent at that tag)"
git tag "$VER"
authgit push origin main
authgit push origin "$VER"
# main first so the commit exists remotely, then the frozen per-version
# branch (the immutable ref the shim 302s to) and the tag (idempotency marker)
authgit 180 push origin main
authgit 180 push origin "main:refs/heads/$VER"
authgit 180 push origin "$VER"
# --- verify exactly what agents will do: anonymous raw fetch ---
# --- verify exactly what agents will do: anonymous raw fetch (per-version branch) ---
for f in "$W/dist"/tacticalagent-*; do
n="$(basename "$f")"
code="$(curl -s -o /dev/null -w '%{http_code}' "$API/$REPO/raw/branch/$VER/dist/$VER/$n")"
code="$(curl -s -m 30 -o /dev/null -w '%{http_code}' "$API/$REPO/raw/branch/$VER/dist/$VER/$n")"
if [ "$code" != "200" ]; then
echo "RAW FETCH FAILED: $n -> HTTP $code"
exit 1