Skip to content

Commit 513dc66

Browse files
committed
If provided, attach to an existing deployment in the deploy command
1 parent 4d0921e commit 513dc66

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

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

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { intro, log, outro } from "@clack/prompts";
22
import { getBranch, prepareDeploymentError, tryCatch } from "@trigger.dev/core/v3";
3-
import { InitializeDeploymentResponseBody } from "@trigger.dev/core/v3/schemas";
3+
import {
4+
InitializeDeploymentRequestBody,
5+
InitializeDeploymentResponseBody,
6+
} from "@trigger.dev/core/v3/schemas";
47
import { Command, Option as CommandOption } from "commander";
58
import { resolve } from "node:path";
69
import { isCI } from "std-env";
@@ -306,19 +309,17 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
306309
return;
307310
}
308311

309-
const deploymentResponse = await projectClient.client.initializeDeployment({
310-
contentHash: buildManifest.contentHash,
311-
userId: authorization.auth.tokenType === "personal" ? authorization.userId : undefined,
312-
gitMeta,
313-
type: features.run_engine_v2 ? "MANAGED" : "V1",
314-
runtime: buildManifest.runtime,
315-
});
316-
317-
if (!deploymentResponse.success) {
318-
throw new Error(`Failed to start deployment: ${deploymentResponse.error}`);
319-
}
320-
321-
const deployment = deploymentResponse.data;
312+
const deployment = await initializeOrAttachDeployment(
313+
projectClient.client,
314+
{
315+
contentHash: buildManifest.contentHash,
316+
userId: authorization.auth.tokenType === "personal" ? authorization.userId : undefined,
317+
gitMeta,
318+
type: features.run_engine_v2 ? "MANAGED" : "V1",
319+
runtime: buildManifest.runtime,
320+
},
321+
envVars.TRIGGER_EXISTING_DEPLOYMENT_ID
322+
);
322323
const isLocalBuild = !deployment.externalBuildData;
323324

324325
// Fail fast if we know local builds will fail
@@ -619,7 +620,7 @@ export async function syncEnvVarsWithServer(
619620

620621
async function failDeploy(
621622
client: CliApiClient,
622-
deployment: Deployment,
623+
deployment: Pick<Deployment, "id" | "shortCode">,
623624
error: { name: string; message: string },
624625
logs: string,
625626
$spinner: ReturnType<typeof spinner>,
@@ -735,6 +736,51 @@ async function failDeploy(
735736
}
736737
}
737738

739+
async function initializeOrAttachDeployment(
740+
apiClient: CliApiClient,
741+
data: InitializeDeploymentRequestBody,
742+
existingDeploymentId?: string
743+
): Promise<InitializeDeploymentResponseBody> {
744+
if (existingDeploymentId) {
745+
// In the build server we initialize the deployment before installing the project dependencies,
746+
// so that the status is correctly reflected in the dashboard. In this case, we need to attach
747+
// to the existing deployment and continue with the remote build process.
748+
// This is a workaround to avoid major changes in the deploy command and workflow. In the future,
749+
// we'll likely make the build server the entry point of the flow for building and deploying and also
750+
// adapt the related deployment API endpoints.
751+
752+
const existingDeploymentOrError = await apiClient.getDeployment(existingDeploymentId);
753+
754+
if (!existingDeploymentOrError.success) {
755+
throw new Error(
756+
`Failed to attach to existing deployment: ${existingDeploymentOrError.error}`
757+
);
758+
}
759+
760+
const { imageReference } = existingDeploymentOrError.data;
761+
if (!imageReference) {
762+
// this is just an artifact of our current DB schema
763+
// `imageReference` is stored as nullable, but it should always exist
764+
throw new Error("Existing deployment does not have an image reference");
765+
}
766+
767+
return {
768+
...existingDeploymentOrError.data,
769+
imageTag: imageReference,
770+
};
771+
}
772+
773+
const newDeploymentOrError = await apiClient.initializeDeployment({
774+
...data,
775+
});
776+
777+
if (!newDeploymentOrError.success) {
778+
throw new Error(`Failed to start deployment: ${newDeploymentOrError.error}`);
779+
}
780+
781+
return newDeploymentOrError.data;
782+
}
783+
738784
export function verifyDirectory(dir: string, projectPath: string) {
739785
if (dir !== "." && !isDirectory(projectPath)) {
740786
if (dir === "staging" || dir === "prod" || dir === "preview") {

0 commit comments

Comments
 (0)