Skip to main content

max / makenotwork

2.3 KB · 63 lines History Blame Raw
1 #!/bin/bash
2 # CI watcher for astra — polls WAM for ci-trigger tickets and runs CI.
3 #
4 # Runs as a background service on astra. Checks WAM every 30 seconds for
5 # open tickets with source=ci-trigger. When found, claims the ticket
6 # (marks it in-progress), runs CI, and updates the ticket with results.
7 #
8 # Location on astra: /home/max/mnw-ci/server/deploy/ci-watcher.sh
9 # Systemd unit: ci-watcher.service
10 #
11 # Usage: ci-watcher.sh
12
13 set -uo pipefail
14
15 export PATH="$HOME/.cargo/bin:$PATH"
16
17 WAM_URL="${WAM_URL:-http://100.120.174.96:7890}"
18 REPO_DIR="$HOME/mnw-ci"
19 POLL_INTERVAL=30
20 CI_SCRIPT="$REPO_DIR/server/deploy/ci-on-push.sh"
21
22 echo "CI watcher started (polling WAM every ${POLL_INTERVAL}s)"
23
24 while true; do
25 sleep "$POLL_INTERVAL"
26
27 # Check for open ci-trigger tickets
28 RESPONSE=$(curl -sf "$WAM_URL/tickets?source=ci-trigger&status=open" 2>/dev/null) || continue
29 COUNT=$(echo "$RESPONSE" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("count",0))' 2>/dev/null || echo "0")
30
31 if [ "$COUNT" = "0" ] || [ -z "$COUNT" ]; then
32 continue
33 fi
34
35 # Get the first trigger ticket
36 TICKET_ID=$(echo "$RESPONSE" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d["data"][0]["id"])' 2>/dev/null) || continue
37 COMMIT=$(echo "$RESPONSE" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d["data"][0].get("source_ref","unknown"))' 2>/dev/null || echo "unknown")
38
39 echo ""
40 echo "=== CI trigger found: $COMMIT ==="
41
42 # Claim the ticket (mark in-progress so we don't pick it up again)
43 curl -sf -X PATCH "$WAM_URL/tickets/$TICKET_ID" \
44 -H "Content-Type: application/json" \
45 -d '{"status": "in-progress"}' >/dev/null 2>&1
46
47 # Run CI
48 if "$CI_SCRIPT" main 2>&1; then
49 # CI passed — close the trigger ticket
50 curl -sf -X PATCH "$WAM_URL/tickets/$TICKET_ID" \
51 -H "Content-Type: application/json" \
52 -d '{"status": "closed"}' >/dev/null 2>&1
53 else
54 # CI failed — ci-on-push.sh already created a failure ticket and reverted.
55 # Close the trigger ticket.
56 curl -sf -X PATCH "$WAM_URL/tickets/$TICKET_ID" \
57 -H "Content-Type: application/json" \
58 -d '{"status": "closed"}' >/dev/null 2>&1
59 fi
60
61 echo "=== CI run complete ==="
62 done
63