Skip to content

Commit 0633aae

Browse files
committed
fix(cli): sync secret env vars on the worker build path too
1 parent 304d90c commit 0633aae

1 file changed

Lines changed: 47 additions & 15 deletions

File tree

  • packages/cli-v3/src/commands/workers

packages/cli-v3/src/commands/workers/build.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,18 @@ async function _workerBuildCommand(dir: string, options: WorkersBuildCommandOpti
259259
local = true;
260260
}
261261

262-
if (
263-
buildManifest.deploy.sync &&
264-
buildManifest.deploy.sync.env &&
265-
Object.keys(buildManifest.deploy.sync.env).length > 0
266-
) {
267-
const numberOfEnvVars = Object.keys(buildManifest.deploy.sync.env).length;
262+
const childVars = buildManifest.deploy.sync?.env ?? {};
263+
const parentVars = buildManifest.deploy.sync?.parentEnv ?? {};
264+
const secretChildVars = buildManifest.deploy.sync?.secretEnv ?? {};
265+
const secretParentVars = buildManifest.deploy.sync?.secretParentEnv ?? {};
266+
267+
const numberOfEnvVars =
268+
Object.keys(childVars).length +
269+
Object.keys(parentVars).length +
270+
Object.keys(secretChildVars).length +
271+
Object.keys(secretParentVars).length;
272+
273+
if (buildManifest.deploy.sync && numberOfEnvVars > 0) {
268274
const vars = numberOfEnvVars === 1 ? "var" : "vars";
269275

270276
if (!options.skipSyncEnvVars) {
@@ -274,8 +280,10 @@ async function _workerBuildCommand(dir: string, options: WorkersBuildCommandOpti
274280
projectClient.client,
275281
resolvedConfig.project,
276282
options.env,
277-
buildManifest.deploy.sync.env,
278-
buildManifest.deploy.sync.parentEnv
283+
childVars,
284+
parentVars,
285+
secretChildVars,
286+
secretParentVars
279287
);
280288

281289
if (!success) {
@@ -451,15 +459,39 @@ export async function syncEnvVarsWithServer(
451459
projectRef: string,
452460
environmentSlug: string,
453461
envVars: Record<string, string>,
454-
parentEnvVars?: Record<string, string>
462+
parentEnvVars?: Record<string, string>,
463+
secretEnvVars?: Record<string, string>,
464+
secretParentEnvVars?: Record<string, string>
455465
) {
456-
const uploadResult = await apiClient.importEnvVars(projectRef, environmentSlug, {
457-
variables: envVars,
458-
parentVariables: parentEnvVars,
459-
override: true,
460-
});
466+
const hasNonSecret =
467+
Object.keys(envVars).length > 0 || Object.keys(parentEnvVars ?? {}).length > 0;
468+
const hasSecret =
469+
Object.keys(secretEnvVars ?? {}).length > 0 ||
470+
Object.keys(secretParentEnvVars ?? {}).length > 0;
471+
472+
// The import API applies isSecret per call, so secret and non-secret vars go in separate calls.
473+
let success = true;
474+
475+
if (hasNonSecret) {
476+
const result = await apiClient.importEnvVars(projectRef, environmentSlug, {
477+
variables: envVars,
478+
parentVariables: parentEnvVars,
479+
override: true,
480+
});
481+
success = result.success;
482+
}
483+
484+
if (hasSecret && success) {
485+
const result = await apiClient.importEnvVars(projectRef, environmentSlug, {
486+
variables: secretEnvVars ?? {},
487+
parentVariables: secretParentEnvVars,
488+
override: true,
489+
isSecret: true,
490+
});
491+
success = result.success;
492+
}
461493

462-
return uploadResult.success;
494+
return success;
463495
}
464496

465497
async function failDeploy(

0 commit comments

Comments
 (0)