-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(deploy): --external-id and --force for deploy idempotency #4663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
0ski
merged 1 commit into
oskar/feat-external-id-api
from
oskar/feat-external-id-deploy
Aug 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "trigger.dev": patch | ||
| --- | ||
|
|
||
| `trigger.dev deploy --external-id` tags a deployment with an id of your own — a commit SHA, a CI run id, a release tag — so runs triggered by that release of your app go to that deployment. Deploying an id that is already deployed builds nothing and reports the existing version instead of creating a duplicate; use `--force` to rebuild it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
apps/webapp/app/v3/services/initializeDeployment/cancelSupersededDeployments.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { logger } from "~/services/logger.server"; | ||
| import { ServiceValidationError } from "~/v3/services/baseService.server"; | ||
| import type { DeploymentService } from "../deployment.server"; | ||
| import type { ExternalIdReuseDeployment } from "./resolveExternalIdReuse.server"; | ||
|
|
||
| export type SupersededDeployment = Pick<ExternalIdReuseDeployment, "version" | "shortCode">; | ||
|
|
||
| export function supersededByForceReason(externalId: string): string { | ||
| return `Superseded by a new deploy with --force for external id "${externalId}"`; | ||
| } | ||
|
|
||
| type CancelSupersededDeploymentsOptions = { | ||
| deploymentService: DeploymentService; | ||
| environmentId: string; | ||
| externalId: string; | ||
| deployments: ExternalIdReuseDeployment[]; | ||
| }; | ||
|
|
||
| export async function cancelSupersededDeployments({ | ||
| deploymentService, | ||
| environmentId, | ||
| externalId, | ||
| deployments, | ||
| }: CancelSupersededDeploymentsOptions): Promise<SupersededDeployment[]> { | ||
| const canceled: SupersededDeployment[] = []; | ||
|
|
||
| for (const deployment of deployments) { | ||
| const result = await deploymentService.cancelDeployment( | ||
| { id: environmentId }, | ||
| deployment.friendlyId, | ||
| { | ||
| canceledReason: supersededByForceReason(externalId), | ||
| } | ||
| ); | ||
|
|
||
| if (result.isOk()) { | ||
| canceled.push({ version: deployment.version, shortCode: deployment.shortCode }); | ||
| continue; | ||
| } | ||
|
|
||
| if ( | ||
| result.error.type === "deployment_cannot_be_cancelled" || | ||
| result.error.type === "deployment_not_found" | ||
| ) { | ||
| logger.debug("Superseded deployment was already final", { | ||
| externalId, | ||
| version: deployment.version, | ||
| reason: result.error.type, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| if (result.error.type === "failed_to_delete_deployment_timeout") { | ||
| logger.warn("Failed to dequeue the timeout job for a superseded deployment", { | ||
| externalId, | ||
| version: deployment.version, | ||
| error: result.error.cause, | ||
| }); | ||
| canceled.push({ version: deployment.version, shortCode: deployment.shortCode }); | ||
| continue; | ||
| } | ||
|
|
||
| throw new ServiceValidationError( | ||
| `Failed to cancel the in-progress deployment ${deployment.version} holding external id "${externalId}". Nothing was built — try again.`, | ||
| 500 | ||
| ); | ||
| } | ||
|
|
||
| return canceled; | ||
| } |
99 changes: 99 additions & 0 deletions
99
apps/webapp/app/v3/services/initializeDeployment/resolveExternalIdReuse.server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { CURRENT_DEPLOYMENT_LABEL } from "@trigger.dev/core/v3/isomorphic"; | ||
| import { type PrismaClientOrTransaction, type WorkerDeployment } from "@trigger.dev/database"; | ||
| import { compareDeploymentVersions } from "../../utils/deploymentVersions"; | ||
| import { FINAL_DEPLOYMENT_STATUSES } from "../failDeployment.server"; | ||
|
|
||
| const MAX_CANDIDATES = 20; | ||
|
|
||
| export type ExternalIdReuseDeployment = Pick< | ||
| WorkerDeployment, | ||
| | "id" | ||
| | "friendlyId" | ||
| | "shortCode" | ||
| | "version" | ||
| | "status" | ||
| | "contentHash" | ||
| | "imageReference" | ||
| | "imagePlatform" | ||
| | "externalId" | ||
| >; | ||
|
|
||
| type ExternalIdReuseCandidate = ExternalIdReuseDeployment & { promotions: { id: string }[] }; | ||
|
|
||
| export type ResolveExternalIdReuseResult = | ||
| | { action: "build" } | ||
| | { action: "short-circuit"; deployment: ExternalIdReuseDeployment; isPromoted: boolean } | ||
| | { action: "reject"; deployment: ExternalIdReuseDeployment } | ||
| | { | ||
| action: "cancel-then-build"; | ||
| externalId: string; | ||
| deployments: ExternalIdReuseDeployment[]; | ||
| }; | ||
|
|
||
| export type ResolveExternalIdReuseOptions = { | ||
| prisma: PrismaClientOrTransaction; | ||
| environmentId: string; | ||
| externalId?: string; | ||
| force?: boolean; | ||
| }; | ||
|
|
||
| export async function resolveExternalIdReuse({ | ||
| prisma, | ||
| environmentId, | ||
| externalId, | ||
| force, | ||
| }: ResolveExternalIdReuseOptions): Promise<ResolveExternalIdReuseResult> { | ||
| if (!externalId) { | ||
| return { action: "build" }; | ||
| } | ||
|
|
||
| const candidates = await prisma.workerDeployment.findMany({ | ||
| where: { environmentId, externalId }, | ||
| select: { | ||
| id: true, | ||
| friendlyId: true, | ||
| shortCode: true, | ||
| version: true, | ||
| status: true, | ||
| contentHash: true, | ||
| imageReference: true, | ||
| imagePlatform: true, | ||
| externalId: true, | ||
| promotions: { | ||
| where: { label: CURRENT_DEPLOYMENT_LABEL }, | ||
| select: { id: true }, | ||
| }, | ||
| }, | ||
| orderBy: { id: "desc" }, | ||
| take: MAX_CANDIDATES, | ||
| }); | ||
|
0ski marked this conversation as resolved.
|
||
|
|
||
| const inFlight = byVersionDesc( | ||
| candidates.filter((deployment) => !FINAL_DEPLOYMENT_STATUSES.includes(deployment.status)) | ||
| ); | ||
|
|
||
| if (force) { | ||
| return inFlight.length | ||
| ? { action: "cancel-then-build", externalId, deployments: inFlight } | ||
| : { action: "build" }; | ||
| } | ||
|
|
||
| if (inFlight.length) { | ||
| return { action: "reject", deployment: inFlight[0]! }; | ||
| } | ||
|
|
||
| const deployed = byVersionDesc( | ||
| candidates.filter((deployment) => deployment.status === "DEPLOYED") | ||
| ); | ||
|
|
||
| if (deployed.length) { | ||
| const deployment = deployed[0]!; | ||
| return { action: "short-circuit", deployment, isPromoted: deployment.promotions.length > 0 }; | ||
| } | ||
|
|
||
| return { action: "build" }; | ||
| } | ||
|
|
||
| function byVersionDesc(deployments: ExternalIdReuseCandidate[]): ExternalIdReuseCandidate[] { | ||
| return [...deployments].sort((a, b) => compareDeploymentVersions(b.version, a.version)); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.