Skip to content

Commit 3afa516

Browse files
committed
syncEnvVars working with branches
1 parent 3ccb6d6 commit 3afa516

File tree

11 files changed

+74
-8
lines changed

11 files changed

+74
-8
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { z } from "zod";
55
import {
66
authenticateProjectApiKeyOrPersonalAccessToken,
77
authenticatedEnvironmentForAuthentication,
8+
branchNameFromRequest,
89
} from "~/services/apiAuth.server";
910
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
1011

@@ -29,7 +30,8 @@ export async function action({ params, request }: ActionFunctionArgs) {
2930
const environment = await authenticatedEnvironmentForAuthentication(
3031
authenticationResult,
3132
parsedParams.data.projectRef,
32-
parsedParams.data.slug
33+
parsedParams.data.slug,
34+
branchNameFromRequest(request)
3335
);
3436

3537
const repository = new EnvironmentVariablesRepository();

apps/webapp/app/services/apiAuth.server.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isPersonalAccessToken,
1818
} from "./personalAccessToken.server";
1919
import { isPublicJWT, validatePublicJwtKey } from "./realtime/jwtAuth.server";
20+
import { sanitizeBranchName } from "./upsertBranch.server";
2021

2122
const ClaimsSchema = z.object({
2223
scopes: z.array(z.string()).optional(),
@@ -261,14 +262,18 @@ function isSecretApiKey(key: string) {
261262
return key.startsWith("tr_");
262263
}
263264

265+
export function branchNameFromRequest(request: Request): string | undefined {
266+
return request.headers.get("x-trigger-branch") ?? undefined;
267+
}
268+
264269
function getApiKeyFromRequest(request: Request): {
265270
apiKey: string | undefined;
266271
branchName: string | undefined;
267272
} {
268273
const apiKey = getApiKeyFromHeader(request.headers.get("Authorization"));
269-
const branchHeaderValue = request.headers.get("x-trigger-branch");
274+
const branchName = branchNameFromRequest(request);
270275

271-
return { apiKey, branchName: branchHeaderValue ? branchHeaderValue : undefined };
276+
return { apiKey, branchName };
272277
}
273278

274279
function getApiKeyFromHeader(authorization?: string | null) {
@@ -340,7 +345,8 @@ export async function authenticateProjectApiKeyOrPersonalAccessToken(
340345
export async function authenticatedEnvironmentForAuthentication(
341346
auth: DualAuthenticationResult,
342347
projectRef: string,
343-
slug: string
348+
slug: string,
349+
branch?: string
344350
): Promise<AuthenticatedEnvironment> {
345351
if (slug === "staging") {
346352
slug = "stg";
@@ -362,7 +368,7 @@ export async function authenticatedEnvironmentForAuthentication(
362368
);
363369
}
364370

365-
if (auth.result.environment.slug !== slug) {
371+
if (auth.result.environment.slug !== slug && auth.result.environment.branchName !== branch) {
366372
throw json(
367373
{
368374
error:
@@ -391,22 +397,53 @@ export async function authenticatedEnvironmentForAuthentication(
391397
throw json({ error: "Project not found" }, { status: 404 });
392398
}
393399

400+
if (!branch) {
401+
const environment = await prisma.runtimeEnvironment.findFirst({
402+
where: {
403+
projectId: project.id,
404+
slug: slug,
405+
},
406+
include: {
407+
project: true,
408+
organization: true,
409+
},
410+
});
411+
412+
if (!environment) {
413+
throw json({ error: "Environment not found" }, { status: 404 });
414+
}
415+
416+
return environment;
417+
}
418+
394419
const environment = await prisma.runtimeEnvironment.findFirst({
395420
where: {
396421
projectId: project.id,
397422
slug: slug,
423+
branchName: sanitizeBranchName(branch),
424+
archivedAt: null,
398425
},
399426
include: {
400427
project: true,
401428
organization: true,
429+
parentEnvironment: true,
402430
},
403431
});
404432

405433
if (!environment) {
406-
throw json({ error: "Environment not found" }, { status: 404 });
434+
throw json({ error: "Branch not found" }, { status: 404 });
435+
}
436+
437+
if (!environment.parentEnvironment) {
438+
throw json({ error: "Branch not associated with a preview environment" }, { status: 400 });
407439
}
408440

409-
return environment;
441+
return {
442+
...environment,
443+
apiKey: environment.parentEnvironment.apiKey,
444+
organization: environment.organization,
445+
project: environment.project,
446+
};
410447
}
411448
}
412449
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type SyncEnvVarsResult =
1111
export type SyncEnvVarsParams = {
1212
projectRef: string;
1313
environment: string;
14+
branch?: string;
1415
env: Record<string, string>;
1516
};
1617

@@ -85,6 +86,7 @@ export function syncEnvVars(fn: SyncEnvVarsFunction, options?: SyncEnvVarsOption
8586
fn,
8687
manifest.deploy.env ?? {},
8788
manifest.environment,
89+
manifest.branch,
8890
context
8991
);
9092

@@ -138,6 +140,7 @@ async function callSyncEnvVarsFn(
138140
syncEnvVarsFn: SyncEnvVarsFunction | undefined,
139141
env: Record<string, string>,
140142
environment: string,
143+
branch: string | undefined,
141144
context: BuildContext
142145
): Promise<Record<string, string> | undefined> {
143146
if (syncEnvVarsFn && typeof syncEnvVarsFn === "function") {
@@ -149,6 +152,7 @@ async function callSyncEnvVarsFn(
149152
projectRef: context.config.project,
150153
environment,
151154
env,
155+
branch,
152156
});
153157
} catch (error) {
154158
context.logger.warn("Error calling syncEnvVars function", error);

packages/cli-v3/src/build/buildWorker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type BuildWorkerOptions = {
2828
destination: string;
2929
target: BuildTarget;
3030
environment: string;
31+
branch?: string;
3132
resolvedConfig: ResolvedConfig;
3233
listener?: BuildWorkerEventListener;
3334
envVars?: Record<string, string>;
@@ -75,6 +76,7 @@ export async function buildWorker(options: BuildWorkerOptions) {
7576
destination: options.destination,
7677
resolvedConfig,
7778
environment: options.environment,
79+
branch: options.branch,
7880
target: options.target,
7981
envVars: options.envVars,
8082
});

packages/cli-v3/src/build/bundle.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ export async function createBuildManifestFromBundle({
339339
resolvedConfig,
340340
workerDir,
341341
environment,
342+
branch,
342343
target,
343344
envVars,
344345
sdkVersion,
@@ -348,6 +349,7 @@ export async function createBuildManifestFromBundle({
348349
resolvedConfig: ResolvedConfig;
349350
workerDir?: string;
350351
environment: string;
352+
branch?: string;
351353
target: BuildTarget;
352354
envVars?: Record<string, string>;
353355
sdkVersion?: string;
@@ -356,6 +358,7 @@ export async function createBuildManifestFromBundle({
356358
contentHash: bundle.contentHash,
357359
runtime: resolvedConfig.runtime ?? DEFAULT_RUNTIME,
358360
environment: environment,
361+
branch,
359362
packageVersion: sdkVersion ?? CORE_VERSION,
360363
cliPackageVersion: VERSION,
361364
target: target,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
277277
const buildManifest = await buildWorker({
278278
target: "deploy",
279279
environment: options.env,
280+
branch,
280281
destination: destination.path,
281282
resolvedConfig,
282283
rewritePaths: true,

packages/cli-v3/src/commands/workers/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ async function _workerBuildCommand(dir: string, options: WorkersBuildCommandOpti
210210
const buildManifest = await buildWorker({
211211
target: "unmanaged",
212212
environment: options.env,
213+
branch,
213214
destination: destination.path,
214215
resolvedConfig,
215216
rewritePaths: true,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const BuildManifest = z.object({
2424
contentHash: z.string(),
2525
runtime: BuildRuntime,
2626
environment: z.string(),
27+
branch: z.string().optional(),
2728
config: ConfigManifest,
2829
files: z.array(TaskFile),
2930
sources: z.record(

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

references/hello-world/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"trigger.dev": "workspace:*"
77
},
88
"dependencies": {
9+
"@trigger.dev/build": "workspace:*",
910
"@trigger.dev/sdk": "workspace:*",
1011
"openai": "^4.97.0",
1112
"replicate": "^1.0.1",
@@ -15,4 +16,4 @@
1516
"dev": "trigger dev",
1617
"deploy": "trigger deploy"
1718
}
18-
}
19+
}

0 commit comments

Comments
 (0)