Skip to content

Commit 2ce39b2

Browse files
committed
test(build): cover syncEnvVars isSecret partitioning + docs
1 parent dc5511f commit 2ce39b2

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

docs/config/extensions/syncEnvVars.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@ The callback is passed a context object with the following properties:
3232
- `projectRef`: The project ref of the Trigger.dev project
3333
- `env`: The environment variables that are currently set in the Trigger.dev project
3434

35+
### Marking variables as secrets
36+
37+
Return the array form and set `isSecret: true` on any variable you want stored as a [secret](/deploy-environment-variables). Secret env vars are redacted in the dashboard and their values can't be revealed after they're set, just like manually created secret variables. You can mix secret and non-secret variables in the same callback.
38+
39+
```ts
40+
import { defineConfig } from "@trigger.dev/sdk";
41+
import { syncEnvVars } from "@trigger.dev/build/extensions/core";
42+
43+
export default defineConfig({
44+
build: {
45+
extensions: [
46+
syncEnvVars(async (ctx) => {
47+
return [
48+
{ name: "PUBLIC_API_URL", value: "https://api.example.com" },
49+
{ name: "DATABASE_URL", value: "postgres://...", isSecret: true },
50+
];
51+
}),
52+
],
53+
},
54+
});
55+
```
56+
57+
<Note>
58+
`isSecret` is only available when you return the array form (`{ name, value, isSecret }`). The
59+
record form (`{ KEY: "value" }`) always creates non-secret variables.
60+
</Note>
61+
3562
### Example: Sync env vars from Infisical
3663

3764
In this example we're using env vars from [Infisical](https://infisical.com).

packages/build/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
"dev": "tshy --watch",
7575
"typecheck": "tsc --noEmit -p tsconfig.src.json",
7676
"update-version": "tsx ../../scripts/updateVersion.ts",
77-
"check-exports": "attw --pack ."
77+
"check-exports": "attw --pack .",
78+
"test": "vitest"
7879
},
7980
"dependencies": {
8081
"@prisma/config": "^6.10.0",
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { BuildContext, BuildLayer } from "@trigger.dev/core/v3/build";
3+
import { syncEnvVars, type SyncEnvVarsFunction } from "./syncEnvVars.js";
4+
5+
async function runExtension(fn: SyncEnvVarsFunction): Promise<BuildLayer | undefined> {
6+
let captured: BuildLayer | undefined;
7+
8+
const context = {
9+
target: "deploy",
10+
config: { project: "proj_test" },
11+
logger: {
12+
spinner: () => ({ stop: () => {} }),
13+
},
14+
addLayer: (layer: BuildLayer) => {
15+
captured = layer;
16+
},
17+
} as unknown as BuildContext;
18+
19+
const manifest = {
20+
deploy: { env: {} },
21+
environment: "prod",
22+
branch: undefined,
23+
} as any;
24+
25+
const extension = syncEnvVars(fn);
26+
await extension.onBuildComplete!(context, manifest);
27+
28+
return captured;
29+
}
30+
31+
describe("syncEnvVars isSecret", () => {
32+
it("partitions secret and non-secret vars across child and parent", async () => {
33+
const layer = await runExtension(() => [
34+
{ name: "PUBLIC_URL", value: "https://example.com" },
35+
{ name: "API_KEY", value: "secret-key", isSecret: true },
36+
{ name: "PARENT_PUBLIC", value: "parent", isParentEnv: true },
37+
{ name: "PARENT_SECRET", value: "parent-secret", isParentEnv: true, isSecret: true },
38+
]);
39+
40+
expect(layer?.deploy?.env).toEqual({ PUBLIC_URL: "https://example.com" });
41+
expect(layer?.deploy?.secretEnv).toEqual({ API_KEY: "secret-key" });
42+
expect(layer?.deploy?.parentEnv).toEqual({ PARENT_PUBLIC: "parent" });
43+
expect(layer?.deploy?.secretParentEnv).toEqual({ PARENT_SECRET: "parent-secret" });
44+
});
45+
46+
it("treats the record form as all non-secret", async () => {
47+
const layer = await runExtension(() => ({ DATABASE_URL: "postgres://..." }));
48+
49+
expect(layer?.deploy?.env).toEqual({ DATABASE_URL: "postgres://..." });
50+
expect(layer?.deploy?.secretEnv).toBeUndefined();
51+
expect(layer?.deploy?.secretParentEnv).toBeUndefined();
52+
});
53+
54+
it("omits secret buckets when no var is marked secret", async () => {
55+
const layer = await runExtension(() => [{ name: "PUBLIC_URL", value: "https://example.com" }]);
56+
57+
expect(layer?.deploy?.env).toEqual({ PUBLIC_URL: "https://example.com" });
58+
expect(layer?.deploy?.secretEnv).toBeUndefined();
59+
expect(layer?.deploy?.secretParentEnv).toBeUndefined();
60+
});
61+
});

packages/build/vitest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
include: ["src/**/*.test.ts"],
6+
globals: true,
7+
},
8+
});

0 commit comments

Comments
 (0)