Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/legal-bags-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

fix `deploy` command not taking into account environment variables
4 changes: 3 additions & 1 deletion packages/cloudflare/src/cli/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize: num

const wranglerConfig = readWranglerConfig(args);

const envVars = await getEnvFromPlatformProxy({
const platformProxyEnvVars = await getEnvFromPlatformProxy({
configPath: args.configPath,
environment: args.env,
});

const envVars = { ...platformProxyEnvVars, ...process.env };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upload.ts should be updated as well.

May be we could add a includeProcessEnv: Boolean to getEnvFromPlatformProxy so that we could document the purpose on the JSDoc of that functon.

I'm wondering if it should { ...platformProxyEnvVars, ...process.env } or { ...process.env, ...platformProxyEnvVars} - I would tend to prefer the latest as proxy vars are more specific as they could change with the environment but I don't have a very strong opinion on that. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm wondering if we should rather update

if (!envVars.CF_WORKER_NAME) {
logger.error("CF_WORKER_NAME should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_PREVIEW_DOMAIN) {
logger.error("CF_PREVIEW_DOMAIN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_WORKERS_SCRIPTS_API_TOKEN) {
logger.error("CF_WORKERS_SCRIPTS_API_TOKEN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_ACCOUNT_ID) {
logger.error("CF_ACCOUNT_ID should be set when skew protection is enabled");
process.exit(1);
}

to read from either process.env of envVars.

That would be a more targeted change.

What are your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upload.ts should be updated as well.

Updated 👍

May be we could add a includeProcessEnv: Boolean to getEnvFromPlatformProxy so that we could document the purpose on the JSDoc of that functon.

What about renaming the function getEnvironmentVariables and have it always include values from both platformProxy and process.env? would there be an issue with that?

I'm wondering if it should { ...platformProxyEnvVars, ...process.env } or { ...process.env, ...platformProxyEnvVars} - I would tend to prefer the latest as proxy vars are more specific as they could change with the environment but I don't have a very strong opinion on that. What do you think?

I purposely put ...process.env as last since I think system environment variables should have precedence over those in the config files, for example if I run something line:

$ CF_ACCOUNT_ID="123" npm run deploy

I would personally expect that "123" would override a potential CF_ACCOUNT_ID value present in the config file.

I do think that this is the common behavior for this sort of things. Having the system environment variables also adds more flexibility to users, that could for example very easily deploy their worker onto different accounts (silly example):

$ CF_ACCOUNT_ID="123" npm run deploy
$ CF_ACCOUNT_ID="456" npm run deploy 

What would the benefits in having the platformProxy have precedence over process.env be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm wondering if we should rather update

if (!envVars.CF_WORKER_NAME) {
logger.error("CF_WORKER_NAME should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_PREVIEW_DOMAIN) {
logger.error("CF_PREVIEW_DOMAIN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_WORKERS_SCRIPTS_API_TOKEN) {
logger.error("CF_WORKERS_SCRIPTS_API_TOKEN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_ACCOUNT_ID) {
logger.error("CF_ACCOUNT_ID should be set when skew protection is enabled");
process.exit(1);
}

to read from either process.env of envVars.

That would be a more targeted change.

What are your thoughts?

I think that generally it would make more sense to get the environment variables from both getPlatformProxy and process.env, actually generally process.env makes more sense to me if we're talking about build time variables, since the variables in getPlatformProxy are, in my opinion, runtime variables.

But if you prefer I'll update the code to limit the change to the skew protection logic.

Copy link
Contributor

@vicb vicb Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about renaming the function getEnvironmentVariables and have it always include values from both platformProxy and process.env? would there be an issue with that?

From the discussion, I think getEnvFromPlatformProxy and have it returns only the the env. This is the original use case and what we usually want - i.e. when we access the cache at build time, we want to use the config that will be used at runtime.

So I think what we want is to pass the getEnvFromPlatformProxy() and getDeployementMapping() (i.e. no change from what we do today).

Inside this function you could add env = {...envVars, ...process.env}; with a comment that process.env has priority for those variables (agreed that process.env should have priority)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, hopefully this is what you had in mind: 945f849


const deploymentMapping = await getDeploymentMapping(options, config, envVars);

await populateCache(options, config, wranglerConfig, {
Expand Down