In Cloudflare Workers, how do you roll a high-traffic API Worker's new version out to 5%, then 50%, then 100% from CI?
answer
- upload first, release second
- two versions, percentages total 100
- version-id@percent shorthand
- version metadata binding for attribution
basics
~10 sUpload with wrangler versions upload, then run wrangler versions deploy NEW@5% STABLE@95%, then 50/50, then NEW@100%, authenticating CI with CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID and comparing versions through the version metadata binding.
solid answer
~40 sCI authenticates with `CLOUDFLARE_API_TOKEN` and `CLOUDFLARE_ACCOUNT_ID` from its secret store. It runs `wrangler versions upload --tag $GIT_SHA`, which creates a version with no traffic and prints its ID. Optionally it deploys `NEW@0% STABLE@100%` and smoke-tests with the `Cloudflare-Workers-Version-Overrides` header. Then `wrangler versions deploy NEW@5% STABLE@95% -y`, then 50/50, then `NEW@100%`; percentages must total exactly 100 and a deployment holds at most two versions. The `version_metadata` binding exposes `env.CF_VERSION_METADATA.id` so logs and metrics can be split by version. For secrets, use `wrangler versions secret put` rather than `wrangler secret put`, and never let another job run `wrangler deploy`, which sends a new upload to 100%.
code
bash · 8 lines# CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID come from the CI secret store
npx wrangler versions upload --tag "$GIT_SHA" --message "orders-api: new pricing rules"
# STABLE_SHA is the tag of the version serving 100% today
npx wrangler versions deploy --version-tag "$GIT_SHA@5%" --version-tag "$STABLE_SHA@95%" -y --message "canary 5%"
# ...error rate and latency per version hold...
npx wrangler versions deploy --version-tag "$GIT_SHA@50%" --version-tag "$STABLE_SHA@50%" -y --message "canary 50%"
npx wrangler versions deploy --version-tag "$GIT_SHA@100%" -y --message "promote"go deeper
Know the two commands, wrangler versions upload then wrangler versions deploy, and that the percentages across the two versions must add up to 100.
Walk through the 0% smoke test with the override header, the 5/50/100 steps and how the version metadata binding lets metrics be split by version.
Name the traps: a stray wrangler deploy in CI that promotes to 100%, wrangler secret put being refused after an unreleased upload, and tag resolution failing when a tag is reused.
Talk about how long each step should hold for this traffic volume, which signals gate promotion, and how the token used by CI is scoped.
## The goal and the building blocks The scenario: a high-traffic API Worker, `orders-api`, has a risky change. The team wants it on **5%** of traffic, then **50%**, then **100%**, all driven from CI, with the option to stop at any point. Cloudflare Workers supports this with **gradual deployments**: a deployment that splits traffic between exactly two versions by percentage. The commands involved: - `wrangler versions upload` creates a version that receives no traffic. - `wrangler versions deploy` creates a deployment from one or two versions with percentages. - `wrangler deployments status` reports what is live, with `--json` for scripts. - `wrangler rollback` returns a chosen version to 100% if something goes wrong. ## Authenticating the pipeline A CI runner cannot complete the interactive `wrangler login` flow. Wrangler instead reads two environment variables: - `CLOUDFLARE_API_TOKEN`: an API token, typically created from the **Edit Cloudflare Workers** template and scoped to only the account (and zones) this Worker uses. - `CLOUDFLARE_ACCOUNT_ID`: the account to deploy into. Both belong in the CI system's secret store, never in the repository, because the token can deploy any Worker it is scoped to. ## Step by step 1. **Upload.** `wrangler versions upload --tag "$GIT_SHA" --message "..."` creates the version and prints its **Worker Version ID**. Tagging with the commit SHA lets later steps refer to it by tag. 2. **Optional smoke test at 0%.** Deploy `NEW@0% STABLE@100%`, then send test requests carrying the `Cloudflare-Workers-Version-Overrides` header (for example `orders-api="<new version id>"`). Overrides only apply to a version that is in the current deployment, which is why it is added at 0% first; real users still get the stable version. 3. **Canary.** `wrangler versions deploy NEW@5% STABLE@95% -y` sends roughly 5% of requests to the new version. 4. **Widen.** After the metrics hold, `wrangler versions deploy NEW@50% STABLE@50% -y`. 5. **Finish.** `wrangler versions deploy NEW@100% -y` leaves a single-version deployment. Each `versions deploy` call takes effect immediately and replaces the previous deployment, so "pausing" a rollout simply means not running the next step. The quickest way to stop it is `wrangler rollback` to the stable version, which puts that version back on 100%. ## Rules Wrangler enforces | Rule | What happens if you break it | |---|---| | Percentages must total exactly 100 | Wrangler errors: the traffic percentages "must total exactly 100%" | | At most two versions per deployment | Wrangler errors that too many versions were selected | | Only recent versions are deployable | A deployment can use only the last 100 uploaded versions | | `--version-tag` must match one version | No match, or more than one, is an error; deploy by version ID instead | Wrangler deploys only the versions you list. Writing `NEW@5%` alone does not keep the other 95% on the old version; it fails the 100% check. A version listed without a percentage shares whatever remains. ## Telling the two versions apart in metrics A split is only useful if you can compare the versions. Add the **version metadata binding** to the Wrangler file, and every invocation can read `env.CF_VERSION_METADATA.id`, `.tag` and `.timestamp`. Put the ID in structured logs, in an `x-worker-version` response header or in analytics events, and the error rate and latency of each version can be compared directly. Workers Logpush can also add a `ScriptVersion` field to each event. The binding is still marked beta in the docs. ## Two traps mid-rollout - **Another pipeline runs `wrangler deploy`.** Wrangler warns "Your last deployment has multiple versions. To progress that deployment use "wrangler versions deploy" instead." and asks to confirm. In a non-interactive CI job the prompt takes its default of yes, so the freshly uploaded version goes straight to 100% and the canary is bypassed. - **Someone rotates a secret with `wrangler secret put`.** The `wrangler secret` commands edit the deployed Worker, so when the latest uploaded version is not deployed (for example right after step 1) the command fails with "Secret edit failed. You attempted to modify a secret, but the latest version of your Worker isn't currently deployed." For Workers that use gradual deployments the supported route is `wrangler versions secret put`, which creates a new version without deploying it. Both traps point to the same habit: once a Worker uses gradual deployments, every pipeline and runbook that touches it should use the `versions` family of commands.
- What happens if a second pipeline runs wrangler deploy while the 5% split is live?Wrangler warns that the last deployment has multiple versions and that `wrangler versions deploy` should be used to progress it, then asks for confirmation. In a non-interactive CI job that prompt takes its default of yes, so the new upload is deployed to 100% and the canary is skipped.
- Why can wrangler secret put fail right after wrangler versions upload, and what should a rollout use instead?The `wrangler secret` commands edit the currently deployed Worker. When the latest uploaded version is not deployed, the API refuses with "Secret edit failed" rather than risk an accidental deployment. `wrangler versions secret put` creates a new version with the secret, which you then release with `wrangler versions deploy`.
- How does the pipeline find the version that is stable today?`wrangler deployments status --json` reports the current deployment and its version IDs. Alternatively, if every release is uploaded with `--tag` set to its commit SHA, the pipeline can refer to both versions with `--version-tag`, which errors when a tag matches none or more than one recent version.
- Passing only NEW@5% leaves the remaining 95% on the current version.
- A gradual deployment can run three versions: stable, canary and a smoke test.
- Running wrangler deploy mid-rollout adds the new code to the existing split.
- wrangler secret put updates the secret on both versions of a live split in place.
- Version overrides can target any uploaded version, even one not in the deployment.
Assumes Wrangler 4.149.0; deploying by --version-tag arrived in Wrangler 4.99.0, so older pipelines must pass version IDs.