Preview Builds don't have access to environment variables and secrets
Hi folks. I'm running into a strange issue where my preview builds don't have access to environment variables and build secrets that I set up on the dashboard.
As a result of this all preview builds are failing.
22 Replies
i am having the same issue as well
I am getting
even though I have STRIPE_SECRET_KEY set in Variables and Secrets and access it via env.STRIPE_SECRET_KEY in my routes.ts file
I am on NextJs 14.2.32
@Walshy | DevPlat EE wondering if you have any ideas as to why this is happening? Preview build fail because they can't access env vars



yep pretty strange issue. env vars are not passed to non-production branch and PR previews for some reason. I even tried to log process.env from my vite config. And the diff shows that env vars are not passed to preview builds

this makes prev/non-prod builds pretty useless, and now we have to devise a new CI solution on github, decoupled from CF 🙁
This is common confusion
In Workers Builds there's env vars AND build vars
You'll want to setup build vars for the things you need in CI and use env vars for things you need on edge
It's further down in the settings UI
Unless you actually want env vars? Not super clear to me - if you don't and they aren't there, send me the build ID and I can take a look
?workers-build-id
The Workers build ID is a unique build identifier.
It's the UUID in the browser bar (for example, a URL would be
dash.cloudflare.com/ACCOUNT_ID/workers/services/view/WORKER/production/builds/BUILD_ID where the build ID looks something like a398d794-7322-4c97-96d9-40b5140a8d9b).
This ID can help troubleshoot some issues with Workers builds so if you have a failing build make sure you grab that ID for the team to use.@Walshy | DevPlat EE I need to have access to my "Variables and Secrets" which I do need for CI (e.g. Sentry or Posthog). These are accessible to my "tracked branch" builds, but not available for preview urls (PRs or non-prod branches). I can see that CF selectively passes build vars to preview builds. For example here
SKIP_DEPENDENCY_INSTALL is passed but not PNPM_VERSION or CLOUDLFARE_ENV
here are some build ids:
- f9515032-edef-4038-9081-d38bb7b05824
- d284dc5f-b89d-4cea-96d7-3b89254b13da
- 759d814a-14b4-4f31-b91b-59b5e4ae997e
- 94c95a08-5bae-4359-ba30-4c732c401d9e
again just to be super clear, this is what i mean by build vars

also agree with this but the dashboard is inconsistent. Build details always refers to build vars as
Environment variablesThese are env vars though not build vars
So expected they aren't in builds
so why are they here?

but not here

also these are build vars, you're confused

@Walshy | DevPlat EE regardless of the syntax/semantics, there is an inconsistency here. Which is the variables available during deployments builds are NOT available during PR previews and non-production branch builds. Because of this all of our branch/pr previews are failing.
@Walshy | DevPlat EE @Salam I figured out the issue and this is a bug on CF for sure.
So when you enable PR previews, for whatever reason your build vars go bad. I realized this because i couldn't even delete them, even though they were showing on the dashboard. The error said can't find var with the specified key.
The way to fix this was to edit the var, add a random character at the end, and edit it again and delete the said character to go back to the original var value. Once I did this I was able to delete the build variables again and they also started being added to the build process.

Yeah sorry, the naming is weird here... i thought we called these build vars explicitly but we changed that :notlikemeow:
I know I posted a suggestion to rename these to runtime vars and build vars
cc: @Will Taylor | Workers who probably has more context on the preview vars
Thanks @hiradsab, will file a ticket internally to track and fix!
We have a fix merged, but we're in a release freeze atm. Should be resolved next week for ya! 🤞
@Will Taylor | Workers amazing stuff and looking forward to it! Also have ran into another massive bug with preview builds and this time env vars that I think is worth checking out and relating directly to the dashboard
https://github.com/cloudflare/workers-sdk/issues/8871#issuecomment-3340611729
GitHub
Prevent secrets/env vars from being deleted when deploying via GitH...
Which Cloudflare product(s) does this pertain to? Workers Runtime What versions & operating system are you using? No local wrangler Please provide a link to a minimal reproduction No response D...
Thanks @hiradsab , I’m out for the weekend, but I’ll file an internal ticket to get it looked at next week!
missing build_vars option from wrangler.jsonc is bad dx when creating a new worker initial build fails/is useless