deploy.sh: submit a Trace deployment receipt after compose up
Trace keeps build and deployment receipts separate on purpose, but nothing was ever wired up to emit the second one from a plain docker-compose deploy host — so Trace kept reporting the last commit someone had told it about, not what's actually running. Opt-in per project via a .trace-meta marker pointing at a site-meta-shaped health endpoint; missing marker, missing TRACE_* config, or Trace being unreachable are all non-fatal, matching the existing keep-pull best-effort style. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
a640bc1faf
commit
0af576d8a8
@@ -8,6 +8,18 @@
|
|||||||
# Expects a directory at $DEPLOY_ROOT/<project>/ containing a
|
# Expects a directory at $DEPLOY_ROOT/<project>/ containing a
|
||||||
# docker-compose.yml.
|
# docker-compose.yml.
|
||||||
#
|
#
|
||||||
|
# After the stack is up, optionally submits a Trace deployment receipt
|
||||||
|
# (see trace/docs/INTEGRATING_PROJECTS.md) — opt-in per project via a
|
||||||
|
# $dir/.trace-meta marker file containing one URL to a site-meta-shaped
|
||||||
|
# health endpoint (a JSON body with an embedded build identity is enough:
|
||||||
|
# {"build":{"commit":...,"artifacts":[...]}}). No marker file, no
|
||||||
|
# TRACE_EVENTS_URL/TRACE_RECEIPT_TOKEN in the pulled .env, or Trace being
|
||||||
|
# unreachable are all non-fatal — the compose deploy has already
|
||||||
|
# happened by the time this runs, same "best effort" philosophy as the
|
||||||
|
# keep pull below. A health check that never responds still submits a
|
||||||
|
# receipt, honestly reporting result:"failed" — absence of a receipt
|
||||||
|
# means "unknown" to Trace, never "success".
|
||||||
|
#
|
||||||
# The vault key is NOT assumed to match the deploy-directory name — repo
|
# The vault key is NOT assumed to match the deploy-directory name — repo
|
||||||
# names, deploy directory names, and vault keys are three independent
|
# names, deploy directory names, and vault keys are three independent
|
||||||
# strings in practice (a repo can be named one thing, its deploy
|
# strings in practice (a repo can be named one thing, its deploy
|
||||||
@@ -51,3 +63,48 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
(cd "$dir" && docker compose up -d)
|
(cd "$dir" && docker compose up -d)
|
||||||
|
|
||||||
|
# --- Trace deployment receipt (best effort — never fails the deploy) ---
|
||||||
|
if [ -f "$dir/.trace-meta" ]; then
|
||||||
|
meta_url="$(cat "$dir/.trace-meta")"
|
||||||
|
trace_events_url=""
|
||||||
|
trace_token=""
|
||||||
|
if [ -f "$dir/.env" ]; then
|
||||||
|
trace_events_url="$(grep -m1 '^TRACE_EVENTS_URL=' "$dir/.env" | cut -d= -f2-)"
|
||||||
|
trace_token="$(grep -m1 '^TRACE_RECEIPT_TOKEN=' "$dir/.env" | cut -d= -f2-)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$trace_events_url" ] || [ -z "$trace_token" ]; then
|
||||||
|
echo " · trace receipt skipped: TRACE_EVENTS_URL/TRACE_RECEIPT_TOKEN not set in $dir/.env"
|
||||||
|
else
|
||||||
|
echo " · waiting for $meta_url to report healthy…"
|
||||||
|
body=""
|
||||||
|
for _ in $(seq 1 15); do
|
||||||
|
if body="$(curl -fsS --max-time 5 "$meta_url" 2>/dev/null)"; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
body=""
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
ts="$(date -u +%Y%m%dT%H%M%SZ)"
|
||||||
|
occurred_at="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||||
|
|
||||||
|
if [ -z "$body" ]; then
|
||||||
|
echo " ⚠ $meta_url never responded — submitting a failed deployment receipt"
|
||||||
|
payload="{\"schemaVersion\":1,\"eventType\":\"deployment\",\"eventId\":\"${project}-${env}-unknown-${ts}\",\"project\":\"${project}\",\"environment\":\"${env}\",\"occurredAt\":\"${occurred_at}\",\"result\":\"failed\",\"verification\":{\"health\":\"unreachable\"}}"
|
||||||
|
else
|
||||||
|
commit="$(node -e 'let b="";process.stdin.on("data",d=>b+=d);process.stdin.on("end",()=>{try{const d=JSON.parse(b);process.stdout.write((d.build&&d.build.commit)||"unknown")}catch{process.stdout.write("unknown")}})' <<<"$body")"
|
||||||
|
artifacts="$(node -e 'let b="";process.stdin.on("data",d=>b+=d);process.stdin.on("end",()=>{try{const d=JSON.parse(b);process.stdout.write(JSON.stringify((d.build&&d.build.artifacts)||[]))}catch{process.stdout.write("[]")}})' <<<"$body")"
|
||||||
|
short_commit="${commit:0:7}"
|
||||||
|
payload="{\"schemaVersion\":1,\"eventType\":\"deployment\",\"eventId\":\"${project}-${env}-${short_commit}-${ts}\",\"project\":\"${project}\",\"environment\":\"${env}\",\"occurredAt\":\"${occurred_at}\",\"result\":\"success\",\"source\":{\"commit\":\"${commit}\"},\"artifacts\":${artifacts},\"verification\":{\"health\":\"healthy\"}}"
|
||||||
|
echo " ✓ deploy verified healthy at commit ${short_commit}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if curl -fsS --max-time 5 -H "Authorization: Bearer ${trace_token}" -H "Content-Type: application/json" --data-binary "$payload" "$trace_events_url" >/dev/null 2>&1; then
|
||||||
|
echo " ✓ submitted deployment receipt to trace"
|
||||||
|
else
|
||||||
|
echo " · trace receipt submission failed (deploy already happened; not fatal)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user