Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions docs/runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
"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",
"check": "npm run demo:drift && npm run verify && npm run verify:db && tsc -b && vitest run",
"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",
Expand Down
38 changes: 38 additions & 0 deletions scripts/cf-deploy.sh
Original file line number Diff line number Diff line change
@@ -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 <command> [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 <command> [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" "$@"