Skip to content

Commit cffbf7a

Browse files
authored
chore(deploy): deploy with the narrow wizardsofodd Workers token (#53)
Route npm run deploy, db:migrate:remote and verify:db:remote through scripts/cf-deploy.sh, which pulls CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD and CLOUDFLARE_ACCOUNT_ID from Infisical deploy/dev via keys-axi, maps the token to CLOUDFLARE_API_TOKEN, drops inherited global-key variables and runs the repo's lockfile-pinned wrangler. Replaces wrangler OAuth / account-wide credentials for these commands.
1 parent da9ded4 commit cffbf7a

4 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ npm run demo:drift # meta verify --templates --codegen — THE DRIFT GATE (
6969
npm run check # demo:drift → verify → verify:db → tsc → vitest (no regen: fails on drift)
7070
npm run dev # wrangler dev (needs .dev.vars with ANTHROPIC_API_KEY)
7171
npm run build:web # build the React SPA into public/
72-
npm run deploy # predeploy (check, verify:db:remote, build:web), then wrangler deploy
72+
npm run deploy # predeploy (check, verify:db:remote, build:web), then wrangler deploy via scripts/cf-deploy.sh (narrow deploy token)
7373
```
7474

7575
See [`docs/runbook.md`](docs/runbook.md) for deploy / regen / rotate / smoke-test procedures.

‎docs/runbook.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ schema drift, `tsc`, the test suite), then `npm run verify:db:remote` (productio
1616
schema drift) and `npm run build:web`, and stops at the first failure. Never call
1717
`wrangler deploy` directly: it skips every one of those gates.
1818

19+
**Deploy credentials.** `npm run deploy`, `npm run db:migrate:remote` and
20+
`npm run verify:db:remote` run through [`scripts/cf-deploy.sh`](../scripts/cf-deploy.sh),
21+
which uses the site's narrow deploy-only token, not `wrangler login` OAuth or an
22+
account-wide key. It pulls `CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD` and
23+
`CLOUDFLARE_ACCOUNT_ID` from Infisical (`deploy` project, `dev` env) via `keys-axi run`,
24+
exposes the token to wrangler as `CLOUDFLARE_API_TOKEN`, drops any inherited
25+
`CLOUDFLARE_EMAIL` / `CLOUDFLARE_API_KEY` / `CLOUDFLARE_API_TOKEN`, and runs the
26+
repo's own pinned wrangler from `node_modules/.bin`. The token covers Workers
27+
Scripts, D1 and Workers KV writes plus Account Settings read on the account, and
28+
Workers Routes write plus Zone read on `wizardsofodd.com` only, so a command outside
29+
that scope (such as `wrangler tail`) still needs your own login.
30+
1931
Production deploys are direct from `main`. Smoke-check:
2032

2133
```bash

‎package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
"dev": "wrangler dev",
1010
"test": "vitest run",
1111
"test:watch": "vitest",
12-
"deploy": "wrangler deploy",
12+
"deploy": "scripts/cf-deploy.sh wrangler deploy",
1313
"gen:db": "meta gen",
1414
"verify:portraits": "tsx scripts/verify-portraits.ts",
1515
"verify": "npm run verify:portraits",
1616
"check": "npm run demo:drift && npm run verify && npm run verify:db && tsc -b && vitest run",
1717
"build:web": "vite build",
1818
"predeploy": "npm run check && npm run verify:db:remote && npm run build:web",
1919
"db:migrate:local": "wrangler d1 migrations apply wizardsofodd --local",
20-
"db:migrate:remote": "wrangler d1 migrations apply wizardsofodd --remote",
20+
"db:migrate:remote": "scripts/cf-deploy.sh wrangler d1 migrations apply wizardsofodd --remote",
2121
"verify:db": "npm run db:migrate:local && meta verify --dialect d1 --d1 DB",
22-
"verify:db:remote": "meta verify --dialect d1 --d1 DB --remote",
22+
"verify:db:remote": "scripts/cf-deploy.sh meta verify --dialect d1 --d1 DB --remote",
2323
"gen:assets": "tsx scripts/generate-assets.ts",
2424
"gen:favicons": "tsx scripts/generate-favicons.ts",
2525
"postinstall": "wrangler types",

‎scripts/cf-deploy.sh‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
# Run a Cloudflare command against the wizardsofodd Worker with the site's narrow
3+
# deploy-only token (Infisical deploy/dev CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD:
4+
# Account Workers Scripts Write, D1 Write, Workers KV Storage Write, Account Settings
5+
# Read; zone wizardsofodd.com Workers Routes Write, Zone Read). Used by
6+
# `npm run deploy`, `npm run db:migrate:remote` and `npm run verify:db:remote`.
7+
#
8+
# Usage: scripts/cf-deploy.sh <command> [args...] e.g. scripts/cf-deploy.sh wrangler deploy
9+
# The command runs with the repo's own lockfile-pinned wrangler (node_modules/.bin)
10+
# first on PATH, so `wrangler` and `meta verify --remote` (which shells out to
11+
# `wrangler`) both use it. Override the keys-axi binary with KEYS_AXI.
12+
# Nothing here prints a token value.
13+
set -euo pipefail
14+
15+
[ "$#" -gt 0 ] || { echo "usage: cf-deploy.sh <command> [args...]" >&2; exit 2; }
16+
17+
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
18+
[ -x "$repo_root/node_modules/.bin/wrangler" ] || {
19+
echo "cf-deploy.sh: $repo_root/node_modules/.bin/wrangler missing; run npm install" >&2
20+
exit 1
21+
}
22+
keys_axi="${KEYS_AXI:-$(command -v keys-axi || echo "$HOME/.local/bin/keys-axi")}"
23+
24+
# The global-key variables must not reach wrangler: it would prefer them over the token.
25+
# The inner script is single-quoted on purpose: it expands after keys-axi injects the keys.
26+
# shellcheck disable=SC2016
27+
exec env -u CLOUDFLARE_EMAIL -u CLOUDFLARE_API_KEY -u CLOUDFLARE_API_TOKEN \
28+
"$keys_axi" run --project deploy --env dev \
29+
--only CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD CLOUDFLARE_ACCOUNT_ID -- \
30+
bash -c '
31+
set -euo pipefail
32+
export CLOUDFLARE_API_TOKEN="${CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD:?}"
33+
unset CLOUDFLARE_WORKERS_TOKEN_WIZARDSOFODD
34+
: "${CLOUDFLARE_ACCOUNT_ID:?}"
35+
export PATH="$1/node_modules/.bin:$PATH"
36+
shift
37+
exec "$@"
38+
' cf-deploy "$repo_root" "$@"

0 commit comments

Comments
 (0)