Skip to content

Commit 84d8fb5

Browse files
authored
Fix syncing parent env vars for non-preview deployments (#2131)
* only try to sync parent env vars for preview deployments * add changeset
1 parent 80d449b commit 84d8fb5

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

.changeset/witty-cherries-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
Fix `syncEnvVars` for non-preview deployments

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.import.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
4747
})),
4848
});
4949

50+
// Only sync parent variables if this is a branch environment
5051
if (environment.parentEnvironmentId && body.parentVariables) {
5152
const parentResult = await repository.create(environment.project.id, {
5253
override: typeof body.override === "boolean" ? body.override : false,

packages/cli-v3/src/commands/deploy.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
220220

221221
const branch =
222222
options.env === "preview" ? getBranch({ specified: options.branch, gitMeta }) : undefined;
223+
223224
if (options.env === "preview" && !branch) {
224225
throw new Error(
225226
"Didn't auto-detect preview branch, so you need to specify one. Pass --branch <branch>."
@@ -352,10 +353,9 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
352353
}
353354

354355
const hasVarsToSync =
355-
buildManifest.deploy.sync &&
356-
((buildManifest.deploy.sync.env && Object.keys(buildManifest.deploy.sync.env).length > 0) ||
357-
(buildManifest.deploy.sync.parentEnv &&
358-
Object.keys(buildManifest.deploy.sync.parentEnv).length > 0));
356+
Object.keys(buildManifest.deploy.sync?.env || {}).length > 0 ||
357+
// Only sync parent variables if this is a branch environment
358+
(branch && Object.keys(buildManifest.deploy.sync?.parentEnv || {}).length > 0);
359359

360360
if (hasVarsToSync) {
361361
const childVars = buildManifest.deploy.sync?.env ?? {};
@@ -367,21 +367,22 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
367367
if (!options.skipSyncEnvVars) {
368368
const $spinner = spinner();
369369
$spinner.start(`Syncing ${numberOfEnvVars} env ${vars} with the server`);
370-
const success = await syncEnvVarsWithServer(
370+
371+
const uploadResult = await syncEnvVarsWithServer(
371372
projectClient.client,
372373
resolvedConfig.project,
373374
options.env,
374375
childVars,
375376
parentVars
376377
);
377378

378-
if (!success) {
379+
if (!uploadResult.success) {
379380
await failDeploy(
380381
projectClient.client,
381382
deployment,
382383
{
383384
name: "SyncEnvVarsError",
384-
message: `Failed to sync ${numberOfEnvVars} env ${vars} with the server`,
385+
message: `Failed to sync ${numberOfEnvVars} env ${vars} with the server: ${uploadResult.error}`,
385386
},
386387
"",
387388
$spinner
@@ -626,13 +627,11 @@ export async function syncEnvVarsWithServer(
626627
envVars: Record<string, string>,
627628
parentEnvVars?: Record<string, string>
628629
) {
629-
const uploadResult = await apiClient.importEnvVars(projectRef, environmentSlug, {
630+
return await apiClient.importEnvVars(projectRef, environmentSlug, {
630631
variables: envVars,
631632
parentVariables: parentEnvVars,
632633
override: true,
633634
});
634-
635-
return uploadResult.success;
636635
}
637636

638637
async function failDeploy(
@@ -663,7 +662,7 @@ async function failDeploy(
663662
}. Full build logs have been saved to ${logPath}`
664663
);
665664
} else {
666-
outro(`${chalkError(`${prefix}:`)} ${error.message}.`);
665+
outro(`${chalkError(`${prefix}:`)} ${error.message}`);
667666
}
668667
};
669668

0 commit comments

Comments
 (0)