Skip to content

v4: Can now set project ref using the TRIGGER_PROJECT_REF env var #2109

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 4 commits into from
May 27, 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/silly-timers-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Can now set project ref using the TRIGGER_PROJECT_REF env var
9 changes: 8 additions & 1 deletion packages/cli-v3/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { login } from "./login.js";
import { updateTriggerPackages } from "./update.js";
import { setGithubActionsOutputAndEnvVars } from "../utilities/githubActions.js";
import { isDirectory } from "../utilities/fileSystem.js";
import { resolveLocalEnvVars } from "../utilities/localEnvVars.js";

const DeployCommandOptions = CommonCommandOptions.extend({
dryRun: z.boolean().default(false),
Expand Down Expand Up @@ -207,9 +208,15 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
}
}

const envVars = resolveLocalEnvVars(options.envFile);

if (envVars.TRIGGER_PROJECT_REF) {
logger.debug("Using project ref from env", { ref: envVars.TRIGGER_PROJECT_REF });
}

const resolvedConfig = await loadConfig({
cwd: projectPath,
overrides: { project: options.projectRef },
overrides: { project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF },
configFile: options.config,
});

Expand Down
19 changes: 10 additions & 9 deletions packages/cli-v3/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { ResolvedConfig } from "@trigger.dev/core/v3/build";
import { Command } from "commander";
import { z } from "zod";
import {
CommonCommandOptions,
commonOptions,
SkipLoggingError,
wrapCommandAction,
} from "../cli/common.js";
import { CommonCommandOptions, commonOptions, wrapCommandAction } from "../cli/common.js";
import { watchConfig } from "../config.js";
import { DevSessionInstance, startDevSession } from "../dev/devSession.js";
import { createLockFile } from "../dev/lock.js";
import { chalkError } from "../utilities/cliOutput.js";
import { resolveLocalEnvVars } from "../utilities/localEnvVars.js";
import { printDevBanner, printStandloneInitialBanner } from "../utilities/initialBanner.js";
import { logger } from "../utilities/logger.js";
import { runtimeChecks } from "../utilities/runtimeCheck.js";
import { getProjectClient, LoginResultOk } from "../utilities/session.js";
import { login } from "./login.js";
import { updateTriggerPackages } from "./update.js";
import { createLockFile } from "../dev/lock.js";
import { BundleError } from "../build/bundle.js";

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

printDevBanner(displayedUpdateMessage);

const envVars = resolveLocalEnvVars(options.envFile);

if (envVars.TRIGGER_PROJECT_REF) {
logger.debug("Using project ref from env", { ref: envVars.TRIGGER_PROJECT_REF });
}

watcher = await watchConfig({
cwd: options.cwd,
async onUpdate(config) {
Expand All @@ -145,7 +146,7 @@ async function startDev(options: StartDevOptions) {
devInstance = await bootDevSession(config);
},
overrides: {
project: options.projectRef,
project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF,
},
configFile: options.config,
});
Expand Down
20 changes: 4 additions & 16 deletions packages/cli-v3/src/dev/devSupervisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { ResolvedConfig } from "@trigger.dev/core/v3/build";
import { CliApiClient } from "../apiClient.js";
import { DevCommandOptions } from "../commands/dev.js";
import { resolveDotEnvVars } from "../utilities/dotEnv.js";
import { eventBus } from "../utilities/eventBus.js";
import { logger } from "../utilities/logger.js";
import { sanitizeEnvVars } from "../utilities/sanitizeEnvVars.js";
Expand All @@ -25,6 +24,7 @@ import {
WorkerServerToClientEvents,
} from "@trigger.dev/core/v3/workers";
import pLimit from "p-limit";
import { resolveLocalEnvVars } from "../utilities/localEnvVars.js";

export type WorkerRuntimeOptions = {
name: string | undefined;
Expand Down Expand Up @@ -372,18 +372,16 @@ class DevSupervisor implements WorkerRuntime {
this.options.config.project
);

const processEnv = gatherProcessEnv();
const dotEnvVars = resolveDotEnvVars(undefined, this.options.args.envFile);
const OTEL_IMPORT_HOOK_INCLUDES = (this.options.config.instrumentedPackageNames ?? []).join(
","
);

return {
...sanitizeEnvVars(processEnv),
...sanitizeEnvVars(
...resolveLocalEnvVars(
this.options.args.envFile,
environmentVariablesResponse.success ? environmentVariablesResponse.data.variables : {}
),
...sanitizeEnvVars(dotEnvVars),
NODE_ENV: "development",
TRIGGER_API_URL: this.options.client.apiURL,
TRIGGER_SECRET_KEY: this.options.client.accessToken!,
OTEL_EXPORTER_OTLP_COMPRESSION: "none",
Expand Down Expand Up @@ -581,16 +579,6 @@ class DevSupervisor implements WorkerRuntime {
}
}

function gatherProcessEnv() {
const $env = {
...process.env,
NODE_ENV: "development",
};

// Filter out undefined values
return Object.fromEntries(Object.entries($env).filter(([key, value]) => value !== undefined));
}

type ValidationIssue =
| {
type: "duplicateTaskId";
Expand Down
1 change: 1 addition & 0 deletions packages/cli-v3/src/entryPoints/dev-run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ export class DevRunController {
...sanitizeEnvVars(this.opts.worker.params.env),
TRIGGER_WORKER_MANIFEST_PATH: join(this.opts.worker.build.outputPath, "index.json"),
RUN_WORKER_SHOW_LOGS: this.opts.logLevel === "debug" ? "true" : "false",
TRIGGER_PROJECT_REF: execution.project.ref,
},
serverWorker: {
id: "unmanaged",
Expand Down
5 changes: 4 additions & 1 deletion packages/cli-v3/src/entryPoints/managed/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,10 @@ export class RunExecution {

// To skip this step and eagerly create the task run process, run prepareForExecution first
if (!this.taskRunProcess || !this.taskRunProcess.isPreparedForNextRun) {
this.taskRunProcess = this.createTaskRunProcess({ envVars, isWarmStart });
this.taskRunProcess = this.createTaskRunProcess({
envVars: { ...envVars, TRIGGER_PROJECT_REF: execution.project.ref },
isWarmStart,
});
}

this.sendDebugLog("executing task run process", { runId: execution.run.id });
Expand Down
25 changes: 25 additions & 0 deletions packages/cli-v3/src/utilities/localEnvVars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { resolveDotEnvVars } from "./dotEnv.js";
import { sanitizeEnvVars } from "./sanitizeEnvVars.js";

export function resolveLocalEnvVars(
envFile?: string,
additionalVariables?: Record<string, string>
) {
const processEnv = gatherProcessEnv();
const dotEnvVars = resolveDotEnvVars(undefined, envFile);

return {
...sanitizeEnvVars(processEnv),
...sanitizeEnvVars(additionalVariables ?? {}),
...sanitizeEnvVars(dotEnvVars),
};
}

function gatherProcessEnv() {
const $env = {
...process.env,
};

// Filter out undefined values
return Object.fromEntries(Object.entries($env).filter(([key, value]) => value !== undefined));
}
2 changes: 2 additions & 0 deletions references/d3-chat/src/trigger/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const queryApprovalTask = schemaTask({
query: z.string().describe("The SQL query to execute"),
}),
run: async ({ userId, input, query }) => {
logger.info("queryApproval: starting", { projectRef: process.env.TRIGGER_PROJECT_REF });

const token = await wait.createToken({
tags: [`user:${userId}`, "approval"],
timeout: "5m", // timeout in 5 minutes
Expand Down
2 changes: 1 addition & 1 deletion references/d3-chat/trigger.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";

export default defineConfig({
project: "proj_cdmymsrobxmcgjqzhdkq",
project: process.env.TRIGGER_PROJECT_REF!,
dirs: ["./src/trigger"],
telemetry: {
logExporters: [
Expand Down
Loading