CI / Secrets and trust
Deploy to Cloudflare
Your deploy job runs Wrangler, and Wrangler needs a Cloudflare API token that no script in the job can be allowed to read. The forge keeps the token, and the passwords that Wrangler gets with it, outside the job container.
5 min read
To skip the token, connect the environment to Cloudflare on the web, and the forge refreshes the token itself: see Connect Cloudflare. When an environment has both, the connection wins: its jobs get the token of the connection, never the manual value. The manual value works again after a Disconnect.
You need admin access to the repository, and a branch rule that requires a pull request on the branch that you deploy from. See Branch rules.
Wrangler exchanges the API token for a temporary password of the private registry, and for an upload token when the Worker has new static assets. The forge keeps these values outside the job container:
- The API token and the registry password: Wrangler and Docker receive placeholders. The outbound handler uses short-lived registry credentials only for requests to the registry of Cloudflare.
- The asset upload token: Wrangler receives it with its signature encrypted, with a key that only the forge has. The outbound handler puts the real token back only on the asset uploads of that job to
api.cloudflare.com.
1. Save the API token in production
Save CLOUDFLARE_API_TOKEN with a value in production only. Bind it to both api.cloudflare.com and registry.cloudflare.com, in the Authorization header:
src secret set CLOUDFLARE_API_TOKEN --env production --host api.cloudflare.com --host registry.cloudflare.com --header Authorization < token.txt
2. Name production and both hosts on the deploy job
Name environment: 'production' in the deployment job, and allow both hosts in its egress list:
import { defineWorkflow, push } from '@tanstack-source/ci'
export default defineWorkflow('deploy', { on: [push({ branches: ['main'] })] }).job('deploy', {
environment: 'production',
secrets: { CLOUDFLARE_API_TOKEN: {} },
egress: ['api.cloudflare.com', 'registry.cloudflare.com'],
steps: [{ name: 'Deploy', run: 'pnpm wrangler deploy' }],
})
3. Deploy only after the checks
Make the deployment job depend on the checks, and restrict it to protected pushes to the production branch. Require all jobs with one check shows a deploy job that starts after one check of the whole workflow.
4. Run one deploy at a time
Serialize the deployment job:
src ci settings --serial-job deploy/deploy
- Jobs named here use
workflow/job. They run one at a time across the runs of the repository, and keep their slot until they finish. - Other jobs can still run in parallel.
--no-serial-jobsclears the list.

Done
A push to main deploys the Worker with Wrangler. The API token stayed outside the job container, and only one deploy runs at a time.
Sync the secrets of a Worker
Your Worker needs its own runtime secrets, like DATABASE_URL. wrangler secret put needs the values inside the job. So the forge sets them for you: it sends each value of the environment to the Cloudflare API itself, and the job never sees one.
Warning
Naming a new Worker deletes the secret values of a write-only environment like production, also the CLOUDFLARE_API_TOKEN of step 1. Name the Worker first, or enter those values again after it.
Name the Worker of the environment, as
<account id>/<script>:src env set production --worker 0123456789abcdef0123456789abcdef/my-worker. The script is thenameinwrangler.jsonc. On the web, use the Cloudflare Worker field of the environment in the Secrets section of Settings.--worker noneclears it. A Cloudflare connection names the Worker for you, so skip this step when the environment has one.Give the secrets of the Worker a value in the environment, like any secret:
src secret set DATABASE_URL --env production < url.txt. A secret that only the Worker reads needs no host and no header. The sync uses the token of the Cloudflare connection of the environment when it has one. Else theCLOUDFLARE_API_TOKENof the environment (step 1 above) must be bound toapi.cloudflare.comin theAuthorizationheader, and must be allowed to set Worker secrets.Add the
syncWorkerSecrets()step to the deployment job, and declare each secret that it syncs in thesecretsof the job:import { defineWorkflow, push, syncWorkerSecrets } from '@tanstack-source/ci' export default defineWorkflow('deploy', { on: [push({ branches: ['main'] })] }).job('deploy', { environment: 'production', secrets: { CLOUDFLARE_API_TOKEN: {}, DATABASE_URL: {} }, egress: ['api.cloudflare.com', 'registry.cloudflare.com'], steps: [{ name: 'Deploy', run: 'pnpm wrangler deploy' }, syncWorkerSecrets()], })syncWorkerSecrets({ names: ['DATABASE_URL'] })syncs only the names that you give. Each name must be one that the job declares.
What the forge does:
- It syncs only for a job of a protected run that is not a pull request run, in the environment that the job declares. It checks the rules of the environment again, with its settings at that time.
- It puts each secret that the job declares and the environment has on the Worker (
PUT /accounts/<account id>/workers/scripts/<script>/secrets), exceptCLOUDFLARE_API_TOKEN, which it never syncs. A secret that the job does not declare stays off the Worker. - The log of the step names the secrets that it synced. No value is in the answer, the log or an error. If Cloudflare refuses a secret, the step fails with the error codes of Cloudflare, never its message, which can repeat a value. The secrets before it stay synced.
- A local run (
src ci run) syncs nothing. It prints the secrets that the step syncs on the forge.
Build images with Docker
- Public Docker Hub pulls also authenticate outside the container, with anonymous pull access to the requested image only. They never forward credentials that the job supplies.
- Allow
registry-1.docker.io,auth.docker.ioand the layer download hosts that your images use. - Docker builds in a job container need host networking, and a rootful daemon with iptables and IP forwarding turned off.
- Pass the job CA as a BuildKit secret to networked build steps, so it never becomes part of an image.