Skip to content
478 linesCodeBlameRaw
1---
2title: Deploy g1t to Cloudflare
3description: How g1t.sh is deployed to Cloudflare, and how to deploy the same code to an account of your own.
4---
5
6g1t.sh runs on Cloudflare, and the tool that deploys it is in the
7repository. You can deploy the same code to a Cloudflare account of your
8own. To run g1t on one machine without Cloudflare, see
9[Run g1t yourself](/guides/self-hosting/) instead.
10
11You need a Cloudflare account on the Workers Paid plan (Cloudflare
12Artifacts, where repositories are kept, requires it), Node 24,
13Rust with the `wasm32-unknown-unknown` target, and Docker to build the
14runner's image.
15
16Every `wrangler.jsonc` names g1t.sh's account, domains and resource ids.
17Before a first deploy to another account, change those to your own (see
18[A first deploy to a new account](#a-first-deploy-to-a-new-account)).
19
20## The pieces
21
22| Piece | Where |
23| --- | --- |
24| The manifest: every deployable part | `deploy/stack.jsonc` |
25| The tool | `scripts/deploy.mjs`, with its library and tests in `scripts/deploy/` |
26| Rust Worker builds | `scripts/build-rust-worker.mjs` |
27| The runner's images | `services/runner/base/Dockerfile`, `services/runner/Dockerfile`, `services/runner/base.json` |
28| The workflows | `.g1t/workflows/deploy.yml`, `.g1t/workflows/runner-base.yml` |
29
30## The manifest
31
32`deploy/stack.jsonc` lists every unit: each folder with a `wrangler.jsonc`.
33
34| Field | What it is |
35| --- | --- |
36| `path` | The unit's folder |
37| `kind` | `rust-worker`, `ts-worker`, `react-router` or `astro` |
38| `worker` | The Worker's name. It must match its `wrangler.jsonc`. |
39| `d1` | `{ database, migrations }`, when it has a database |
40| `stage` | `core`, `edge` or `front` (below) |
41| `secrets` | The Wrangler secrets it needs, by name |
42| `setup` | One-time steps a first deploy needs that no config can say |
43| `inputs` | Files outside its folder it is built from that no package names |
44| `image` | A Containers image, for the runner |
45| `self_host` | `run`, `off`, `separate` or `none`: what a [self-hosted installation](/guides/self-hosting-architecture/) does with it |
46
47What a unit is built from is read, never listed by hand: Rust path
48dependencies from `cargo metadata`, workspace packages from each
49`package.json`. A lockfile change counts for a unit only when a package
50it uses changed. `node scripts/deploy.mjs manifest` prints the result.
51
52Units deploy in stages. A stage starts only when the one before it
53succeeded, and units inside a stage deploy in parallel:
54
55| Stage | What |
56| --- | --- |
57| `migrations` | Every pending D1 migration, before any code |
58| `core` | The services, reached through bindings |
59| `edge` | Public endpoints other than the site: `api`, `pages`, `status` |
60| `front` | The site, sudo and the docs |
61
62A unit binds only to units in its own stage or an earlier one, so new code
63never calls a service that has not shipped. `npm run test:deploy` checks
64this, and that every `wrangler.jsonc` has a unit whose names match it.
65Cloudflare refuses a Worker bound to a Worker that does not exist, so
66inside a stage a unit whose Worker has never been deployed (a new or
67renamed one) goes first, and the units of its stage that bind to it go
68after it succeeds.
69
70## The tool
71
72```sh
73node scripts/deploy.mjs plan # what would deploy, and why (read-only)
74node scripts/deploy.mjs deploy # migrations, then every changed unit
75node scripts/deploy.mjs deploy --only web,api # just these, if they changed
76node scripts/deploy.mjs deploy --only web --force # just this, changed or not
77node scripts/deploy.mjs deploy --all # everything
78node scripts/deploy.mjs build --only events # build as a deploy would; upload nothing
79node scripts/deploy.mjs migrate # pending migrations only
80node scripts/deploy.mjs manifest --check # the manifest's problems
81node scripts/deploy.mjs doctor # secrets each unit lacks
82node scripts/deploy.mjs build-base # build and push the runner's base image (Docker)
83node scripts/deploy.mjs image # build and push the runner's image (Docker)
84```
85
86| Flag | What it does |
87| --- | --- |
88| `--only a,b`, `--skip a,b` | Units by short name, folder or Worker name |
89| `--all` | Every unit, changed or not |
90| `--force` | The selected units even if unchanged |
91| `--concurrency N` | Units at once inside a stage, and migrations at once (default 4) |
92| `--stage core` | One stage only |
93| `--no-migrations` | Skip the migrations step |
94| `--allow-dirty` | Deploy with uncommitted changes. The version records no commit, so the next plan deploys it again. |
95| `--rebuild-image`, `--rebuild-base` | Build the runner's image, or its base, even if one exists |
96| `--rollback` | Allow deploying a commit older than the one live |
97| `--since REV` | Treat Workers with no recorded commit as running `REV` |
98
99**Only what changed deploys.** Each deploy records its commit on the
100Worker's version (`wrangler deploy --message "g1t-deploy <sha> ..." --tag
101g1t-<sha>`). `plan` reads the live version of each Worker, diffs from its
102commit to `HEAD`, and deploys the units the change touches. A change only
103to a crate's tests deploys nothing. A version deployed any other way has
104no commit, so the unit is deployed again.
105
106With `CLOUDFLARE_API_TOKEN` (in CI) or `CLOUDFLARE_DEPLOY_TOKEN` (on your
107machine) and `CLOUDFLARE_ACCOUNT_ID`, `plan` reads Cloudflare's API itself
108and takes a few seconds. With only `wrangler login`, it asks Wrangler and
109takes minutes. Each unit's output is kept in `$TMPDIR/g1t-deploy/<unit>.log`.
110
111### Migrations run while the old code is live
112
113Migrations apply before any code, so for a minute or more the old code
114reads the new schema. Keep it working:
115
116- **Add, never change meaning.** New tables and columns with defaults are
117 safe.
118- **Change meaning in two deploys.** First ship code that reads both forms;
119 then the migration that rewrites the rows.
120- **Never drop or rename** a column or table in the same deploy that stops
121 reading it.
122
123Migrations never run backwards: undoing one is a new migration.
124
125## The workflow
126
127`.g1t/workflows/deploy.yml` runs the tool on every push to `main`, and by
128hand (**Actions → Deploy → Run workflow**) with `units`, `all` and
129`dry_run`.
130
131| Job | Does |
132| --- | --- |
133| `check` | `manifest --check` and `npm run test:deploy` |
134| `plan` | The plan, shown in the run's summary |
135| `migrate` | Pending migrations; skipped when there are none |
136| `core`, `edge`, `front` | Each stage's units, one job per build group |
137| `smoke` | Loads the landing page, sign-in, sign-up and pricing |
138
139Deploys run one at a time; a second push waits. Rust units build on
140`g1t-4core` machines, at most four to a job, with Cargo's target and
141sccache kept in the Actions cache.
142
143The deploy jobs use `environment: production`, so only they can read the
144Cloudflare token. They reach Cloudflare through the project's
145[workflow-only domains](/guides/guardrails/), under **Settings →
146Guardrails**:
147
148```text
149api.cloudflare.com | deploy.yml | production
150registry.cloudflare.com | deploy.yml, runner-base.yml | production
151```
152
153Every deploy shows on the repository's Deployments page. A deploy run by
154hand reports one too when `G1T_DEPLOY_TOKEN` holds a g1t token with
155`deployments:write`.
156
157### The Cloudflare API token
158
159Make a custom token at **dash.cloudflare.com → My Profile → API Tokens**,
160restricted to your account and zones:
161
162| Scope | Permission |
163| --- | --- |
164| Account | Workers Scripts: Edit |
165| Account | D1: Edit |
166| Account | Queues: Edit |
167| Account | Workers R2 Storage: Read |
168| Account | Account Settings: Read |
169| Account | Containers: Edit |
170| Zone | Workers Routes: Edit |
171| Zone | DNS: Edit |
172| Zone | Zone: Read |
173
174Store it on the repository under **Settings → Secrets and variables** as
175the secret `CLOUDFLARE_API_TOKEN`, for workflows in the `production`
176environment, and your account id as the variable `CLOUDFLARE_ACCOUNT_ID`.
177A missing permission fails with `Authentication error [code: 10000]` and
178the route it was refused on.
179
180## A first deploy to a new account
181
182What the configs refer to must exist first.
183`node scripts/deploy.mjs manifest --json` lists, for each unit, its
184queues, KV namespaces, R2 buckets, Vectorize indexes and dispatch
185namespaces; its `setup` and `secrets` say the rest.
186
1871. Set `account_id`, routes and custom domains in each `wrangler.jsonc` to
188 your own.
1892. D1: `npx wrangler d1 create <database>` for each, and put its id in the
190 unit's `wrangler.jsonc`.
1913. KV: `npx wrangler kv namespace create <name>` for each name under
192 `resources.kv` in the manifest, and put the ids in the configs.
1934. Queues: `npx wrangler queues create <queue>` for each queue in the
194 manifest, including the dead-letter queue `g1t-events-dlq`.
1955. R2: create `g1t-screenshots`, `g1t-actions-cache` and `g1t-git-packs`,
196 with lifecycle rules:
197
198 ```sh
199 npx wrangler r2 bucket lifecycle add g1t-actions-cache expire-cache c/ --expire-days 30 --abort-multipart-days 1
200 npx wrangler r2 bucket lifecycle add g1t-actions-cache expire-artifacts a/ --expire-days 91 --abort-multipart-days 1
201 npx wrangler r2 bucket lifecycle add g1t-git-packs expire-packs packs/ --expire-days 7 --abort-multipart-days 1
202 ```
203
2046. The runner's base image: `node scripts/deploy.mjs build-base`, then
205 commit `services/runner/base.json`.
2067. Everything else each unit's `setup` names: Vectorize, the dispatch
207 namespace, DNS, Access, Email Sending and Artifacts.
2088. Secrets: `npx wrangler secret put <NAME>` in the unit's folder.
209 `node scripts/deploy.mjs doctor` lists what is missing.
2109. For [Deployments](/guides/deployments/), which need the Workers for
211 Platforms add-on and a zone for apps: `scripts/setup-deployments.sh`.
21210. Durable Objects: the artifacts service's v3 migration
213 (`services/artifacts/wrangler.jsonc`) moves g1t.sh's live rooms from
214 the Worker it was renamed from, which a new account does not have.
215 Replace it with
216 `{ "tag": "v3", "new_sqlite_classes": ["PageRoom", "FolioRoom"] }`.
21711. `node scripts/deploy.mjs deploy --all`. Inside a stage, Workers that do
218 not exist yet go before those bound to them; a Worker bound to one in a
219 later stage, or to one that failed, is refused until it exists.
22012. Register on your site to make the first account, or run
221 `node services/identity/scripts/create-user.mjs <username>`.
222
223### Adding a unit
224
2251. Make its folder with a `wrangler.jsonc`. A Rust Worker is a member of
226 the root `Cargo.toml`, with `node ../../scripts/build-rust-worker.mjs`
227 as its build command and `wasm-opt = ["-O1"]`; a TypeScript one is an
228 npm workspace.
2292. Add it to `deploy/stack.jsonc` in the earliest stage after everything
230 it binds to, with its secrets, setup and `self_host`.
2313. Add a row for it to the table in
232 [How a self-hosted g1t runs](/guides/self-hosting-architecture/#each-part).
2334. `npm run test:deploy` and `node scripts/deploy.mjs manifest --check`
234 say what is missing.
235
236### Renaming a Worker
237
238A Worker's name is its identity on Cloudflare, so a renamed Worker is a
239new one. Keep its resources: D1 databases, R2 buckets, Vectorize indexes
240and queues are bound by id or name and keep theirs. Durable Objects
241belong to a Worker, so the new one takes them with a
242[transfer migration](https://developers.cloudflare.com/durable-objects/reference/durable-object-class-migrations-legacy/#transfer-migration)
243(`transferred_classes`, with `from_script` the old name), every object's
244storage with them; until the old Worker is deleted, its own bindings to
245them reach the new one. A queue has one consumer, so the old Worker lets
246go of it first.
247
248The artifacts service was `g1t-docs-service` (`services/docs`) and is
249`g1t-artifacts` (`services/artifacts`). An installation that ran the old
250one moves once, in this order:
251
2521. Let go of the queue. Its messages wait for the new consumer:
253
254 ```sh
255 npx wrangler queues consumer remove g1t-events-docs g1t-docs-service
256 ```
257
2582. Deploy: push to `main`, or `node scripts/deploy.mjs deploy`.
259 `g1t-artifacts` goes first in `core`, takes `PageRoom` and `FolioRoom`
260 and the queue; then `api` (edge) and `web` (front), each bound to it.
261 Until each has gone out it still calls
262 `g1t-docs-service`, which answers from the same database, bucket and
263 (forwarded) rooms.
2643. Check: an artifact opens with its content and edits live, a file
265 uploads, and `npx wrangler queues info g1t-events-docs` names
266 `g1t-artifacts` as the consumer. Cloudflare's
267 `GET /accounts/{account_id}/workers/durable_objects/namespaces` lists
268 `PageRoom` and `FolioRoom` under `g1t-artifacts`.
2694. After a day with no requests to it (`npx wrangler tail g1t-docs-service`
270 shows only its 03:17 UTC cron), delete it by hand:
271 `npx wrangler delete g1t-docs-service`. Never with `--force`: a refusal
272 means something still binds to it.
273
274If step 2 fails before `g1t-artifacts` deploys, nothing moved: put the
275consumer back with `npx wrangler deploy` in `services/docs` at the
276previous commit. Once it has deployed, the rooms are its own: fix forward.
277
278## The runner's images
279
280`services/runner` runs every sandbox (checks, the merge queue,
281workflow jobs and deploy builds) from one Containers image in two parts:
282
283| Image | Built from | Rebuilt |
284| --- | --- | --- |
285| The base | `services/runner/base/Dockerfile`: Debian, Node, Python, Docker, Go, Rust, Java, .NET, Ruby and the Claude Code CLI | When its folder changes, weekly by `runner-base.yml`, or by hand with `build-base` |
286| The runner | `services/runner/Dockerfile`: the base plus the `g1t-runner` binary | When the binary or the base changes |
287
288`services/runner/base.json` records the base that was pushed;
289`npm run test:deploy` fails while it and the base's folder disagree. The
290runner's image is tagged with a hash of what it is built from, so a deploy
291builds it only when the registry does not already have it. When nothing
292the image is built from changed, the deploy leaves running sandboxes
293alone. When it did, Cloudflare replaces sandboxes on the new image but
294leaves any that started within the last two hours
295(`rollout_active_grace_period` in `services/runner/wrangler.jsonc`) to
296finish first. A workflow job on g1t's machines runs for at most 60
297minutes, so a deploy never cuts one short; an agent run whose guardrails
298allow more than two hours can be.
299
300Jobs choose larger machines with `runs-on: g1t-2core` or `g1t-4core`. Your
301account's Containers limits must allow `standard-4`.
302
303Set the runner Worker's `DOCKER` variable to `off` to give workflow jobs no
304Docker Engine.
305
306`CLOUDFLARE_API_TOKEN=<token> node scripts/ops/runner-errors.mjs` sorts the
307sandboxes' errors over the last 7 days into expected ones (a deploy
308resetting them, no free capacity, a container that stopped) and ones to
309read.
310
311## Docker deployments
312
313Container apps ([Docker deployments](/guides/deployments/#docker-deployments))
314are off until `CONTAINERS` is `on` in `services/deployments/wrangler.jsonc`.
315The code follows Cloudflare's documentation and Wrangler's own client for
316these APIs, but none of them has been called against g1t's account yet.
317Before turning it on, check each of these with a test project:
318
3191. **The token.** `CLOUDFLARE_API_TOKEN` of `services/deployments` has
320 **Containers: Edit** (and the account has Containers, on Workers Paid).
3212. **Registry logins.**
322 `POST /accounts/{id}/containers/registries/registry.cloudflare.com/credentials`
323 with `{ "permissions": ["push"], "expiration_minutes": 15 }` answers
324 inside the API's usual `{ success, result }` with a `username` and
325 `password`, and `docker push` succeeds with that login alone, with no
326 `pull` (Docker checks for layers before it uploads them). If it needs
327 `pull`, do not add it: a pull login reads every app's image. Find
328 another way first, such as Cloudflare scoping logins to a repository.
3293. **The registry's hosts.** A push from a guarded sandbox reaches only
330 `registry.cloudflare.com` (on the deploy builds' list in
331 `services/runner/src/egress.ts`). If its layers upload to another host,
332 add that host there.
3334. **Checking a digest.** g1t asks
334 `HEAD https://registry.cloudflare.com/v2/<account>/g1t-<app>/manifests/sha256:…`
335 with a pull login of its own, following a `WWW-Authenticate: Bearer`
336 challenge. It answers 200 for an image that was pushed.
3375. **The app's Worker.** A script uploaded to the dispatch namespace with
338 `exports: { "G1tApp": { "type": "durable-object", "storage": "sqlite" } }`
339 and `containers: [{ "class_name": "G1tApp" }]` in its metadata is taken,
340 and taken again on the next deploy with nothing changed about the class.
3416. **Its Durable Object namespace.**
342 `GET /accounts/{id}/workers/durable_objects/namespaces` lists it with
343 `class` `G1tApp`, `script` the app's name and `dispatch_namespace`
344 `g1t-deployments`.
3457. **The Container application.** `POST /accounts/{id}/containers/applications`
346 with `scheduling_policy` `default`, the image by digest, `instance_type`,
347 `max_instances` 1 and `durable_objects.namespace_id` creates it; a
348 rollout (`…/rollouts`, `strategy` `rolling`, `step_percentage` 100,
349 `kind` `full_auto`) moves it to a new image; `DELETE` removes it.
3508. **Starting.** `ctx.container.start({ env, enableInternet: true })`
351 works under the default policy from a Worker in a dispatch namespace,
352 the container gets its environment, and `getTcpPort(port).fetch()`
353 reaches it; the dispatcher's limits (50 ms of CPU, 50 subrequests) leave
354 room for a page that waits on a container starting.
3559. **Sleeping and billing.** Five minutes after the last request the
356 container stops (the app's object stops it itself), Cloudflare's billable
357 usage for Containers stops growing, and `container_meters` in the
358 deployments database has the minutes the app's Worker reported, which
359 should be at least what Cloudflare billed for the same time.
36010. **Pausing and taking down.** A workspace over its limit gets its
361 container app's Worker put back paused, which stops the container, and
362 taking an app down deletes its application before its Worker.
363
364Then set `CONTAINERS` to `on`, deploy `services/deployments`, and the
365container build of a project with a `Dockerfile` goes live; the
366[limitations page](/about/limitations/#containers-are-not-switched-on-yet)
367and the caution in the deployments guide come out in the same change.
368
369## Rolling back
370
371- **One unit, at once:** `npx wrangler rollback` in its folder. Revert the
372 commit on `main` too, or the next push brings it back.
373- **To a commit:** check it out and run
374 `node scripts/deploy.mjs deploy --only <units> --rollback`. Without
375 `--rollback` the tool refuses a unit whose live commit is newer.
376- **The runner's image:** redeploy the older commit with
377 `--only runner --rollback`; its image is still in the registry.
378
379## Other one-time setup
380
381### The dead-letter queue
382
383Every queue consumer retries a message a fixed number of times, then puts
384it on `g1t-events-dlq`. Nothing consumes that queue: read it with
385`npx wrangler queues info g1t-events-dlq`, fix the cause, and replay by
386hand.
387
388### OIDC tokens for workflow jobs
389
390The API issues workflow jobs' [OIDC tokens](/guides/actions/), signed with
391an RSA key in its secret `ACTIONS_OIDC_KEY`. Without it, jobs get no
392tokens.
393
394```sh
395openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out oidc.pem
396cd apps/api && npx wrangler secret put ACTIONS_OIDC_KEY < ../../oidc.pem
397rm ../../oidc.pem
398```
399
400To rotate it, store the current key as `ACTIONS_OIDC_KEY_PREVIOUS`, store
401a new one as `ACTIONS_OIDC_KEY`, and delete the previous one a day later.
402For a key that may have leaked, skip keeping the previous one.
403
404### Telling the status page about deploys
405
406Restarts during a deploy can look like an outage to the status page's
407checks. Set the same value as the status Worker's secret
408`STATUS_DEPLOY_TOKEN` and as the repository's Actions secret
409`STATUS_DEPLOY_TOKEN`, and add the status page's host to the workflow-only
410domains. Deploys then announce their start and finish, and the status page
411drafts no incident during them or for 3 minutes after.
412
413### Browser push
414
415`node scripts/ops/vapid-keys.mjs` prints a VAPID key pair. Put the public
416half in `VAPID_PUBLIC_KEY` in `services/notify/wrangler.jsonc` and the
417private half in the secret `VAPID_PRIVATE_KEY`.
418
419Pushes to the phone app go through Expo's push service and need nothing
420set. If the Expo project the app is built from turns on **enhanced push
421security**, put an Expo access token in the notify Worker's secret
422`EXPO_ACCESS_TOKEN`:
423
424```sh
425cd services/notify && npx wrangler secret put EXPO_ACCESS_TOKEN
426```
427
428For g1t.sh, leave enhanced push security off: every self-hosted g1t
429pushes to the same app, through the same Expo project, without a token of
430g1t's. Turning it on stops notifications to phones from every
431installation but g1t.sh.
432
433To send phones nothing at all, set `PHONE_PUSH` to `off` in the `vars` of
434both `services/notify/wrangler.jsonc` and `apps/web/wrangler.jsonc`: the
435notify Worker sends them nothing, and the site's
436`/.well-known/g1t.json` tells the app so.
437
438## Rate limits
439
440Every public surface that costs money or sends something is behind a
441[Workers Rate Limiting](https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/)
442binding, with the limits on the [rate limits](/reference/rate-limits/)
443page. The binding's `namespace_id` must be unique in your account. A
444missing binding, or one that fails, lets requests through.
445
446## Spend guardrails
447
448Cloudflare has no hard spending cap. Besides each workspace's own
449[limits](/guides/usage-and-billing/), g1t watches the platform as a whole:
450
451- **The hourly watch.** At a quarter past each hour, billing reads the
452 hour before from Cloudflare's GraphQL Analytics API: Workers requests
453 and CPU, D1 rows, Queue operations, Durable Objects, KV and Artifacts.
454 It needs `CLOUDFLARE_BILLING_TOKEN` (or `CLOUDFLARE_USAGE_TOKEN`) with
455 Account Analytics Read. An hour over its threshold, or over ten times
456 the week's median hour, is emailed to `COSTS_ALERT_EMAIL`.
457- **Thresholds** are the `PLATFORM_HOURLY_*` variables in
458 `services/billing/wrangler.jsonc`; `0` turns one off. A breach at five
459 times its threshold pauses the kinds of work in `AUTO_PAUSE`.
460- **The platform pause** stops a kind of work everywhere: `compute`
461 (sandboxes), `schedules`, `indexing` or `renders` (social cards). Staff
462 pause and resume in sudo, under **Costs & margin**. Work already
463 running finishes, and if the pause cannot be read, nothing is paused.
464- **Check the watch's queries** after changing them:
465 `CLOUDFLARE_API_TOKEN=<token> node scripts/ops/platform-usage.mjs`. It
466 exits 1 and names any dataset that errored or answered empty.
467
468In Cloudflare's dashboard, which no code can set, add **Billable Usage**
469notifications for each product g1t uses, a monthly **Budget alert**, and a
470notification destination that reaches someone.
471
472## Logs
473
474Each Worker keeps a share of its invocations' logs, set by
475`head_sampling_rate` in its `wrangler.jsonc`: every one for billing,
476identity, runner, actions, deployments, status and sudo, and a tenth for
477the rest. Metrics are not sampled. To chase a rare error on a sampled
478Worker, raise its rate to 1 while you look.