Skip to content

Commit d756d07

Browse files
committed
feat(build): support isSecret in syncEnvVars
1 parent fcee52f commit d756d07

2 files changed

Lines changed: 42 additions & 7 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@trigger.dev/build": minor
3+
"@trigger.dev/core": minor
4+
"trigger.dev": minor
5+
---
6+
7+
You can now mark environment variables synced via the `syncEnvVars` build extension as secrets. Return `{ name, value, isSecret: true }` from your callback and those variables are stored redacted in the dashboard, just like manually created secret env vars.

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

Lines changed: 35 additions & 7 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
@@ -100,9 +100,16 @@ export function syncEnvVars(fn: SyncEnvVarsFunction, options?: SyncEnvVarsOption
100100

101101
const env = stripUnsyncableEnvVars(result.env);
102102
const parentEnv = result.parentEnv ? stripUnsyncableEnvVars(result.parentEnv) : undefined;
103+
const secretEnv = result.secretEnv ? stripUnsyncableEnvVars(result.secretEnv) : undefined;
104+
const secretParentEnv = result.secretParentEnv
105+
? stripUnsyncableEnvVars(result.secretParentEnv)
106+
: undefined;
103107

104108
const numberOfEnvVars =
105-
Object.keys(env).length + (parentEnv ? Object.keys(parentEnv).length : 0);
109+
Object.keys(env).length +
110+
(parentEnv ? Object.keys(parentEnv).length : 0) +
111+
(secretEnv ? Object.keys(secretEnv).length : 0) +
112+
(secretParentEnv ? Object.keys(secretParentEnv).length : 0);
106113

107114
if (numberOfEnvVars === 0) {
108115
$spinner.stop("No env vars detected");
@@ -119,6 +126,8 @@ export function syncEnvVars(fn: SyncEnvVarsFunction, options?: SyncEnvVarsOption
119126
deploy: {
120127
env,
121128
parentEnv,
129+
secretEnv,
130+
secretParentEnv,
122131
override: options?.override ?? true,
123132
},
124133
});
@@ -151,9 +160,22 @@ async function callSyncEnvVarsFn(
151160
environment: string,
152161
branch: string | undefined,
153162
context: BuildContext
154-
): Promise<{ env: Record<string, string>; parentEnv?: Record<string, string> } | undefined> {
163+
): Promise<
164+
| {
165+
env: Record<string, string>;
166+
parentEnv?: Record<string, string>;
167+
secretEnv?: Record<string, string>;
168+
secretParentEnv?: Record<string, string>;
169+
}
170+
| undefined
171+
> {
155172
if (syncEnvVarsFn && typeof syncEnvVarsFn === "function") {
156-
let resolvedEnvVars: { env: Record<string, string>; parentEnv?: Record<string, string> } = {
173+
let resolvedEnvVars: {
174+
env: Record<string, string>;
175+
parentEnv?: Record<string, string>;
176+
secretEnv?: Record<string, string>;
177+
secretParentEnv?: Record<string, string>;
178+
} = {
157179
env: {},
158180
};
159181
let result;
@@ -184,10 +206,16 @@ async function callSyncEnvVarsFn(
184206
typeof item.value === "string"
185207
) {
186208
if (item.isParentEnv) {
187-
if (!resolvedEnvVars.parentEnv) {
188-
resolvedEnvVars.parentEnv = {};
209+
if (item.isSecret) {
210+
resolvedEnvVars.secretParentEnv ??= {};
211+
resolvedEnvVars.secretParentEnv[item.name] = item.value;
212+
} else {
213+
resolvedEnvVars.parentEnv ??= {};
214+
resolvedEnvVars.parentEnv[item.name] = item.value;
189215
}
190-
resolvedEnvVars.parentEnv[item.name] = item.value;
216+
} else if (item.isSecret) {
217+
resolvedEnvVars.secretEnv ??= {};
218+
resolvedEnvVars.secretEnv[item.name] = item.value;
191219
} else {
192220
resolvedEnvVars.env[item.name] = item.value;
193221
}

0 commit comments

Comments
 (0)