name: build-agent # Builds the TacticalRMM agent (github.com/amidaware/rmmagent) and publishes the # binaries into this public repo under dist//. The TRMM update shim 302s # agents to the raw file URLs; agents fetch them anonymously. # # Every trigger runs the same idempotent "scan & publish" job: # - hourly cron : picks up a new upstream release automatically # - workflow_dispatch : manual run from the Gitea UI # - push to 'publish' : on-demand check (git push origin main:publish) # # 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: branches: [publish] schedule: - cron: '17 * * * *' workflow_dispatch: jobs: build: runs-on: agentbuilds permissions: contents: write steps: - name: Scan, build, publish shell: sh run: | set -eu API="https://git.izebra.net" REPO="izebra_projects/agent-builds" UP="https://github.com/amidaware/rmmagent" # 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 # POSIX-safe (dash): first arg = timeout seconds, rest = git args authgit() { at="$1"; shift timeout "$at" env GIT_TERMINAL_PROMPT=0 GIT_CONFIG_COUNT=1 \ GIT_CONFIG_KEY_0=http.extraHeader GIT_CONFIG_VALUE_0="Authorization: token ***" \ git "$@" } 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 (3 attempts x 120s cap: github hiccups) --- VER="" for i in 1 2 3; do VER="$(GIT_TERMINAL_PROMPT=0 timeout 180 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 exit 0 fi echo "newest upstream version: $VER" # --- idempotency: already published here? (2 attempts: gitea stalls observed) --- PUBLISHED="" for i in 1 2; do PUBLISHED="$(authgit 120 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 120 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_TERMINAL_PROMPT=0 timeout 300 git clone --quiet --depth 1 --branch "$VER" "$UP" "$W/src" ( cd "$W/src" b() { t="$1"; GOOS="${t%%/*}"; GOARCH="${t##*/}" EXE=""; [ "$GOOS" = "windows" ] && EXE=".exe" echo "=== go build $GOOS/$GOARCH ===" CGO_ENABLED=0 GOOS="$GOOS" GOARCH="$GOARCH" \ timeout 900 go build -ldflags "-s -w" \ -o "$W/dist/tacticalagent-$VER-$GOOS-$GOARCH$EXE" . || { echo "BUILD FAILED $GOOS/$GOARCH" >&2; exit 1; } } # 4 waves of 2 (proven pattern; much faster than serial on the runner) ( b linux/amd64; b linux/arm64 ) & ( b linux/arm; b linux/386 ) & wait ( b darwin/amd64; b darwin/arm64 ) & ( b windows/amd64; b windows/386 ) & wait ) # hard guard: all 8 targets must exist before publish (POSIX sh `wait` # does not propagate child failures, so verify the artifacts) n="$(ls "$W/dist" | grep -c '^tacticalagent-' || true)" if [ "$n" != "8" ]; then echo "expected 8 built binaries, found $n" >&2 exit 1 fi ( cd "$W/dist" && sha256sum tacticalagent-* > SHA256SUMS ) # --- publish into this repo (public; agents fetch the raw URLs) --- 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" mkdir -p "dist/$VER" cp "$W/dist"/tacticalagent-* "dist/$VER"/ cp "$W/dist/SHA256SUMS" "dist/$VER/" # read old index before the redirect below truncates it if [ -f dist/INDEX ]; then OLDIDX="$(grep -v '^#' dist/INDEX || true)"; else OLDIDX=""; fi { echo "# agent-builds: published versions (the per-version branch is the git ref used in raw URLs)" if [ -n "$OLDIDX" ]; then printf '%s\n' "$OLDIDX"; fi 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" # 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 (per-version branch) --- # busybox curl has no -w: use -f (exit 22 on HTTP >=400) as the pass/fail signal for f in "$W/dist"/tacticalagent-*; do n="$(basename "$f")" if curl -sf -m 60 -o /dev/null "$API/$REPO/raw/branch/$VER/dist/$VER/$n"; then echo "raw 200: $n" else rc=$? echo "RAW FETCH FAILED: $n (curl exit $rc: 22=HTTP error, 28=timeout, 6=DNS)" exit 1 fi done echo "published" > /tmp/agentbuilds-status echo "$VER" > /tmp/agentbuilds-published echo "PUBLISHED $VER: $(ls "$W/dist" | grep -c '^tacticalagent-') binaries + SHA256SUMS; raw URLs verified 200" - name: Announce shell: sh run: | if [ -f /tmp/agentbuilds-published ]; then VER="$(cat /tmp/agentbuilds-published)" curl -s -m 5 "http://10.2.1.10/izebra" \ -H "X-Title: agent-builds: $VER published" \ -H 'X-Priority: 3' -H 'X-Tag: rocket' \ --data "Published TacticalRMM agent $VER (8 targets) to git.izebra.net/izebra_projects/agent-builds. RMM2's LATEST_AGENT_VER gate (tacticalrmm settings) controls rollout; bump it to $VER when ready." || true fi - name: Alert shell: sh run: | st="$(cat /tmp/agentbuilds-status 2>/dev/null || echo failed)" if [ "$st" = "failed" ]; then curl -s -m 5 "http://10.2.1.10/izebra" \ -H 'X-Title: agent-builds pipeline FAILED' \ -H 'X-Priority: 4' -H 'X-Tag: red_circle' \ --data "Build/publish job failed on git.izebra.net/izebra_projects/agent-builds - check the run log (runner: agentbuilds on DKR4)." || true fi