Skip to content
Source
Sign inSign up

Connect Cloudflare

CI / Secrets and trust

Connect Cloudflare

Your deploy job runs wrangler deploy, and Wrangler needs a Cloudflare API token. A token that you make by hand needs the right permissions, a paste into a CI secret, and a new paste when it leaks or someone leaves.

5 min read

Connect the CI environment to Cloudflare instead. You sign in to Cloudflare once and pick the Worker. Then the protected deploy jobs of that environment get CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID, and Source refreshes the token itself.

You need:

  • Admin access to the repository.
  • A Worker that exists in your Cloudflare account. Source lists the Workers of the account, so deploy a new Worker once from your machine first.
  • A branch rule that requires a pull request on the branch that you deploy from: src ci rule set main --require-pull. Production takes no run without it.

1. Connect the environment

  1. Open Settings, then Secrets under Code (/@<owner>/<repo>/settings/secrets).
  2. Pick the environment next to Secrets, for example production.
  3. In the Cloudflare card, click Connect Cloudflare.
  4. On Cloudflare's screen, pick the account, then allow the access.
The Cloudflare card of the production environment, with the Connect Cloudflare button

Cloudflare sends you back to the Secrets page of the environment.

Source always asks Cloudflare for Account Settings Read, User Details Read, Workers Scripts Write, and offline access, so that it can refresh the token. It also asks for these, and you can untick the ones that your deploy does not use:

  • Workers Containers Write
  • D1 Write and Queues Write
  • Workers KV Storage Write and Workers R2 Storage Write
  • Workers Routes Write

If the page has no Cloudflare card, this Source server has no Cloudflare client. Save a token by hand: see Deploy to Cloudflare with a manual token.

2. Pick the account and the Worker

  1. If the connection reaches more than one account, pick the Cloudflare account.
  2. Pick the Worker, then click Use this Worker.
  3. If Source lists values of the environment that will be deleted, read the list. Then click Delete the values and use this Worker, or Back.
The Cloudflare card with an account and the Worker acme-app picked, and the Use this Worker button

A write-only environment, like production, keeps its values only under the rules that they were set under. A Worker that the environment did not name before changes those rules, so its values go. Enter them again after. See CI environments.

The connected Cloudflare card: the account, the Worker acme-app, who connected it, and the Change Worker and Disconnect buttons

The card now shows the account, the Worker, and who connected it and when. The connection also sets the Worker of the environment, so the syncWorkerSecrets() step uses it: see Sync the secrets of a Worker.

3. Write the deploy job

Name the environment, declare the token and the account id, and allow both Cloudflare hosts. For example, .source/workflows/deploy.ts:

import { defineWorkflow, push, setup } from '@tanstack-source/ci'

export default defineWorkflow('deploy', { on: [push({ branches: ['main'] })] }).job('deploy', {
  environment: 'production',
  secrets: { CLOUDFLARE_API_TOKEN: {} },
  vars: ['CLOUDFLARE_ACCOUNT_ID'],
  egress: ['registry.npmjs.org', 'api.cloudflare.com', 'registry.cloudflare.com'],
  steps: [
    setup.pnpm(),
    { name: 'Install', run: 'pnpm install --frozen-lockfile' },
    { name: 'Deploy', run: 'pnpm wrangler deploy' },
  ],
})
  • environment: the connected environment. A run that does not pass its rules skips the job, and the run page says why.
  • secrets and vars: the connection gives both values. You save neither of them.
  • egress: Wrangler sends the token to api.cloudflare.com, and pushes container images to registry.cloudflare.com.

A job that deploys with a manual CLOUDFLARE_API_TOKEN already has this shape. It needs no change.

4. Merge and watch the deploy

Merge a pull request into main. The push run starts the deploy job. Follow it with src run list and src run view <id> --log-failed.

What works now

  • Only protected runs that are not pull requests get the token of the connection. Every other run gets no CLOUDFLARE_API_TOKEN value, a pull request run in preview too.
  • The token goes only in the Authorization header, to api.cloudflare.com and registry.cloudflare.com. The job container sees a placeholder, never the token.
  • Source refreshes the token before it expires. Nobody pastes a new one.
  • When the environment also has a manual CLOUDFLARE_API_TOKEN value, the connection wins. The card shows the manual value as replaced, and it works again after a Disconnect.
  • src env cloudflare production shows the connection: its status, account, Worker, and who connected it and when.

When the connection changes

It needs a new sign-in

Cloudflare can refuse a refresh, or the account can leave the connection. Then the card shows a warning, and the deploy jobs of the environment fail with:

The Cloudflare connection needs a new sign-in. A repo admin can reconnect it in Settings > Secrets.

Click Reconnect, sign in to Cloudflare again, and pick the Worker again.

You deploy another Worker

Click Change Worker and pick the other one. You do not sign in again. In a write-only environment, Source lists the values that the new Worker deletes before you confirm.

You disconnect

Click Disconnect, then Disconnect Cloudflare. Or run src env cloudflare production --disconnect.

  • Source deletes the connection and revokes its token at Cloudflare.
  • If Cloudflare does not confirm the revoke, Source tells you. Then revoke the access of TanStack Source in the Cloudflare dashboard.
  • Running deploys are not cancelled, but they can fail.
  • A manual CLOUDFLARE_API_TOKEN value of the environment works again.

A new Connect Cloudflare replaces the old connection and revokes its token. Deleting the environment, or loosening a write-only environment, deletes its connection too.