Modelled on dynamic-qr: Traefik Gateway with Let's Encrypt, Longhorn PVC, 1 replica with Recreate, Calico network policies, and a rate limit on POST /api/leads. CI runs the tests in a Dockerfile stage before pushing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
83 lines
3.8 KiB
Bash
Executable File
83 lines
3.8 KiB
Bash
Executable File
#!/bin/bash
|
|
# Usage: waitForOlderRuns.sh <own_run_number>
|
|
# Queues deploy runs per branch (openspec ci-pipeline): returns 0 once no OLDER push run of this
|
|
# branch is pending, queued or in progress, so runs build and roll out one at a time, in push order.
|
|
# Gitea's workflow `concurrency` cannot do this: it keeps one waiting run per group and cancels the
|
|
# others, even with cancel-in-progress: false.
|
|
#
|
|
# It also refuses (exit 1) when a NEWER push run of the branch has already succeeded: that is a manual
|
|
# re-run of an old run, which would roll an older image out over a newer one. Push again instead; the
|
|
# self-heal (resolveDeployBase.sh) rebuilds whatever the old run missed.
|
|
#
|
|
# Fails closed: a failed or unreadable API answer counts as "still waiting", and a run still waiting
|
|
# after WAIT_TIMEOUT seconds exits 1 rather than build alongside an older run. A refused token (401/403)
|
|
# exits 1 at once — waiting would only end in the timeout.
|
|
#
|
|
# ponytail: reads the newest 50 runs of each kind; an older unfinished run beyond them is not seen. A run
|
|
# being cancelled (Gitea "cancelling") is reported in_progress by the API but is not matched by the
|
|
# status=in_progress filter; both gaps are seconds-to-minutes wide here — page the API if that changes.
|
|
#
|
|
# Env: GITEA_TOKEN, GITEA_API (…/api/v1), GITEA_REPO (owner/name), GITEA_BRANCH — for the default fetch.
|
|
# WAIT_INTERVAL (s, default 30), WAIT_TIMEOUT (s, default 7200), WAIT_FETCH (a command printing the
|
|
# runs JSON; tests replace the API with it).
|
|
set -u
|
|
own="${1:-}"
|
|
if ! [[ "$own" =~ ^[0-9]+$ ]]; then
|
|
echo "waitForOlderRuns: own run number must be a number, got '$own'" >&2
|
|
exit 2
|
|
fi
|
|
interval="${WAIT_INTERVAL:-30}"
|
|
deadline=$(( $(date +%s) + ${WAIT_TIMEOUT:-7200} ))
|
|
|
|
fetch() { # $1 = unfinished | succeeded. Prints the runs JSON; exit 3 = token refused, other non-zero = failed.
|
|
if [ -n "${WAIT_FETCH:-}" ]; then
|
|
"$WAIT_FETCH" "$1"
|
|
return
|
|
fi
|
|
local status=status=success code body
|
|
[ "$1" = unfinished ] && status="status=pending&status=queued&status=in_progress"
|
|
body="$(mktemp)"
|
|
code="$(curl -sS --max-time 20 -o "$body" -w '%{http_code}' -H "Authorization: token $GITEA_TOKEN" \
|
|
"$GITEA_API/repos/$GITEA_REPO/actions/runs?branch=$GITEA_BRANCH&event=push&$status&limit=50")" || code=000
|
|
case "$code" in
|
|
200) cat "$body"; rm -f "$body"; return 0 ;;
|
|
401|403) rm -f "$body"; return 3 ;;
|
|
*) rm -f "$body"; return 1 ;;
|
|
esac
|
|
}
|
|
|
|
run_numbers() { # runs JSON on stdin -> its run numbers, one per line. Nested objects carry no run_number.
|
|
grep -o '"run_number":[0-9]\+' | cut -d: -f2
|
|
}
|
|
|
|
while :; do
|
|
json="$(fetch unfinished)"; rc=$?
|
|
if [ "$rc" -eq 3 ]; then
|
|
echo "Runs API refused the job token (401/403); cannot queue run #$own safely." >&2
|
|
exit 1
|
|
fi
|
|
if [ "$rc" -eq 0 ] && [[ "$json" == *'"workflow_runs"'* ]]; then
|
|
older="$(printf '%s' "$json" | run_numbers | awk -v own="$own" '$1 < own' | sort -n | tr '\n' ' ')"
|
|
if [ -n "$older" ]; then
|
|
echo "Run #$own waits for older run(s): ${older% }"
|
|
elif done_json="$(fetch succeeded)" && [[ "$done_json" == *'"workflow_runs"'* ]]; then
|
|
newer="$(printf '%s' "$done_json" | run_numbers | awk -v own="$own" '$1 > own' | sort -n | tr '\n' ' ')"
|
|
if [ -n "$newer" ]; then
|
|
echo "Run #$own is older than run(s) ${newer% }, which already deployed; refusing to roll back. Push again instead." >&2
|
|
exit 1
|
|
fi
|
|
echo "No older run of this branch is unfinished; starting run #$own."
|
|
exit 0
|
|
else
|
|
echo "Runs API read failed; run #$own keeps waiting."
|
|
fi
|
|
else
|
|
echo "Runs API read failed; run #$own keeps waiting."
|
|
fi
|
|
if [ "$(date +%s)" -ge "$deadline" ]; then
|
|
echo "Run #$own still waiting after ${WAIT_TIMEOUT:-7200}s; failing rather than building alongside an older run." >&2
|
|
exit 1
|
|
fi
|
|
sleep "$interval"
|
|
done
|