Skip to content

Commit a670816

Browse files
committed
chore: misc fixes and refactor
1 parent 14f1005 commit a670816

File tree

2 files changed

+110
-35
lines changed

2 files changed

+110
-35
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const DeployCommandOptions = CommonCommandOptions.extend({
7171
saveLogs: z.boolean().default(false),
7272
skipUpdateCheck: z.boolean().default(false),
7373
skipPromotion: z.boolean().default(false),
74-
noCache: z.boolean().default(false),
74+
cache: z.boolean().default(true),
7575
envFile: z.string().optional(),
7676
// Local build options
7777
forceLocalBuild: z.boolean().optional(),
@@ -83,9 +83,10 @@ const DeployCommandOptions = CommonCommandOptions.extend({
8383
nativeBuildServer: z.boolean().default(false),
8484
detach: z.boolean().default(false),
8585
plain: z.boolean().default(false),
86-
useZstd: z.boolean().default(true),
87-
useZstdCache: z.boolean().default(true),
86+
compression: z.enum(["zstd", "gzip"]).default("zstd"),
87+
cacheCompression: z.enum(["zstd", "gzip"]).default("zstd"),
8888
compressionLevel: z.number().optional(),
89+
forceCompression: z.boolean().default(true),
8990
});
9091

9192
type DeployCommandOptions = z.infer<typeof DeployCommandOptions>;
@@ -162,20 +163,36 @@ export function configureDeployCommand(program: Command) {
162163
)
163164
.addOption(
164165
new CommandOption(
165-
"--use-zstd",
166-
"Use zstd compression when building the image. This will reduce image size and speed up pull times."
166+
"--compression <algorithm>",
167+
"Compression algorithm for image layers: zstd or gzip (default: zstd)"
168+
)
169+
.choices(["zstd", "gzip"])
170+
.hideHelp()
171+
)
172+
.addOption(
173+
new CommandOption(
174+
"--cache-compression <algorithm>",
175+
"Compression algorithm for build cache: zstd or gzip (default: zstd)"
176+
)
177+
.choices(["zstd", "gzip"])
178+
.hideHelp()
179+
)
180+
.addOption(
181+
new CommandOption(
182+
"--compression-level <level>",
183+
"The compression level to use when building the image."
167184
).hideHelp()
168185
)
169186
.addOption(
170187
new CommandOption(
171-
"--use-zstd-cache",
172-
"Use zstd compression for the build cache. This will reduce cache size and speed up build times."
188+
"--force-compression",
189+
"Force recompression of all layers. Enabled by default when using zstd."
173190
).hideHelp()
174191
)
175192
.addOption(
176193
new CommandOption(
177-
"--compression-level <level>",
178-
"The compression level to use when building the image. The default is 3."
194+
"--no-force-compression",
195+
"Disable forced recompression of layers."
179196
).hideHelp()
180197
)
181198
// Local build options
@@ -501,7 +518,7 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
501518
const buildResult = await buildImage({
502519
isLocalBuild,
503520
useRegistryCache: options.useRegistryCache,
504-
noCache: options.noCache,
521+
noCache: !options.cache,
505522
deploymentId: deployment.id,
506523
deploymentVersion: deployment.version,
507524
imageTag: deployment.imageTag,
@@ -520,9 +537,10 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
520537
authAccessToken: authorization.auth.accessToken,
521538
compilationPath: destination.path,
522539
buildEnvVars: buildManifest.build.env,
523-
useZstd: options.useZstd,
524-
useZstdCache: options.useZstdCache,
540+
compression: options.compression,
541+
cacheCompression: options.cacheCompression,
525542
compressionLevel: options.compressionLevel,
543+
forceCompression: options.forceCompression,
526544
onLog: (logMessage) => {
527545
if (options.plain || isCI) {
528546
console.log(logMessage);

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

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export interface BuildImageOptions {
2020
imagePlatform: string;
2121
noCache?: boolean;
2222
load?: boolean;
23-
useZstd?: boolean;
24-
useZstdCache?: boolean;
23+
compression?: "zstd" | "gzip";
24+
cacheCompression?: "zstd" | "gzip";
2525
compressionLevel?: number;
26+
forceCompression?: boolean;
2627

2728
// Local build options
2829
push?: boolean;
@@ -82,9 +83,10 @@ export async function buildImage(options: BuildImageOptions): Promise<BuildImage
8283
buildEnvVars,
8384
network,
8485
builder,
85-
useZstd,
86-
useZstdCache,
86+
compression,
87+
cacheCompression,
8788
compressionLevel,
89+
forceCompression,
8890
onLog,
8991
} = options;
9092

@@ -111,9 +113,10 @@ export async function buildImage(options: BuildImageOptions): Promise<BuildImage
111113
buildEnvVars,
112114
network,
113115
builder,
114-
useZstd,
115-
useZstdCache,
116+
compression,
117+
cacheCompression,
116118
compressionLevel,
119+
forceCompression,
117120
onLog,
118121
});
119122
}
@@ -143,8 +146,9 @@ export async function buildImage(options: BuildImageOptions): Promise<BuildImage
143146
apiKey,
144147
branchName,
145148
buildEnvVars,
146-
useZstd,
149+
compression,
147150
compressionLevel,
151+
forceCompression,
148152
onLog,
149153
});
150154
}
@@ -168,8 +172,9 @@ export interface DepotBuildImageOptions {
168172
noCache?: boolean;
169173
extraCACerts?: string;
170174
buildEnvVars?: Record<string, string | undefined>;
171-
useZstd?: boolean;
175+
compression?: "zstd" | "gzip";
172176
compressionLevel?: number;
177+
forceCompression?: boolean;
173178
onLog?: (log: string) => void;
174179
}
175180

@@ -193,17 +198,19 @@ async function remoteBuildImage(options: DepotBuildImageOptions): Promise<BuildI
193198
.filter(([key, value]) => value)
194199
.flatMap(([key, value]) => ["--build-arg", `${key}=${value}`]);
195200

201+
const outputOptions = getOutputOptions({
202+
imageTag: undefined, // This is already handled via the --save flag
203+
push: true, // We always push the image to the registry
204+
compression: options.compression,
205+
compressionLevel: options.compressionLevel,
206+
forceCompression: options.forceCompression,
207+
});
208+
196209
const args = [
197210
"build",
198211
"-f",
199212
"Containerfile",
200213
options.noCache ? "--no-cache" : undefined,
201-
...(options.useZstd
202-
? [
203-
"--output",
204-
`compression=zstd${options.compressionLevel ? `,level=${options.compressionLevel}` : ""}`,
205-
]
206-
: []),
207214
"--platform",
208215
options.imagePlatform,
209216
options.load ? "--load" : undefined,
@@ -233,6 +240,8 @@ async function remoteBuildImage(options: DepotBuildImageOptions): Promise<BuildI
233240
"plain",
234241
".",
235242
"--save",
243+
"--output",
244+
outputOptions.join(","),
236245
].filter(Boolean) as string[];
237246

238247
logger.debug(`depot ${args.join(" ")}`, { cwd: options.cwd });
@@ -335,9 +344,10 @@ interface SelfHostedBuildImageOptions {
335344
network?: string;
336345
builder: string;
337346
load?: boolean;
338-
useZstd?: boolean;
339-
useZstdCache?: boolean;
347+
compression?: "zstd" | "gzip";
348+
cacheCompression?: "zstd" | "gzip";
340349
compressionLevel?: number;
350+
forceCompression?: boolean;
341351
onLog?: (log: string) => void;
342352
}
343353

@@ -348,9 +358,10 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
348358
deploymentId,
349359
apiClient,
350360
useRegistryCache,
351-
useZstd,
352-
useZstdCache,
361+
compression,
362+
cacheCompression,
353363
compressionLevel,
364+
forceCompression,
354365
} = options;
355366

356367
// Ensure multi-platform build is supported on the local machine
@@ -520,6 +531,14 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
520531

521532
const projectCacheRef = getProjectCacheRefFromImageTag(imageTag);
522533

534+
const outputOptions = getOutputOptions({
535+
imageTag,
536+
push,
537+
compression,
538+
compressionLevel,
539+
forceCompression,
540+
});
541+
523542
const args = [
524543
"buildx",
525544
"build",
@@ -532,20 +551,18 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
532551
? [
533552
"--cache-to",
534553
`type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=${projectCacheRef}${
535-
useZstdCache ? ",compression=zstd" : ""
554+
cacheCompression === "zstd" ? ",compression=zstd" : ""
536555
}`,
537556
"--cache-from",
538557
`type=registry,ref=${projectCacheRef}`,
539558
]
540559
: []),
541-
...(useZstd
542-
? ["--output", `compression=zstd${compressionLevel ? `,level=${compressionLevel}` : ""}`]
543-
: []),
560+
"--output",
561+
outputOptions.join(","),
544562
"--platform",
545563
options.imagePlatform,
546564
options.network ? `--network=${options.network}` : undefined,
547565
addHost ? `--add-host=${addHost}` : undefined,
548-
push ? "--push" : undefined,
549566
load ? "--load" : undefined,
550567
"--provenance",
551568
"false",
@@ -1108,3 +1125,43 @@ function shouldLoad(load?: boolean, push?: boolean) {
11081125
}
11091126
}
11101127
}
1128+
1129+
function getOutputOptions({
1130+
imageTag,
1131+
push,
1132+
compression,
1133+
compressionLevel,
1134+
forceCompression,
1135+
}: {
1136+
imageTag?: string;
1137+
push?: boolean;
1138+
compression?: "zstd" | "gzip";
1139+
compressionLevel?: number;
1140+
forceCompression?: boolean;
1141+
}): string[] {
1142+
// Always use OCI media types for compatibility
1143+
const outputOptions: string[] = ["type=image", "oci-mediatypes=true"];
1144+
1145+
if (imageTag) {
1146+
outputOptions.push(`name=${imageTag}`);
1147+
}
1148+
1149+
if (push) {
1150+
outputOptions.push("push=true");
1151+
}
1152+
1153+
// Only add compression args when using zstd (gzip is the default, no args needed)
1154+
if (compression === "zstd") {
1155+
outputOptions.push("compression=zstd");
1156+
1157+
if (compressionLevel !== undefined) {
1158+
outputOptions.push(`compression-level=${compressionLevel}`);
1159+
}
1160+
}
1161+
1162+
if (forceCompression) {
1163+
outputOptions.push("force-compression=true");
1164+
}
1165+
1166+
return outputOptions;
1167+
}

0 commit comments

Comments
 (0)