Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·70 lines (61 loc) · 2.51 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·70 lines (61 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# Runs on Adam's VPS from debatelab-staging-sync.timer. Keeps a dedicated clone
# of main and syncs staging whenever main moves, plus an hourly drift check.
# Never runs npm install: the sync scripts have no dependencies, so merged code
# only reaches the Supabase CLI as files to upload, never as packages to run.
set -euo pipefail
BASE="${DEBATELAB_STAGING_SYNC_HOME:-$HOME/ops/debatelab-staging-sync}"
REPO="$BASE/repo"
STATE="$BASE/state"
mkdir -p "$STATE"
exec 9>"$BASE/lock"
flock -n 9 || { echo "Another sync is running."; exit 0; }
if [ ! -d "$REPO/.git" ]; then
git clone --quiet https://github.com/Programcode/DebateOwl.git "$REPO"
fi
git -C "$REPO" fetch --quiet origin main
sha="$(git -C "$REPO" rev-parse origin/main)"
# Skip when this commit already synced within the hour, or failed within the hour.
for marker in ok failed; do
if [ "$(cat "$STATE/last-$marker" 2>/dev/null)" = "$sha" ] && [ -n "$(find "$STATE/last-$marker" -mmin -60)" ]; then
exit 0
fi
done
# Don't change the backend while a browser job is testing it. Only a running
# browser job counts: a queued run, or one still waiting in its gate for this
# very sync, isn't using staging yet, and an older run's gate skips once main
# moves on. The browser job times out after 30 minutes.
fail_query() {
echo "Could not ask GitHub whether staging E2E is running; not syncing blind." >&2
exit 1
}
busy=0
runs="$(gh run list --repo Programcode/DebateOwl --workflow staging-e2e.yml --status in_progress --limit 100 \
--json databaseId --jq '.[].databaseId')" || fail_query
for id in $runs; do
testing="$(gh run view "$id" --repo Programcode/DebateOwl --json jobs \
--jq '[.jobs[] | select(.name == "staging-e2e" and .status == "in_progress")] | length')" || fail_query
[ "$testing" = "0" ] || busy=1
done
if [ "$busy" != "0" ]; then
echo "Protected staging E2E is testing; syncing $sha after it finishes."
exit 0
fi
git -C "$REPO" checkout --quiet --force --detach "$sha"
git -C "$REPO" clean -fdq
if [ ! -f "$REPO/scripts/staging-sync.mjs" ]; then
echo "main ($sha) has no scripts/staging-sync.mjs yet; nothing to do."
exit 0
fi
seed=()
[ "$(cat "$STATE/last-ok" 2>/dev/null)" = "$sha" ] || seed=(--seed)
if node "$REPO/scripts/staging-sync.mjs" --root "$REPO" --report "${seed[@]}"; then
echo "$sha" > "$STATE/last-ok"
rm -f "$STATE/last-failed"
else
status=$?
# 3: a browser job started while syncing; nothing changed, retry next tick.
[ "$status" -eq 3 ] && exit 0
echo "$sha" > "$STATE/last-failed"
exit "$status"
fi