Skip to content

Commit a9d4443

Browse files
Update to add suffix to version
1 parent 2f4f38b commit a9d4443

9 files changed

Lines changed: 67 additions & 34 deletions

File tree

apps/webapp/app/env.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ const EnvironmentSchema = z.object({
239239
DEPLOY_REGISTRY_ECR_ASSUME_ROLE_ARN: z.string().optional(),
240240
DEPLOY_REGISTRY_ECR_ASSUME_ROLE_EXTERNAL_ID: z.string().optional(),
241241
DEPLOY_IMAGE_PLATFORM: z.string().default("linux/amd64"),
242+
DEPLOY_VERSION_SUFFIX: z.string().optional(), // Optional suffix for deployment versions, e.g., "hardened"
242243
DEPLOY_TIMEOUT_MS: z.coerce
243244
.number()
244245
.int()

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ WHERE
137137
wd."projectId" = ${project.id}
138138
AND wd."environmentId" = ${environment.id}
139139
ORDER BY
140-
string_to_array(wd."version", '.')::int[] DESC
140+
string_to_array(split_part(wd."version", '-', 1), '.')::int[] DESC,
141+
split_part(wd."version", '-', 2) DESC
141142
LIMIT ${pageSize} OFFSET ${pageSize * (page - 1)};`;
142143

143144
return {
@@ -224,7 +225,13 @@ LIMIT ${pageSize} OFFSET ${pageSize * (page - 1)};`;
224225
FROM ${sqlDatabaseSchema}."WorkerDeployment"
225226
WHERE "projectId" = ${project.id}
226227
AND "environmentId" = ${environment.id}
227-
AND string_to_array(version, '.')::int[] > string_to_array(${version}, '.')::int[]
228+
AND (
229+
string_to_array(split_part(version, '-', 1), '.')::int[] > string_to_array(split_part(${version}, '-', 1), '.')::int[]
230+
OR (
231+
string_to_array(split_part(version, '-', 1), '.')::int[] = string_to_array(split_part(${version}, '-', 1), '.')::int[]
232+
AND split_part(version, '-', 2) > split_part(${version}, '-', 2)
233+
)
234+
)
228235
`;
229236

230237
const count = Number(deploymentsSinceVersion[0].count);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export class TestPresenter extends BasePresenter {
4545
>`WITH workers AS (
4646
SELECT
4747
bw.*,
48-
ROW_NUMBER() OVER(ORDER BY string_to_array(bw.version, '.')::int[] DESC) AS rn
48+
ROW_NUMBER() OVER(
49+
ORDER BY
50+
string_to_array(split_part(bw.version, '-', 1), '.')::int[] DESC,
51+
split_part(bw.version, '-', 2) DESC
52+
) AS rn
4953
FROM
5054
${sqlDatabaseSchema}."BackgroundWorker" bw
5155
WHERE "runtimeEnvironmentId" = ${envId}

apps/webapp/app/v3/marqs/devQueueConsumer.server.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { FailedTaskRunService } from "../failedTaskRun.server";
2222
import { CancelDevSessionRunsService } from "../services/cancelDevSessionRuns.server";
2323
import { CompleteAttemptService } from "../services/completeAttempt.server";
2424
import { attributesFromAuthenticatedEnv, tracer } from "../tracer.server";
25+
import { compareDeploymentVersions } from "../utils/deploymentVersions";
2526
import { DevSubscriber, devPubSub } from "./devPubSub.server";
2627

2728
const MessageBody = z.discriminatedUnion("type", [
@@ -589,7 +590,7 @@ export class DevQueueConsumer {
589590
}
590591

591592
// Get the latest background worker based on the version.
592-
// Versions are in the format of 20240101.1 and 20240101.2, or even 20240101.10, 20240101.11, etc.
593+
// Versions are in the format of YYYYMMDD.N (e.g., 20240101.1) with optional suffix (e.g., 20240101.1-hardened)
593594
#getLatestBackgroundWorker() {
594595
const workers = Array.from(this._backgroundWorkers.values());
595596

@@ -598,22 +599,10 @@ export class DevQueueConsumer {
598599
}
599600

600601
return workers.reduce((acc, curr) => {
601-
const accParts = acc.version.split(".").map(Number);
602-
const currParts = curr.version.split(".").map(Number);
603-
604-
// Compare the major part
605-
if (accParts[0] < currParts[0]) {
606-
return curr;
607-
} else if (accParts[0] > currParts[0]) {
608-
return acc;
609-
}
610-
611-
// Compare the minor part (assuming all versions have two parts)
612-
if (accParts[1] < currParts[1]) {
613-
return curr;
614-
} else {
615-
return acc;
616-
}
602+
// Use compareDeploymentVersions to properly handle versions with suffixes
603+
const comparison = compareDeploymentVersions(acc.version, curr.version);
604+
// If curr is newer (comparison returns -1), use curr, otherwise use acc
605+
return comparison < 0 ? curr : acc;
617606
});
618607
}
619608
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { BackgroundWorkerId } from "@trigger.dev/core/v3/isomorphic";
99
import type { BackgroundWorker, TaskQueue, TaskQueueType } from "@trigger.dev/database";
1010
import cronstrue from "cronstrue";
1111
import { Prisma, PrismaClientOrTransaction } from "~/db.server";
12+
import { env } from "~/env.server";
1213
import { sanitizeQueueName } from "~/models/taskQueue.server";
1314
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
1415
import { logger } from "~/services/logger.server";
@@ -64,7 +65,7 @@ export class CreateBackgroundWorkerService extends BaseService {
6465
return latestBackgroundWorker;
6566
}
6667

67-
const nextVersion = calculateNextBuildVersion(project.backgroundWorkers[0]?.version);
68+
const nextVersion = calculateNextBuildVersion(project.backgroundWorkers[0]?.version, env.DEPLOY_VERSION_SUFFIX);
6869

6970
logger.debug(`Creating background worker`, {
7071
nextVersion,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class InitializeDeploymentService extends BaseService {
4545
take: 1,
4646
});
4747

48-
const nextVersion = calculateNextBuildVersion(latestDeployment?.version);
48+
const nextVersion = calculateNextBuildVersion(latestDeployment?.version, env.DEPLOY_VERSION_SUFFIX);
4949

5050
if (payload.selfHosted && remoteBuildsEnabled()) {
5151
throw new ServiceValidationError(
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
// Calculate next build version based on the previous version
22
// Version formats are YYYYMMDD.1, YYYYMMDD.2, etc.
3+
// With optional suffix: YYYYMMDD.1-suffix
34
// If there is no previous version, start at Todays date and .1
4-
export function calculateNextBuildVersion(latestVersion?: string | null): string {
5+
export function calculateNextBuildVersion(latestVersion?: string | null, suffix?: string): string {
56
const today = new Date();
67
const year = today.getFullYear();
78
const month = today.getMonth() + 1;
89
const day = today.getDate();
910
const todayFormatted = `${year}${month < 10 ? "0" : ""}${month}${day < 10 ? "0" : ""}${day}`;
1011

1112
if (!latestVersion) {
12-
return `${todayFormatted}.1`;
13+
const baseVersion = `${todayFormatted}.1`;
14+
return suffix ? `${baseVersion}-${suffix}` : baseVersion;
1315
}
1416

15-
const [date, buildNumber] = latestVersion.split(".");
17+
// Extract base version and suffix from latest version
18+
const [baseVersion, existingSuffix] = latestVersion.split("-");
19+
const [date, buildNumber] = baseVersion.split(".");
1620

1721
if (date === todayFormatted) {
1822
const nextBuildNumber = parseInt(buildNumber, 10) + 1;
19-
return `${date}.${nextBuildNumber}`;
23+
const newBaseVersion = `${date}.${nextBuildNumber}`;
24+
return suffix ? `${newBaseVersion}-${suffix}` : newBaseVersion;
2025
}
2126

22-
return `${todayFormatted}.1`;
27+
const newBaseVersion = `${todayFormatted}.1`;
28+
return suffix ? `${newBaseVersion}-${suffix}` : newBaseVersion;
2329
}

apps/webapp/app/v3/utils/deploymentVersions.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Compares two versions of a deployment, like 20250208.1 and 20250208.2
2+
// Also handles versions with suffixes like 20250208.1-hardened
23
// Returns -1 if versionA is older than versionB, 0 if they are the same, and 1 if versionA is newer than versionB
34
export function compareDeploymentVersions(versionA: string, versionB: string) {
4-
const [dateA, numberA] = versionA.split(".");
5-
const [dateB, numberB] = versionB.split(".");
5+
// Extract base versions and suffixes
6+
const [baseVersionA, suffixA = ""] = versionA.split("-");
7+
const [baseVersionB, suffixB = ""] = versionB.split("-");
8+
9+
const [dateA, numberA] = baseVersionA.split(".");
10+
const [dateB, numberB] = baseVersionB.split(".");
611

712
if (dateA < dateB) {
813
return -1;
@@ -24,5 +29,20 @@ export function compareDeploymentVersions(versionA: string, versionB: string) {
2429
return 1;
2530
}
2631

32+
// Base versions are equal, compare suffixes alphabetically
33+
// Versions without suffixes should come before versions with suffixes
34+
if (suffixA === "" && suffixB !== "") {
35+
return -1;
36+
}
37+
if (suffixA !== "" && suffixB === "") {
38+
return 1;
39+
}
40+
if (suffixA < suffixB) {
41+
return -1;
42+
}
43+
if (suffixA > suffixB) {
44+
return 1;
45+
}
46+
2747
return 0;
2848
}

internal-packages/run-engine/src/engine/tests/setup.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,23 +292,28 @@ export async function setupBackgroundWorker(
292292
};
293293
}
294294

295-
function calculateNextBuildVersion(latestVersion?: string | null): string {
295+
function calculateNextBuildVersion(latestVersion?: string | null, suffix?: string): string {
296296
const today = new Date();
297297
const year = today.getFullYear();
298298
const month = today.getMonth() + 1;
299299
const day = today.getDate();
300300
const todayFormatted = `${year}${month < 10 ? "0" : ""}${month}${day < 10 ? "0" : ""}${day}`;
301301

302302
if (!latestVersion) {
303-
return `${todayFormatted}.1`;
303+
const baseVersion = `${todayFormatted}.1`;
304+
return suffix ? `${baseVersion}-${suffix}` : baseVersion;
304305
}
305306

306-
const [date, buildNumber] = latestVersion.split(".");
307+
// Extract base version and suffix from latest version
308+
const [baseVersion, existingSuffix] = latestVersion.split("-");
309+
const [date, buildNumber] = baseVersion.split(".");
307310

308311
if (date === todayFormatted) {
309312
const nextBuildNumber = parseInt(buildNumber, 10) + 1;
310-
return `${date}.${nextBuildNumber}`;
313+
const newBaseVersion = `${date}.${nextBuildNumber}`;
314+
return suffix ? `${newBaseVersion}-${suffix}` : newBaseVersion;
311315
}
312316

313-
return `${todayFormatted}.1`;
317+
const newBaseVersion = `${todayFormatted}.1`;
318+
return suffix ? `${newBaseVersion}-${suffix}` : newBaseVersion;
314319
}

0 commit comments

Comments
 (0)