diff --git a/CLAUDE.md b/CLAUDE.md index f7679a4..a2fec85 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -69,7 +69,7 @@ npm run demo:drift # meta verify --templates --codegen — THE DRIFT GATE ( npm run check # demo:drift → verify → verify:db → tsc → vitest (no regen: fails on drift) npm run dev # wrangler dev (needs .dev.vars with ANTHROPIC_API_KEY) npm run build:web # build the React SPA into public/ -npm run deploy # predeploy (check, verify:db:remote, build:web), then wrangler deploy +npm run deploy # predeploy (check, verify:db:remote, build:web), then wrangler deploy via scripts/cf-deploy.sh (narrow deploy token) ``` See [`docs/runbook.md`](docs/runbook.md) for deploy / regen / rotate / smoke-test procedures. diff --git a/docs/runbook.md b/docs/runbook.md index 24c4532..d114737 100644 --- a/docs/runbook.md +++ b/docs/runbook.md @@ -16,6 +16,18 @@ schema drift, `tsc`, the test suite), then `npm run verify:db:remote` (productio schema drift) and `npm run build:web`, and stops at the first failure. Never call `wrangler deploy` directly: it skips every one of those gates. +**Deploy credentials.** `npm run deploy`, `npm run db:migrate:remote` and +`npm run verify:db:remote` run through [`scripts/cf-deploy.sh`](../scripts/cf-deploy.sh), +which uses the site's narrow deploy-only token, not `wrangler login` OAuth or an +account-wide key. It pulls `CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD` and +`CLOUDFLARE_ACCOUNT_ID` from Infisical (`deploy` project, `dev` env) via `keys-axi run`, +exposes the token to wrangler as `CLOUDFLARE_API_TOKEN`, drops any inherited +`CLOUDFLARE_EMAIL` / `CLOUDFLARE_API_KEY` / `CLOUDFLARE_API_TOKEN`, and runs the +repo's own pinned wrangler from `node_modules/.bin`. The token covers Workers +Scripts, D1 and Workers KV writes plus Account Settings read on the account, and +Workers Routes write plus Zone read on `wizardsofodd.com` only, so a command outside +that scope (such as `wrangler tail`) still needs your own login. + Production deploys are direct from `main`. Smoke-check: ```bash diff --git a/package.json b/package.json index ec80c92..2e49971 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "dev": "wrangler dev", "test": "vitest run", "test:watch": "vitest", - "deploy": "wrangler deploy", + "deploy": "scripts/cf-deploy.sh wrangler deploy", "gen:db": "meta gen", "verify:portraits": "tsx scripts/verify-portraits.ts", "verify": "npm run verify:portraits", @@ -17,9 +17,9 @@ "build:web": "vite build", "predeploy": "npm run check && npm run verify:db:remote && npm run build:web", "db:migrate:local": "wrangler d1 migrations apply wizardsofodd --local", - "db:migrate:remote": "wrangler d1 migrations apply wizardsofodd --remote", + "db:migrate:remote": "scripts/cf-deploy.sh wrangler d1 migrations apply wizardsofodd --remote", "verify:db": "npm run db:migrate:local && meta verify --dialect d1 --d1 DB", - "verify:db:remote": "meta verify --dialect d1 --d1 DB --remote", + "verify:db:remote": "scripts/cf-deploy.sh meta verify --dialect d1 --d1 DB --remote", "gen:assets": "tsx scripts/generate-assets.ts", "gen:favicons": "tsx scripts/generate-favicons.ts", "postinstall": "wrangler types", diff --git a/scripts/cf-deploy.sh b/scripts/cf-deploy.sh new file mode 100755 index 0000000..e164323 --- /dev/null +++ b/scripts/cf-deploy.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# Run a Cloudflare command against the wizardsofodd Worker with the site's narrow +# deploy-only token (Infisical deploy/dev CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD: +# Account Workers Scripts Write, D1 Write, Workers KV Storage Write, Account Settings +# Read; zone wizardsofodd.com Workers Routes Write, Zone Read). Used by +# `npm run deploy`, `npm run db:migrate:remote` and `npm run verify:db:remote`. +# +# Usage: scripts/cf-deploy.sh [args...] e.g. scripts/cf-deploy.sh wrangler deploy +# The command runs with the repo's own lockfile-pinned wrangler (node_modules/.bin) +# first on PATH, so `wrangler` and `meta verify --remote` (which shells out to +# `wrangler`) both use it. Override the keys-axi binary with KEYS_AXI. +# Nothing here prints a token value. +set -euo pipefail + +[ "$#" -gt 0 ] || { echo "usage: cf-deploy.sh [args...]" >&2; exit 2; } + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +[ -x "$repo_root/node_modules/.bin/wrangler" ] || { + echo "cf-deploy.sh: $repo_root/node_modules/.bin/wrangler missing; run npm install" >&2 + exit 1 +} +keys_axi="${KEYS_AXI:-$(command -v keys-axi || echo "$HOME/.local/bin/keys-axi")}" + +# The global-key variables must not reach wrangler: it would prefer them over the token. +# The inner script is single-quoted on purpose: it expands after keys-axi injects the keys. +# shellcheck disable=SC2016 +exec env -u CLOUDFLARE_EMAIL -u CLOUDFLARE_API_KEY -u CLOUDFLARE_API_TOKEN \ + "$keys_axi" run --project deploy --env dev \ + --only CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD CLOUDFLARE_ACCOUNT_ID -- \ + bash -c ' + set -euo pipefail + export CLOUDFLARE_API_TOKEN="${CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD:?}" + unset CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD + : "${CLOUDFLARE_ACCOUNT_ID:?}" + export PATH="$1/node_modules/.bin:$PATH" + shift + exec "$@" + ' cf-deploy "$repo_root" "$@"