Skip to content

Fix syncing parent env vars for non-preview deployments #2131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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/witty-cherries-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Fix `syncEnvVars` for non-preview deployments
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
})),
});

// Only sync parent variables if this is a branch environment
if (environment.parentEnvironmentId && body.parentVariables) {
const parentResult = await repository.create(environment.project.id, {
override: typeof body.override === "boolean" ? body.override : false,
Expand Down
21 changes: 10 additions & 11 deletions packages/cli-v3/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {

const branch =
options.env === "preview" ? getBranch({ specified: options.branch, gitMeta }) : undefined;

if (options.env === "preview" && !branch) {
throw new Error(
"Didn't auto-detect preview branch, so you need to specify one. Pass --branch <branch>."
Expand Down Expand Up @@ -352,10 +353,9 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
}

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

if (hasVarsToSync) {
const childVars = buildManifest.deploy.sync?.env ?? {};
Expand All @@ -367,21 +367,22 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
if (!options.skipSyncEnvVars) {
const $spinner = spinner();
$spinner.start(`Syncing ${numberOfEnvVars} env ${vars} with the server`);
const success = await syncEnvVarsWithServer(

const uploadResult = await syncEnvVarsWithServer(
projectClient.client,
resolvedConfig.project,
options.env,
childVars,
parentVars
);

if (!success) {
if (!uploadResult.success) {
await failDeploy(
projectClient.client,
deployment,
{
name: "SyncEnvVarsError",
message: `Failed to sync ${numberOfEnvVars} env ${vars} with the server`,
message: `Failed to sync ${numberOfEnvVars} env ${vars} with the server: ${uploadResult.error}`,
},
"",
$spinner
Expand Down Expand Up @@ -626,13 +627,11 @@ export async function syncEnvVarsWithServer(
envVars: Record<string, string>,
parentEnvVars?: Record<string, string>
) {
const uploadResult = await apiClient.importEnvVars(projectRef, environmentSlug, {
return await apiClient.importEnvVars(projectRef, environmentSlug, {
variables: envVars,
parentVariables: parentEnvVars,
override: true,
});

return uploadResult.success;
}

async function failDeploy(
Expand Down Expand Up @@ -663,7 +662,7 @@ async function failDeploy(
}. Full build logs have been saved to ${logPath}`
);
} else {
outro(`${chalkError(`${prefix}:`)} ${error.message}.`);
outro(`${chalkError(`${prefix}:`)} ${error.message}`);
}
};

Expand Down