deploy: parse the CF zone id with json, not a greedy sed

The purge silently did nothing. `sed -n 's/.*"id":"\([a-f0-9]\{32\}\)".*/\1/p'`
looks right, but the zones response carries several 32-hex "id" fields
(account, plan, ...) and the leading `.*` is greedy, so it returned the
LAST one — a valid-looking id for the wrong object. The API then rejected
the purge and the old grep-for-success check reported a bare "purge
failed" with no reason.

Parse with python3 (already a dep — serve.py) and surface the API's own
error message when it fails.

Verified end to end against the live zone: cf-cache-status HIT -> purge
-> MISS -> HIT.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
m3ultra 2026-07-25 23:27:50 +10:00
parent 6466973f4e
commit 802879fc3e

View File

@ -45,17 +45,28 @@ fi
if [ -n "${CF_API_TOKEN:-}" ]; then if [ -n "${CF_API_TOKEN:-}" ]; then
echo "==> purging cloudflare" echo "==> purging cloudflare"
API=https://api.cloudflare.com/client/v4 API=https://api.cloudflare.com/client/v4
# Parse with python3 (already a dep — serve.py), NOT sed. The zone response
# carries several 32-hex "id" fields (account, plan, ...) and a greedy
# `.*"id":"..."` grabs the LAST one, which silently purges nothing.
ZONE=$(curl -s -H "Authorization: Bearer $CF_API_TOKEN" \ ZONE=$(curl -s -H "Authorization: Bearer $CF_API_TOKEN" \
"$API/zones?name=monsterrobot.games" | sed -n 's/.*"id":"\([a-f0-9]\{32\}\)".*/\1/p' | head -1) "$API/zones?name=monsterrobot.games" \
| python3 -c 'import sys,json;r=json.load(sys.stdin).get("result") or [];print(r[0]["id"] if r else "")')
if [ -n "$ZONE" ]; then if [ -n "$ZONE" ]; then
files=$(cd "$SRC" && find index.html src -type f \ files=$(cd "$SRC" && find index.html src -type f \
| sed "s|^|\"https://monsterrobot.games/mollycool/|; s|$|\"|" | paste -sd, -) | python3 -c 'import sys,json;print(json.dumps({"files":["https://monsterrobot.games/mollycool/"+l.strip() for l in sys.stdin if l.strip()]}))')
ok=$(curl -s -X POST "$API/zones/$ZONE/purge_cache" \ resp=$(curl -s -X POST "$API/zones/$ZONE/purge_cache" \
-H "Authorization: Bearer $CF_API_TOKEN" -H 'Content-Type: application/json' \ -H "Authorization: Bearer $CF_API_TOKEN" -H 'Content-Type: application/json' \
--data "{\"files\":[$files]}" | grep -o '"success":true' || true) --data "$files")
[ -n "$ok" ] && echo " purged" || echo " !! purge failed — hard-reload to be sure" echo "$resp" | python3 -c '
import sys,json
d=json.load(sys.stdin)
if d.get("success"): print(" purged")
else:
print(" !! purge failed:", "; ".join(e.get("message","?") for e in d.get("errors") or []))
print(" hard-reload to be sure")'
else else
echo " !! could not resolve zone id — hard-reload to be sure" echo " !! could not resolve zone id — check the token has Zone:Read"
echo " hard-reload to be sure"
fi fi
else else
echo "==> NO CF_API_TOKEN — skipping purge." echo "==> NO CF_API_TOKEN — skipping purge."