-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathremoteImageBuilder.server.ts
More file actions
66 lines (56 loc) · 1.56 KB
/
remoteImageBuilder.server.ts
File metadata and controls
66 lines (56 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { depot } from "@depot/sdk-node";
import { type ExternalBuildData } from "@trigger.dev/core/v3";
import { type Project } from "@trigger.dev/database";
import { prisma } from "~/db.server";
import { env } from "~/env.server";
export async function createRemoteImageBuild(
project: Project
): Promise<ExternalBuildData | undefined> {
if (!remoteBuildsEnabled()) {
return;
}
const builderProjectId = await createBuilderProjectIfNotExists(project);
const result = await depot.build.v1.BuildService.createBuild(
{ projectId: builderProjectId },
{
headers: {
Authorization: `Bearer ${env.DEPOT_TOKEN}`,
},
}
);
return {
projectId: builderProjectId,
buildToken: result.buildToken,
buildId: result.buildId,
};
}
async function createBuilderProjectIfNotExists(project: Project) {
if (project.builderProjectId) {
return project.builderProjectId;
}
const result = await depot.core.v1.ProjectService.createProject(
{
name: `${env.APP_ENV} ${project.externalRef}`,
organizationId: env.DEPOT_ORG_ID,
regionId: env.DEPOT_REGION,
},
{
headers: {
Authorization: `Bearer ${env.DEPOT_TOKEN}`,
},
}
);
if (!result.project) {
throw new Error("Failed to create builder project");
}
await prisma.project.update({
where: { id: project.id },
data: {
builderProjectId: result.project.projectId,
},
});
return result.project.projectId;
}
export function remoteBuildsEnabled() {
return env.DEPOT_TOKEN && env.DEPOT_ORG_ID && env.DEPOT_REGION;
}