Skip to content

Commit 1011f89

Browse files
committed
v4: Can now set project ref using the TRIGGER_PROJECT_REF env var
1 parent e84ede9 commit 1011f89

File tree

8 files changed

+52
-17
lines changed

8 files changed

+52
-17
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { login } from "./login.js";
3636
import { updateTriggerPackages } from "./update.js";
3737
import { setGithubActionsOutputAndEnvVars } from "../utilities/githubActions.js";
3838
import { isDirectory } from "../utilities/fileSystem.js";
39+
import { resolveEnvVars } from "../utilities/envVars.js";
3940

4041
const DeployCommandOptions = CommonCommandOptions.extend({
4142
dryRun: z.boolean().default(false),
@@ -207,9 +208,15 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
207208
}
208209
}
209210

211+
const envVars = resolveEnvVars(options.envFile);
212+
213+
if (envVars.TRIGGER_PROJECT_REF) {
214+
logger.debug("Using project ref from env", { ref: envVars.TRIGGER_PROJECT_REF });
215+
}
216+
210217
const resolvedConfig = await loadConfig({
211218
cwd: projectPath,
212-
overrides: { project: options.projectRef },
219+
overrides: { project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF },
213220
configFile: options.config,
214221
});
215222

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import { ResolvedConfig } from "@trigger.dev/core/v3/build";
22
import { Command } from "commander";
33
import { z } from "zod";
4-
import {
5-
CommonCommandOptions,
6-
commonOptions,
7-
SkipLoggingError,
8-
wrapCommandAction,
9-
} from "../cli/common.js";
4+
import { CommonCommandOptions, commonOptions, wrapCommandAction } from "../cli/common.js";
105
import { watchConfig } from "../config.js";
116
import { DevSessionInstance, startDevSession } from "../dev/devSession.js";
7+
import { createLockFile } from "../dev/lock.js";
128
import { chalkError } from "../utilities/cliOutput.js";
9+
import { resolveEnvVars } from "../utilities/envVars.js";
1310
import { printDevBanner, printStandloneInitialBanner } from "../utilities/initialBanner.js";
1411
import { logger } from "../utilities/logger.js";
1512
import { runtimeChecks } from "../utilities/runtimeCheck.js";
1613
import { getProjectClient, LoginResultOk } from "../utilities/session.js";
1714
import { login } from "./login.js";
1815
import { updateTriggerPackages } from "./update.js";
19-
import { createLockFile } from "../dev/lock.js";
20-
import { BundleError } from "../build/bundle.js";
2116

2217
const DevCommandOptions = CommonCommandOptions.extend({
2318
debugOtel: z.boolean().default(false),
@@ -133,6 +128,12 @@ async function startDev(options: StartDevOptions) {
133128

134129
printDevBanner(displayedUpdateMessage);
135130

131+
const envVars = resolveEnvVars(options.envFile);
132+
133+
if (envVars.TRIGGER_PROJECT_REF) {
134+
logger.debug("Using project ref from env", { ref: envVars.TRIGGER_PROJECT_REF });
135+
}
136+
136137
watcher = await watchConfig({
137138
cwd: options.cwd,
138139
async onUpdate(config) {
@@ -145,7 +146,7 @@ async function startDev(options: StartDevOptions) {
145146
devInstance = await bootDevSession(config);
146147
},
147148
overrides: {
148-
project: options.projectRef,
149+
project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF,
149150
},
150151
configFile: options.config,
151152
});

packages/cli-v3/src/dev/devSupervisor.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
WorkerServerToClientEvents,
2626
} from "@trigger.dev/core/v3/workers";
2727
import pLimit from "p-limit";
28+
import { resolveEnvVars } from "../utilities/envVars.js";
2829

2930
export type WorkerRuntimeOptions = {
3031
name: string | undefined;
@@ -372,18 +373,15 @@ class DevSupervisor implements WorkerRuntime {
372373
this.options.config.project
373374
);
374375

375-
const processEnv = gatherProcessEnv();
376-
const dotEnvVars = resolveDotEnvVars(undefined, this.options.args.envFile);
377376
const OTEL_IMPORT_HOOK_INCLUDES = (this.options.config.instrumentedPackageNames ?? []).join(
378377
","
379378
);
380379

381380
return {
382-
...sanitizeEnvVars(processEnv),
383-
...sanitizeEnvVars(
381+
...resolveEnvVars(
382+
this.options.args.envFile,
384383
environmentVariablesResponse.success ? environmentVariablesResponse.data.variables : {}
385384
),
386-
...sanitizeEnvVars(dotEnvVars),
387385
TRIGGER_API_URL: this.options.client.apiURL,
388386
TRIGGER_SECRET_KEY: this.options.client.accessToken!,
389387
OTEL_EXPORTER_OTLP_COMPRESSION: "none",

packages/cli-v3/src/entryPoints/dev-run-controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ export class DevRunController {
612612
...sanitizeEnvVars(this.opts.worker.params.env),
613613
TRIGGER_WORKER_MANIFEST_PATH: join(this.opts.worker.build.outputPath, "index.json"),
614614
RUN_WORKER_SHOW_LOGS: this.opts.logLevel === "debug" ? "true" : "false",
615+
TRIGGER_PROJECT_REF: execution.project.ref,
615616
},
616617
serverWorker: {
617618
id: "unmanaged",

packages/cli-v3/src/entryPoints/managed/execution.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,10 @@ export class RunExecution {
586586

587587
// To skip this step and eagerly create the task run process, run prepareForExecution first
588588
if (!this.taskRunProcess || !this.taskRunProcess.isPreparedForNextRun) {
589-
this.taskRunProcess = this.createTaskRunProcess({ envVars, isWarmStart });
589+
this.taskRunProcess = this.createTaskRunProcess({
590+
envVars: { ...envVars, TRIGGER_PROJECT_REF: execution.project.ref },
591+
isWarmStart,
592+
});
590593
}
591594

592595
this.sendDebugLog("executing task run process", { runId: execution.run.id });
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { resolveDotEnvVars } from "./dotEnv.js";
2+
import { sanitizeEnvVars } from "./sanitizeEnvVars.js";
3+
4+
export function resolveEnvVars(envFile?: string, additionalVariables?: Record<string, string>) {
5+
const processEnv = gatherProcessEnv();
6+
const dotEnvVars = resolveDotEnvVars(undefined, envFile);
7+
8+
return {
9+
...sanitizeEnvVars(processEnv),
10+
...sanitizeEnvVars(additionalVariables ?? {}),
11+
...sanitizeEnvVars(dotEnvVars),
12+
};
13+
}
14+
15+
function gatherProcessEnv() {
16+
const $env = {
17+
...process.env,
18+
NODE_ENV: "development",
19+
};
20+
21+
// Filter out undefined values
22+
return Object.fromEntries(Object.entries($env).filter(([key, value]) => value !== undefined));
23+
}

references/d3-chat/src/trigger/chat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const queryApprovalTask = schemaTask({
2727
query: z.string().describe("The SQL query to execute"),
2828
}),
2929
run: async ({ userId, input, query }) => {
30+
logger.info("queryApproval: starting", { projectRef: process.env.TRIGGER_PROJECT_REF });
31+
3032
const token = await wait.createToken({
3133
tags: [`user:${userId}`, "approval"],
3234
timeout: "5m", // timeout in 5 minutes

references/d3-chat/trigger.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
55
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
66

77
export default defineConfig({
8-
project: "proj_cdmymsrobxmcgjqzhdkq",
8+
project: process.env.TRIGGER_PROJECT_REF!,
99
dirs: ["./src/trigger"],
1010
telemetry: {
1111
logExporters: [

0 commit comments

Comments
 (0)