Skip to content

Commit 9f19f50

Browse files
committed
Fix external build token generation issue
1 parent 921285c commit 9f19f50

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ export class DeploymentService extends BaseService {
110110
const createRemoteBuildIfNeeded = deployment.buildServerMetadata?.isNativeBuild
111111
? okAsync(undefined)
112112
: fromPromise(createRemoteImageBuild(authenticatedEnv.project), (error) => ({
113-
type: "failed_to_create_remote_build" as const,
114-
cause: error,
115-
}));
113+
type: "failed_to_create_remote_build" as const,
114+
cause: error,
115+
}));
116116

117117
const existingBuildServerMetadata = deployment.buildServerMetadata as
118118
| BuildServerMetadata
@@ -134,7 +134,7 @@ export class DeploymentService extends BaseService {
134134
},
135135
}
136136
: {}),
137-
externalBuildData,
137+
...(externalBuildData ? { externalBuildData } : {}),
138138
status: "BUILDING",
139139
installedAt: new Date(),
140140
},
@@ -496,14 +496,16 @@ export class DeploymentService extends BaseService {
496496
type: "other" as const,
497497
cause: error,
498498
})
499-
).andThen((deployment) => {
500-
if (!deployment) {
501-
return errAsync({ type: "deployment_not_found" as const });
502-
}
503-
return okAsync(deployment);
504-
}).map((deployment) => ({
505-
...deployment,
506-
buildServerMetadata: BuildServerMetadata.safeParse(deployment.buildServerMetadata).data,
507-
}));
499+
)
500+
.andThen((deployment) => {
501+
if (!deployment) {
502+
return errAsync({ type: "deployment_not_found" as const });
503+
}
504+
return okAsync(deployment);
505+
})
506+
.map((deployment) => ({
507+
...deployment,
508+
buildServerMetadata: BuildServerMetadata.safeParse(deployment.buildServerMetadata).data,
509+
}));
508510
}
509511
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,18 @@ export class InitializeDeploymentService extends BaseService {
8989
);
9090
}
9191

92-
// For the `PENDING` initial status, defer the creation of the Depot build until the deployment is started.
93-
// This helps avoid Depot token expiration issues.
94-
const externalBuildData =
95-
payload.initialStatus === "PENDING" || payload.isNativeBuild
96-
? undefined
97-
: await createRemoteImageBuild(environment.project);
92+
// For the `PENDING` initial status, defer the creation of the Depot build until the deployment is started to avoid token expiration issues.
93+
// For local and native builds we don't need to generate the Depot tokens. We still need to create an empty object sadly due to a bug in older CLI versions.
94+
const generateExternalBuildToken =
95+
payload.initialStatus === "PENDING" || payload.isNativeBuild || payload.isLocal;
96+
97+
const externalBuildData = generateExternalBuildToken
98+
? ({
99+
projectId: "-",
100+
buildToken: "-",
101+
buildId: "-",
102+
} satisfies ExternalBuildData)
103+
: await createRemoteImageBuild(environment.project);
98104

99105
const triggeredBy = payload.userId
100106
? await this._prisma.user.findFirst({

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ export function configureDeployCommand(program: Command) {
201201
localBuild: true,
202202
})
203203
)
204-
.addOption(new CommandOption("--local-build", "Build the deployment image locally"))
204+
.addOption(
205+
new CommandOption("--local-build", "Build the deployment image locally").conflicts(
206+
"nativeBuildServer"
207+
)
208+
)
205209
.addOption(new CommandOption("--push", "Push the image after local builds").hideHelp())
206210
.addOption(
207211
new CommandOption("--no-push", "Do not push the image after local builds").hideHelp()
@@ -420,14 +424,19 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
420424
gitMeta,
421425
type: features.run_engine_v2 ? "MANAGED" : "V1",
422426
runtime: buildManifest.runtime,
427+
isLocal: options.localBuild,
423428
isNativeBuild: false,
424429
triggeredVia: getTriggeredVia(),
425430
},
426431
envVars.TRIGGER_EXISTING_DEPLOYMENT_ID
427432
);
433+
434+
// When `externalBuildData` is not present the deployment implicitly goes into the local build path
435+
// which is used in self-hosted setups. There are a few subtle differences between local builds for the cloud
436+
// and local builds for self-hosted setups. We need to make the separation of the two paths clearer to avoid confusion.
428437
const isLocalBuild = options.localBuild || !deployment.externalBuildData;
429-
// Would be best to actually store this separately in the deployment object. This is an okay proxy for now.
430-
const remoteBuildExplicitlySkipped = options.localBuild && !!deployment.externalBuildData;
438+
const authenticateToTriggerRegistry = options.localBuild;
439+
const skipServerSideRegistryPush = options.localBuild;
431440

432441
// Fail fast if we know local builds will fail
433442
if (isLocalBuild) {
@@ -559,7 +568,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
559568
network: options.network,
560569
builder: options.builder,
561570
push: options.push,
562-
authenticateToRegistry: remoteBuildExplicitlySkipped,
571+
authenticateToRegistry: authenticateToTriggerRegistry,
563572
});
564573

565574
logger.debug("Build result", buildResult);
@@ -657,7 +666,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
657666
{
658667
imageDigest: buildResult.digest,
659668
skipPromotion: options.skipPromotion,
660-
skipPushToRegistry: remoteBuildExplicitlySkipped,
669+
skipPushToRegistry: skipServerSideRegistryPush,
661670
},
662671
(logMessage) => {
663672
if (options.plain || isCI) {

packages/core/src/v3/schemas/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ const InitializeDeploymentRequestBodyBase = z.object({
602602
runtime: z.string().optional(),
603603
initialStatus: z.enum(["PENDING", "BUILDING"]).optional(),
604604
triggeredVia: DeploymentTriggeredVia.optional(),
605-
buildId: z.string().optional()
605+
buildId: z.string().optional(),
606606
});
607607
type BaseOutput = z.output<typeof InitializeDeploymentRequestBodyBase>;
608608

@@ -624,6 +624,7 @@ type NonNativeBuildOutput = BaseOutput & {
624624

625625
const InitializeDeploymentRequestBodyFull = InitializeDeploymentRequestBodyBase.extend({
626626
isNativeBuild: z.boolean().default(false),
627+
isLocal: z.boolean().optional(),
627628
skipPromotion: z.boolean().optional(),
628629
artifactKey: z.string().optional(),
629630
configFilePath: z.string().optional(),

0 commit comments

Comments
 (0)