-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathchangeCurrentDeployment.server.ts
More file actions
98 lines (91 loc) · 3.12 KB
/
changeCurrentDeployment.server.ts
File metadata and controls
98 lines (91 loc) · 3.12 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { WorkerDeployment } from "@trigger.dev/database";
import { BaseService, ServiceValidationError } from "./baseService.server";
import { compareDeploymentVersions } from "../utils/deploymentVersions";
import { CURRENT_DEPLOYMENT_LABEL } from "@trigger.dev/core/v3/isomorphic";
export type ChangeCurrentDeploymentDirection = "promote" | "rollback";
export class ChangeCurrentDeploymentService extends BaseService {
public async call(
deployment: WorkerDeployment,
direction: ChangeCurrentDeploymentDirection,
disableVersionCheck?: boolean
) {
if (!deployment.workerId) {
throw new ServiceValidationError(
direction === "promote"
? "Deployment is not associated with a worker and cannot be promoted."
: "Deployment is not associated with a worker and cannot be rolled back."
);
}
if (deployment.status !== "DEPLOYED") {
throw new ServiceValidationError(
direction === "promote"
? "Deployment must be in the DEPLOYED state to be promoted."
: "Deployment must be in the DEPLOYED state to be rolled back."
);
}
const currentPromotion = await this._prisma.workerDeploymentPromotion.findFirst({
where: {
environmentId: deployment.environmentId,
label: CURRENT_DEPLOYMENT_LABEL,
},
select: {
deployment: {
select: { id: true, version: true },
},
},
});
if (currentPromotion) {
if (currentPromotion.deployment.id === deployment.id) {
throw new ServiceValidationError("Deployment is already the current deployment.");
}
// if there is a current promotion, we have to validate we are moving in the right direction based on the deployment versions
if (!disableVersionCheck) {
switch (direction) {
case "promote": {
if (
compareDeploymentVersions(
currentPromotion.deployment.version,
deployment.version
) >= 0
) {
throw new ServiceValidationError(
"Cannot promote a deployment that is older than the current deployment."
);
}
break;
}
case "rollback": {
if (
compareDeploymentVersions(
currentPromotion.deployment.version,
deployment.version
) <= 0
) {
throw new ServiceValidationError(
"Cannot rollback to a deployment that is newer than the current deployment."
);
}
break;
}
}
}
}
//set this deployment as the current deployment for this environment
await this._prisma.workerDeploymentPromotion.upsert({
where: {
environmentId_label: {
environmentId: deployment.environmentId,
label: CURRENT_DEPLOYMENT_LABEL,
},
},
create: {
deploymentId: deployment.id,
environmentId: deployment.environmentId,
label: CURRENT_DEPLOYMENT_LABEL,
},
update: {
deploymentId: deployment.id,
},
});
}
}