Skip to content

Commit de33741

Browse files
feat(build): add support for marking env vars as secrets in syncEnvVars
- Added isSecret flag to SyncEnvVarsBody type - Updated CLI's syncEnvVarsWithServer to pass secret flags - Modified backend API endpoint to handle secret flags - Added documentation for the new feature
1 parent 471c960 commit de33741

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

.changeset/fresh-bats-travel.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"trigger.dev": minor
3+
"@trigger.dev/build": minor
4+
---
5+
6+
Added support for the secret flag on variables in the syncEnvVars extension

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
4444
variables: Object.entries(body.variables).map(([key, value]) => ({
4545
key,
4646
value,
47+
isSecret: body.secrets?.[key] ?? false,
4748
})),
4849
});
4950

@@ -55,6 +56,7 @@ export async function action({ params, request }: ActionFunctionArgs) {
5556
variables: Object.entries(body.parentVariables).map(([key, value]) => ({
5657
key,
5758
value,
59+
isSecret: body.parentSecrets?.[key] ?? false,
5860
})),
5961
});
6062

packages/build/src/extensions/core/syncEnvVars.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build";
22

33
export type SyncEnvVarsBody =
44
| Record<string, string>
5-
| Array<{ name: string; value: string; isParentEnv?: boolean }>;
5+
| Array<{ name: string; value: string; isParentEnv?: boolean; isSecret?: boolean }>;
66

77
export type SyncEnvVarsResult =
88
| SyncEnvVarsBody
@@ -151,9 +151,19 @@ async function callSyncEnvVarsFn(
151151
environment: string,
152152
branch: string | undefined,
153153
context: BuildContext
154-
): Promise<{ env: Record<string, string>; parentEnv?: Record<string, string> } | undefined> {
154+
): Promise<{
155+
env: Record<string, string>;
156+
secrets?: Record<string, boolean>;
157+
parentEnv?: Record<string, string>;
158+
parentEnvSecrets?: Record<string, boolean>;
159+
} | undefined> {
155160
if (syncEnvVarsFn && typeof syncEnvVarsFn === "function") {
156-
let resolvedEnvVars: { env: Record<string, string>; parentEnv?: Record<string, string> } = {
161+
let resolvedEnvVars: {
162+
env: Record<string, string>;
163+
secrets?: Record<string, boolean>;
164+
parentEnv?: Record<string, string>;
165+
parentEnvSecrets?: Record<string, boolean>;
166+
} = {
157167
env: {},
158168
};
159169
let result;
@@ -186,10 +196,20 @@ async function callSyncEnvVarsFn(
186196
if (item.isParentEnv) {
187197
if (!resolvedEnvVars.parentEnv) {
188198
resolvedEnvVars.parentEnv = {};
199+
resolvedEnvVars.parentEnvSecrets = {};
189200
}
190201
resolvedEnvVars.parentEnv[item.name] = item.value;
202+
if (item.isSecret && resolvedEnvVars.parentEnvSecrets) {
203+
resolvedEnvVars.parentEnvSecrets[item.name] = true;
204+
}
191205
} else {
192206
resolvedEnvVars.env[item.name] = item.value;
207+
if (item.isSecret) {
208+
if (!resolvedEnvVars.secrets) {
209+
resolvedEnvVars.secrets = {};
210+
}
211+
resolvedEnvVars.secrets[item.name] = true;
212+
}
193213
}
194214
}
195215
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,8 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
381381
const version = deployment.version;
382382

383383
const rawDeploymentLink = `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project}/deployments/${deployment.shortCode}`;
384-
const rawTestLink = `${authorization.dashboardUrl}/projects/v3/${
385-
resolvedConfig.project
386-
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`;
384+
const rawTestLink = `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project
385+
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`;
387386

388387
const deploymentLink = cliLink("View deployment", rawDeploymentLink);
389388
const testLink = cliLink("Test tasks", rawTestLink);
@@ -562,8 +561,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
562561
const taskCount = deploymentWithWorker.worker?.tasks.length ?? 0;
563562

564563
outro(
565-
`Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"} ${
566-
isLinksSupported ? `| ${deploymentLink} | ${testLink}` : ""
564+
`Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"} ${isLinksSupported ? `| ${deploymentLink} | ${testLink}` : ""
567565
}`
568566
);
569567

@@ -586,18 +584,16 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
586584
TRIGGER_VERSION: version,
587585
TRIGGER_DEPLOYMENT_SHORT_CODE: deployment.shortCode,
588586
TRIGGER_DEPLOYMENT_URL: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project}/deployments/${deployment.shortCode}`,
589-
TRIGGER_TEST_URL: `${authorization.dashboardUrl}/projects/v3/${
590-
resolvedConfig.project
591-
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
587+
TRIGGER_TEST_URL: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project
588+
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
592589
},
593590
outputs: {
594591
deploymentVersion: version,
595592
workerVersion: version,
596593
deploymentShortCode: deployment.shortCode,
597594
deploymentUrl: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project}/deployments/${deployment.shortCode}`,
598-
testUrl: `${authorization.dashboardUrl}/projects/v3/${
599-
resolvedConfig.project
600-
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
595+
testUrl: `${authorization.dashboardUrl}/projects/v3/${resolvedConfig.project
596+
}/test?environment=${options.env === "prod" ? "prod" : "stg"}`,
601597
needsPromotion: options.skipPromotion ? "true" : "false",
602598
},
603599
});
@@ -608,11 +604,15 @@ export async function syncEnvVarsWithServer(
608604
projectRef: string,
609605
environmentSlug: string,
610606
envVars: Record<string, string>,
611-
parentEnvVars?: Record<string, string>
607+
parentEnvVars?: Record<string, string>,
608+
secrets?: Record<string, boolean>,
609+
parentEnvSecrets?: Record<string, boolean>
612610
) {
613611
return await apiClient.importEnvVars(projectRef, environmentSlug, {
614612
variables: envVars,
615613
parentVariables: parentEnvVars,
614+
secrets,
615+
parentSecrets: parentEnvSecrets,
616616
override: true,
617617
});
618618
}
@@ -640,8 +640,7 @@ async function failDeploy(
640640
checkLogsForErrors(logs);
641641

642642
outro(
643-
`${chalkError(`${prefix}:`)} ${
644-
error.message
643+
`${chalkError(`${prefix}:`)} ${error.message
645644
}. Full build logs have been saved to ${logPath}`
646645
);
647646

packages/core/src/v3/apiClient/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ export interface ImportEnvironmentVariablesParams {
1313
* To specify the variables, you can pass them in as a record of key-value pairs. e.g. `{ "key1": "value1", "key2": "value2" }`
1414
*/
1515
variables: Record<string, string>;
16+
/**
17+
* Optional parent variables to be imported. These are used when dealing with branch environments.
18+
*/
19+
parentVariables?: Record<string, string>;
20+
/**
21+
* Optional map of which variables should be marked as secrets. The keys should match the keys in `variables`.
22+
*/
23+
secrets?: Record<string, boolean>;
24+
/**
25+
* Optional map of which parent variables should be marked as secrets. The keys should match the keys in `parentVariables`.
26+
*/
27+
parentSecrets?: Record<string, boolean>;
1628
override?: boolean;
1729
}
1830

packages/core/src/v3/schemas/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,8 @@ export type UpdateEnvironmentVariableRequestBody = z.infer<
799799
export const ImportEnvironmentVariablesRequestBody = z.object({
800800
variables: z.record(z.string()),
801801
parentVariables: z.record(z.string()).optional(),
802+
secrets: z.record(z.boolean()).optional(),
803+
parentSecrets: z.record(z.boolean()).optional(),
802804
override: z.boolean().optional(),
803805
});
804806

0 commit comments

Comments
 (0)