Support CLOUDFLARE_ENV environment variable for selecting the active environment#11228
Conversation
🦋 Changeset detectedLatest commit: f482fc4 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
- Add CLOUDFLARE_ENV to system environment variables documentation - Update environments documentation to mention CLOUDFLARE_ENV usage - Add changelog entry for the new feature Related to cloudflare/workers-sdk#11228 Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com>
04c0b77 to
b7c64fc
Compare
|
I think these test failures are legit, as they are related to enviornments |
|
Yeah sorry @ascorbic - I am working on it. I meant to set this back to "draft". |
f7ca3ef to
dfce7e7
Compare
| setLogLevel(envLogLevel as LoggerLevel); | ||
| } | ||
| // Also set the CLI package log level to match | ||
| setLogLevel(logger.loggerLevel); |
There was a problem hiding this comment.
The loggerLevel property is actually a getter that computes the level taking into account the current ALS.
jamesopstad
left a comment
There was a problem hiding this comment.
As discussed in chat, if CLOUDFLARE_ENV is also read via dotenv in Wrangler then I am less convinced this is a good idea. Imagine the following example:
.env
CLOUDFLARE_ENV=development.env.production
CLOUDFLARE_ENV=productionThe build is run using vite build --mode production, which reads .env.production. Running wrangler deploy would then read .env and so the selected CLOUDFLARE_ENV value would not match the build.
Dropping our offline conversation in here for transparency... Looking at our docs, we do not recommend that the user puts Instead we only give examples of putting it in If there is not a We could update the docs to explicitly call out not putting |
7b99508 to
44af2c2
Compare
fa8151a to
b272db3
Compare
345ab79 to
194775d
Compare
The startup-profiling test was leaking the logger level between tests, so that got fixed in here too.
…e environment This change enables users to select the environment for commands such as `CLOUDFLARE_ENV=prod wrangler versions upload`. The `--env` command line argument takes precedence. The `CLOUDFLARE_ENV` enviroment variable is mostly used with the `@cloudflare/vite-plugin` to select the environment for building the Worker to be deployed. This build also generates a "redirected deploy config" that is flattened to only contain the active environment. To avoid accidentally deploying a version that is built for one environment to a different environment, there is an additional check to ensure that if the user specifies an environment in Wrangler it matches the original selected environment from the build.
194775d to
9014b9c
Compare
| /You have specified the environment ".*?", but are using a redirected configuration/ | ||
| ]); | ||
| } catch (e) { | ||
| error = e as string; |
There was a problem hiding this comment.
isn't e already an any?
| error = e as string; | |
| error = e; |
There was a problem hiding this comment.
You're right! For some reason I always assume that the thing being caught is types as unknown, not any.
|
|
||
| let error = ""; | ||
| try { | ||
| const { getOutput } = await runWranglerDev(basePath, [ |
There was a problem hiding this comment.
| const { getOutput } = await runWranglerDev(basePath, [ | |
| await runWranglerDev(basePath, [ |
getOutput is not being used anywhere, is it?
* Document CLOUDFLARE_ENV environment variable for Wrangler - Add CLOUDFLARE_ENV to system environment variables documentation - Update environments documentation to mention CLOUDFLARE_ENV usage - Add changelog entry for the new feature Related to cloudflare/workers-sdk#11228 Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> * Update changelog to combine build and deploy commands with & Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> * Add link to generated wrangler configuration docs in changelog Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
* Document CLOUDFLARE_ENV environment variable for Wrangler - Add CLOUDFLARE_ENV to system environment variables documentation - Update environments documentation to mention CLOUDFLARE_ENV usage - Add changelog entry for the new feature Related to cloudflare/workers-sdk#11228 Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> * Update changelog to combine build and deploy commands with & Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> * Add link to generated wrangler configuration docs in changelog Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Folks, sorry to resurrect a merged PR, but this cost me a couple days of debugging and I think it is worth addressing because it is an easy footgun. I have a project with multiple dotenv files ( What happens in my setup:
This results in a confusing warning like: ▲ [WARNING] Processing build/server/wrangler.json configuration: I see the docs recommend setting Could you please consider one of the following?
Happy to open a new issue if that is preferred. Thanks! |
Fixes #10928
Replaces #10940 (thanks @eduardo-vargas).
This change enables users to select the environment for commands such as
CLOUDFLARE_ENV=prod wrangler versions upload. The--envcommand line argument takes precedence.The
CLOUDFLARE_ENVenviroment variable is mostly used with the@cloudflare/vite-pluginto select the environment for building the Worker to be deployed. This build also generates a "redirected deploy config" that is flattened to only contain the active environment.To avoid accidentally deploying a version that is built for one environment to a different environment, there is an additional
check to ensure that if the user specifies an environment in Wrangler it matches the original selected environment from the build.
Reviewer Notes