|
| 1 | +# Deploy boundary |
| 2 | + |
| 3 | +The deploy workflows (`deploy.yml`, `deploy-site.yml`, `deploy-files.yml`, |
| 4 | +`deploy-update.yml`) split every service in two: |
| 5 | + |
| 6 | +- **build** runs repository code (`bun install`, `cf build`, `bun script/build.ts`, |
| 7 | + `wrangler deploy --dry-run`) with no environment, no secrets, and no OIDC |
| 8 | + token. It uploads the deploy-ready output with a `SHA256SUMS` and reports that |
| 9 | + manifest's digest as a job output. |
| 10 | +- **deploy** runs in the `dev` or `production` environment, assumes the stage's |
| 11 | + AWS role, mints a short-lived Cloudflare token from the token issuer |
| 12 | + (`cloudflare-token.sh`), and checks out only `.github/release/`. It verifies |
| 13 | + the artifact against the digest the build job reported, installs the pinned |
| 14 | + `cf`/`wrangler` CLI (`npm ci --ignore-scripts`), and runs only a |
| 15 | + `deploy-*.sh` script here. |
| 16 | + |
| 17 | +Repository code outside `.github/` does not need Dax's review, so the deploy job |
| 18 | +must never execute it. The scripts treat the artifact as data: Worker code and |
| 19 | +assets may change freely, but names, routes, custom domains, crons, Workflow and |
| 20 | +export names, and every binding (including service bindings to other Workers) |
| 21 | +come from `targets/`. An artifact whose resolved config is not byte-for-byte one |
| 22 | +of those pinned targets is refused before the token is used. |
| 23 | + |
| 24 | +For `cf` services the pinned target is also what `cf` reads: the script copies |
| 25 | +the target over the artifact's config after validating it. For `wrangler` |
| 26 | +services the artifact is only `index.js`; the complete `wrangler.json` is the |
| 27 | +pinned target. |
| 28 | + |
| 29 | +## Why the token can be account-wide |
| 30 | + |
| 31 | +Cloudflare has no per-Worker permission for Workflows. The data sync Worker owns |
| 32 | +a Workflow, and `PUT /accounts/<id>/workflows/<name>` needs account-wide |
| 33 | +`Workers Scripts Write`, so `Individual Workers Editor` fails that deploy. |
| 34 | + |
| 35 | +Dev and production therefore mint a token that can write any Worker on the |
| 36 | +account. That is only safe because the code holding it is this directory: one |
| 37 | +reviewed script per service, on a verified artifact, with every destination |
| 38 | +pinned in `targets/`. Do not move a deploy back into a job that runs repository |
| 39 | +code, and do not widen a script's accepted targets without review. |
| 40 | + |
| 41 | +Production keeps this token only for a Dax-approved fast-forward of `dev`; a |
| 42 | +`dev` merge cannot reach the production environment. |
| 43 | + |
| 44 | +## Production promotion notes |
| 45 | + |
| 46 | +Moving `dev` to `production` deploys the same pinned targets with the same |
| 47 | +scripts, but production holds an account-wide token and a bad deploy is live. |
| 48 | + |
| 49 | +Before promoting: |
| 50 | + |
| 51 | +1. Confirm every dev deploy job is green on the commit being promoted |
| 52 | + (`deploy`, `deploy-site`, `deploy-files`; `deploy-update` runs on production |
| 53 | + only). |
| 54 | +2. Diff `.github/release/targets/` against `origin/production`. Any change here |
| 55 | + is a change to what production may deploy. |
| 56 | +3. Rollback is a fast-forward back to the previous `production` commit, which |
| 57 | + redeploys the previous Worker code from the same pinned targets. If a target |
| 58 | + itself changed and must be reverted, revert it with the deploy commit. |
| 59 | +4. The production deploy job runs only `.github/release/` at `github.sha`; it |
| 60 | + does not run repo code. The build job still runs repo code, but with no |
| 61 | + secrets and no OIDC token. |