178 lines
8.2 KiB
YAML
178 lines
8.2 KiB
YAML
name: build-agent
|
|
# Builds the TacticalRMM agent (github.com/amidaware/rmmagent) and publishes the
|
|
# binaries into this public repo under dist/<tag>/. 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 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
|
|
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 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_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 \
|
|
darwin/amd64 darwin/arm64 windows/amd64 windows/386; do
|
|
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" .
|
|
done
|
|
)
|
|
( 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 30 -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 LATEST_AGENT_VER is 2.12.0; bump it to $VER when the rollout is 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
|