#!/usr/bin/env bash # Pulls a project's env from keep (if it's bootstrapped onto keep), then # brings its docker compose stack up. # # Usage: # ./deploy.sh [env] # # Expects a directory at $DEPLOY_ROOT// containing a # 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 # names, deploy directory names, and vault keys are three independent # strings in practice (a repo can be named one thing, its deploy # directory another, entirely by history/accident). Resolution: # 1. $dir/.keep-vault, if present — a one-time-per-project marker # (`echo "" > "$dir/.keep-vault"` when bootstrapping), # one line, human-readable, cat-able to check what's configured. # 2. Otherwise falls back to "/" — works when the vault # key genuinely does match, and is also the graceful "not # bootstrapped yet" case for a keep pull that's expected to fail. # # `keep pull` is always best-effort: a missing vault, a missing grant, or # `keep` not being installed at all is never fatal here — it means "keep # doesn't apply to this project (yet)," never "stop the deploy." Whatever # .env already exists on disk (hand-copied, or from a previous keep pull) # is what docker compose reads either way. set -euo pipefail project="${1:?usage: deploy.sh [env]}" env="${2:-production}" root="${DEPLOY_ROOT:-/opt/docker}" dir="$root/$project" if [ ! -d "$dir" ]; then echo "error: $dir does not exist" >&2 exit 1 fi if [ -f "$dir/.keep-vault" ]; then vault="$(cat "$dir/.keep-vault")" else vault="$project/$env" fi if command -v keep >/dev/null 2>&1 && keep pull "$vault" --out "$dir/.env.new" 2>/dev/null; then mv "$dir/.env.new" "$dir/.env" echo " ✓ pulled $vault" else rm -f "$dir/.env.new" echo " · keep pull skipped or failed for $vault (using existing .env, if any)" fi (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