← back to Ga4 Fleet

deploy-propagated.sh

25 lines

#!/usr/bin/env bash
# Deploy the 42 propagated sites (gtag added to untagged pages) to Kamatera + verify.
# Operator-run: a 42-site customer-facing deploy is gated for the agent, so Steve runs this.
# Each deploy is: rsync source (no node_modules/.env/.git, no --delete) -> pm2 reload -> curl-verify
# the site's own Measurement ID is live. Reversible per site: `git revert HEAD` in the repo + re-run.
set -uo pipefail
cd "$(dirname "$0")"
node -e 'require("./deploy-plan.json").forEach(p=>console.log([p.repo,p.base,p.pm2,p.domain,p.id].join("\t")))' > /tmp/ga4_deploy.tsv
pass=0; fail=0; failed=""
while IFS=$'\t' read -r repo base pm2 domain id; do
  rsync -az --exclude node_modules --exclude .git --exclude '.env*' --exclude '*.log' --exclude .DS_Store "$repo/" "root@45.61.58.125:/root/Projects/$base/" >/dev/null 2>&1
  # -n: do NOT read the loop's stdin (else ssh swallows the remaining plan lines and the loop runs once)
  ssh -n -o ConnectTimeout=12 root@45.61.58.125 "pm2 reload $pm2 --update-env >/dev/null 2>&1" 2>/dev/null
  # verify with a settle + one retry (a reload isn't instantly live)
  live=""
  for try in 1 2 3; do
    sleep 2
    live=$(curl -s -m 12 "https://$domain/" | grep -oE "$id" | head -1)
    [ "$live" = "$id" ] && break
  done
  if [ "$live" = "$id" ]; then pass=$((pass+1)); printf '  ok   %s\n' "$domain"; else fail=$((fail+1)); failed="$failed $domain"; printf '  FAIL %s\n' "$domain"; fi
done < /tmp/ga4_deploy.tsv
echo "=== $pass verified live · $fail failed ==="
[ -n "$failed" ] && echo "failed/unverified:$failed"