Skip to content

Commit 7fba9e9

Browse files
authored
feat(deployments): add build server meta and trigger source info (#2767)
This PR applies a small change to the deployments table to keep track of: - where the deployment was triggered from - build server metadata, if the build server was involved
1 parent cf63fc9 commit 7fba9e9

File tree

9 files changed

+109
-7
lines changed

9 files changed

+109
-7
lines changed

.changeset/fluffy-weeks-sleep.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"trigger.dev": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
The new `triggeredVia` field is now populated in deployments via the CLI.

apps/webapp/app/presenters/v3/DeploymentPresenter.server.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
BuildServerMetadata,
23
DeploymentErrorData,
34
ExternalBuildData,
45
prepareDeploymentError,
@@ -154,17 +155,23 @@ export class DeploymentPresenter {
154155
avatarUrl: true,
155156
},
156157
},
158+
buildServerMetadata: true,
157159
},
158160
});
159161

160162
const gitMetadata = processGitMetadata(deployment.git);
161-
162163
const externalBuildData = deployment.externalBuildData
163164
? ExternalBuildData.safeParse(deployment.externalBuildData)
164165
: undefined;
166+
const buildServerMetadata = deployment.buildServerMetadata
167+
? BuildServerMetadata.safeParse(deployment.buildServerMetadata)
168+
: undefined;
165169

166170
let eventStream = undefined;
167-
if (env.S2_ENABLED === "1" && gitMetadata?.source === "trigger_github_app") {
171+
if (
172+
env.S2_ENABLED === "1" &&
173+
(buildServerMetadata || gitMetadata?.source === "trigger_github_app")
174+
) {
168175
const [error, accessToken] = await tryCatch(this.getS2AccessToken(project.externalRef));
169176

170177
if (error) {

apps/webapp/app/v3/services/initializeDeployment.server.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type InitializeDeploymentRequestBody } from "@trigger.dev/core/v3";
1+
import { BuildServerMetadata, type InitializeDeploymentRequestBody } from "@trigger.dev/core/v3";
22
import { customAlphabet } from "nanoid";
33
import { env } from "~/env.server";
44
import { type AuthenticatedEnvironment } from "~/services/apiAuth.server";
@@ -190,6 +190,21 @@ export class InitializeDeploymentService extends BaseService {
190190
isNativeBuild: payload.isNativeBuild,
191191
});
192192

193+
const buildServerMetadata: BuildServerMetadata | undefined =
194+
payload.isNativeBuild || payload.buildId
195+
? {
196+
buildId: payload.buildId,
197+
...(payload.isNativeBuild
198+
? {
199+
isNativeBuild: payload.isNativeBuild,
200+
artifactKey: payload.artifactKey,
201+
skipPromotion: payload.skipPromotion,
202+
configFilePath: payload.configFilePath,
203+
}
204+
: {}),
205+
}
206+
: undefined;
207+
193208
const deployment = await this._prisma.workerDeployment.create({
194209
data: {
195210
friendlyId: generateFriendlyId("deployment"),
@@ -200,12 +215,14 @@ export class InitializeDeploymentService extends BaseService {
200215
environmentId: environment.id,
201216
projectId: environment.projectId,
202217
externalBuildData,
218+
buildServerMetadata,
203219
triggeredById: triggeredBy?.id,
204220
type: payload.type,
205221
imageReference: imageRef,
206222
imagePlatform: env.DEPLOY_IMAGE_PLATFORM,
207223
git: payload.gitMeta ?? undefined,
208224
runtime: payload.runtime ?? undefined,
225+
triggeredVia: payload.triggeredVia ?? undefined,
209226
startedAt: initialStatus === "BUILDING" ? new Date() : undefined,
210227
},
211228
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "public"."WorkerDeployment" ADD COLUMN "buildServerMetadata" JSONB;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "public"."WorkerDeployment" ADD COLUMN "triggeredVia" TEXT;

internal-packages/database/prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,7 @@ model WorkerDeployment {
17491749
imagePlatform String @default("linux/amd64")
17501750
17511751
externalBuildData Json?
1752+
buildServerMetadata Json?
17521753
17531754
status WorkerDeploymentStatus @default(PENDING)
17541755
type WorkerDeploymentType @default(V1)
@@ -1764,6 +1765,7 @@ model WorkerDeployment {
17641765
17651766
triggeredBy User? @relation(fields: [triggeredById], references: [id], onDelete: SetNull, onUpdate: Cascade)
17661767
triggeredById String?
1768+
triggeredVia String?
17671769
17681770
startedAt DateTime?
17691771
installedAt DateTime?

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
GitMeta,
77
DeploymentFinalizedEvent,
88
DeploymentEventFromString,
9+
DeploymentTriggeredVia,
910
} from "@trigger.dev/core/v3/schemas";
1011
import { Command, Option as CommandOption } from "commander";
1112
import { join, relative, resolve } from "node:path";
@@ -48,7 +49,7 @@ import {
4849
import { loadDotEnvVars } from "../utilities/dotEnv.js";
4950
import { isDirectory } from "../utilities/fileSystem.js";
5051
import { setGithubActionsOutputAndEnvVars } from "../utilities/githubActions.js";
51-
import { createGitMeta } from "../utilities/gitMeta.js";
52+
import { createGitMeta, isGitHubActions } from "../utilities/gitMeta.js";
5253
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
5354
import { resolveLocalEnvVars } from "../utilities/localEnvVars.js";
5455
import { logger } from "../utilities/logger.js";
@@ -382,6 +383,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
382383
type: features.run_engine_v2 ? "MANAGED" : "V1",
383384
runtime: buildManifest.runtime,
384385
isNativeBuild: false,
386+
triggeredVia: getTriggeredVia(),
385387
},
386388
envVars.TRIGGER_EXISTING_DEPLOYMENT_ID
387389
);
@@ -893,6 +895,40 @@ async function initializeOrAttachDeployment(
893895
return newDeploymentOrError.data;
894896
}
895897

898+
function getTriggeredVia(): DeploymentTriggeredVia {
899+
// Check specific CI providers first (most specific to least specific)
900+
if (isGitHubActions()) {
901+
return "cli:github_actions";
902+
}
903+
if (process.env.GITLAB_CI === "true") {
904+
return "cli:gitlab_ci";
905+
}
906+
if (process.env.CIRCLECI === "true") {
907+
return "cli:circleci";
908+
}
909+
if (process.env.JENKINS_URL) {
910+
return "cli:jenkins";
911+
}
912+
if (process.env.TF_BUILD === "True") {
913+
return "cli:azure_pipelines";
914+
}
915+
if (process.env.BITBUCKET_BUILD_NUMBER) {
916+
return "cli:bitbucket_pipelines";
917+
}
918+
if (process.env.TRAVIS === "true") {
919+
return "cli:travis_ci";
920+
}
921+
if (process.env.BUILDKITE === "true") {
922+
return "cli:buildkite";
923+
}
924+
// Fallback for other/unknown CI environments
925+
if (isCI) {
926+
return "cli:ci_other";
927+
}
928+
929+
return "cli:manual";
930+
}
931+
896932
async function handleNativeBuildServerDeploy({
897933
apiClient,
898934
options,
@@ -998,6 +1034,7 @@ async function handleNativeBuildServerDeploy({
9981034
artifactKey,
9991035
skipPromotion: options.skipPromotion,
10001036
configFilePath,
1037+
triggeredVia: getTriggeredVia(),
10011038
});
10021039

10031040
if (!initializeDeploymentResult.success) {

packages/cli-v3/src/utilities/gitMeta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function errorToString(err: unknown): string {
105105
return err instanceof Error ? err.message : String(err);
106106
}
107107

108-
function isGitHubActions() {
108+
export function isGitHubActions() {
109109
// GH Actions CI sets these env variables
110110
return (
111111
process.env.GITHUB_ACTIONS === "true" &&

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,37 @@ export const ExternalBuildData = z.object({
400400

401401
export type ExternalBuildData = z.infer<typeof ExternalBuildData>;
402402

403+
const anyString = z.custom<string & {}>((v) => typeof v === "string");
404+
405+
export const DeploymentTriggeredVia = z
406+
.enum([
407+
"cli:manual",
408+
"cli:ci_other",
409+
"cli:github_actions",
410+
"cli:gitlab_ci",
411+
"cli:circleci",
412+
"cli:jenkins",
413+
"cli:azure_pipelines",
414+
"cli:bitbucket_pipelines",
415+
"cli:travis_ci",
416+
"cli:buildkite",
417+
"git_integration:github",
418+
"dashboard",
419+
])
420+
.or(anyString);
421+
422+
export type DeploymentTriggeredVia = z.infer<typeof DeploymentTriggeredVia>;
423+
424+
export const BuildServerMetadata = z.object({
425+
buildId: z.string().optional(),
426+
isNativeBuild: z.boolean().optional(),
427+
artifactKey: z.string().optional(),
428+
skipPromotion: z.boolean().optional(),
429+
configFilePath: z.string().optional(),
430+
});
431+
432+
export type BuildServerMetadata = z.infer<typeof BuildServerMetadata>;
433+
403434
export const UpsertBranchRequestBody = z.object({
404435
git: GitMeta.optional(),
405436
env: z.enum(["preview"]),
@@ -462,6 +493,8 @@ export const InitializeDeploymentRequestBody = z
462493
type: z.enum(["MANAGED", "UNMANAGED", "V1"]).optional(),
463494
runtime: z.string().optional(),
464495
initialStatus: z.enum(["PENDING", "BUILDING"]).optional(),
496+
triggeredVia: DeploymentTriggeredVia.optional(),
497+
buildId: z.string().optional(),
465498
})
466499
.and(
467500
z.preprocess(
@@ -587,8 +620,6 @@ export const DeploymentLogEvent = z.object({
587620
}),
588621
});
589622

590-
const anyString = z.custom<string & {}>((v) => typeof v === "string");
591-
592623
export const DeploymentFinalizedEvent = z.object({
593624
type: z.literal("finalized"),
594625
data: z.object({

0 commit comments

Comments
 (0)