workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

wrangler versions upload --preview-alias rejects raw WORKERS_CI_BRANCH values containing slashes in Workers Builds

Open mynameistito opened this issue 5 months ago • 2 comments

What versions & operating system are you using?

System: OS: Windows 11 10.0.26200 CPU: (16) x64 AMD Ryzen 7 7700 8-Core Processor Memory: 16.09 GB / 31.11 GB Binaries: Node: 22.22.3 - C:\nvm4w\nodejs\node.EXE npm: 11.17.0 - C:\Users\mynameistito\node_modules.bin\npm.EXE pnpm: 11.3.0 - C:\Users\mynameistito.local\share\pnpm\bin\pnpm.CMD bun: 1.3.14 - C:\Users\mynameistito.bun\bin\bun.EXE Deno: 2.8.3 - C:\Users\mynameistito\node_modules.bin\deno.EXE npmPackages: @cloudflare/vite-plugin: ^1.41.0 => 1.41.0 wrangler: ^4.101.0 => 4.101.0

Please provide a link to a minimal reproduction

https://github.com/mynameistito/workers-preview-alias-repro

Describe the Bug

Workers Builds provides WORKERS_CI_BRANCH, but using it directly as a preview alias can fail for normal branch names that contain /. Repro: https://github.com/mynameistito/workers-preview-alias-repro/pull/1 Steps:

  1. Set up a Worker with Workers Builds.
  2. Enable non-production branch builds.
  3. Use a branch like: chore/simplify-wrangler-deploy
  4. Set the non-production deploy command to: bunx wrangler versions upload --preview-alias "${WORKERS_CI_BRANCH}"
  5. Run the build. The deploy fails because WORKERS_CI_BRANCH is passed through as: chore/simplify-wrangler-deploy Wrangler rejects that alias: validation failures: invalid alias: chore/simplify-wrangler-deploy [code: 10021] I expected this to either work out of the box, or for Workers Builds to provide a sanitized branch alias that can be passed to --preview-alias. Current workaround is to sanitize the branch manually before calling Wrangler: ALIAS="$(printf '%s' "$WORKERS_CI_BRANCH" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_-]+/-/g; s/-+/-/g; s/^-+//; s/-+$//' | cut -c1-63)" && bunx wrangler versions upload --preview-alias "${ALIAS:-preview}" Tested with Wrangler 4.101.0.

Please provide any relevant error logs

No response

mynameistito avatar Jun 17 '26 23:06 mynameistito

I'd like to fix this. The rejection is server-side (code: 10021) — --preview-alias is passed straight to the API without client-side validation, so users get a cryptic error instead of guidance (aliases must be lowercase alphanumeric + -/_, ≤63 chars).

Two directions, happy to PR whichever you prefer:

  1. Validate client-side with an actionable error — fail fast in wrangler with a message stating the allowed format and a suggested sanitized alias (e.g. chore/simplify-wrangler-deploy → chore-simplify-wrangler-deploy). No behavior change; just turns 10021 into something legible.
  2. Auto-sanitize the alias (optionally with a warning) — coerce to the valid charset so --preview-alias "$WORKERS_CI_BRANCH" works out of the box, matching the reporter's expectation. This silently changes user input, so it's more of a product decision.

My lean is (1). Which would you accept? Happy to open a PR with tests + a changeset.

thatssoheil avatar Jun 23 '26 22:06 thatssoheil

Thanks for the detailed report. Wrangler already sanitizes the branch name for you: set the build variable WRANGLER_CI_GENERATE_PREVIEW_ALIAS=true and run wrangler versions upload without --preview-alias. It derives the alias from WORKERS_CI_BRANCH, so chore/simplify-wrangler-deploy becomes chore-simplify-wrangler-deploy, lowercased and truncated to fit. --preview-alias is passed through as-is, which is why the raw value is rejected. If that doesn't work for you, reply here and we'll take another look.

workers-devprod avatar Nov 12 '26 10:11 workers-devprod