@@ -16,6 +16,7 @@ import { CliApiClient } from "../apiClient.js";
1616export interface BuildImageOptions {
1717 // Common options
1818 isLocalBuild : boolean ;
19+ useRegistryCache ?: boolean ;
1920 imagePlatform : string ;
2021 noCache ?: boolean ;
2122 load ?: boolean ;
@@ -53,6 +54,7 @@ export interface BuildImageOptions {
5354export async function buildImage ( options : BuildImageOptions ) : Promise < BuildImageResults > {
5455 const {
5556 isLocalBuild,
57+ useRegistryCache,
5658 imagePlatform,
5759 noCache,
5860 push,
@@ -94,6 +96,7 @@ export async function buildImage(options: BuildImageOptions): Promise<BuildImage
9496 authenticateToRegistry,
9597 load,
9698 noCache,
99+ useRegistryCache,
97100 extraCACerts,
98101 apiUrl,
99102 apiKey,
@@ -307,6 +310,7 @@ interface SelfHostedBuildImageOptions {
307310 apiClient : CliApiClient ;
308311 branchName ?: string ;
309312 noCache ?: boolean ;
313+ useRegistryCache ?: boolean ;
310314 extraCACerts ?: string ;
311315 buildEnvVars ?: Record < string , string | undefined > ;
312316 network ?: string ;
@@ -316,7 +320,7 @@ interface SelfHostedBuildImageOptions {
316320}
317321
318322async function localBuildImage ( options : SelfHostedBuildImageOptions ) : Promise < BuildImageResults > {
319- const { builder, imageTag, deploymentId, apiClient } = options ;
323+ const { builder, imageTag, deploymentId, apiClient, useRegistryCache } = options ;
320324
321325 // Ensure multi-platform build is supported on the local machine
322326 let builderExists = false ;
@@ -483,6 +487,8 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
483487 options . onLog ?.( `Successfully logged in to ${ cloudRegistryHost } ` ) ;
484488 }
485489
490+ const projectCacheRef = getProjectCacheRefFromImageTag ( imageTag ) ;
491+
486492 const args = [
487493 "buildx" ,
488494 "build" ,
@@ -491,6 +497,14 @@ async function localBuildImage(options: SelfHostedBuildImageOptions): Promise<Bu
491497 "-f" ,
492498 "Containerfile" ,
493499 options . noCache ? "--no-cache" : undefined ,
500+ ...( useRegistryCache
501+ ? [
502+ "--cache-to" ,
503+ `type=registry,mode=max,image-manifest=true,oci-mediatypes=true,ref=${ projectCacheRef } ` ,
504+ "--cache-from" ,
505+ `type=registry,ref=${ projectCacheRef } ` ,
506+ ]
507+ : [ ] ) ,
494508 "--platform" ,
495509 options . imagePlatform ,
496510 options . network ? `--network=${ options . network } ` : undefined ,
@@ -928,6 +942,11 @@ function extractRegistryHostFromImageTag(imageTag: string): string | undefined {
928942 return host ;
929943}
930944
945+ function getProjectCacheRefFromImageTag ( imageTag : string ) : string {
946+ const lastColonIndex = imageTag . lastIndexOf ( ":" ) ;
947+ return `${ imageTag . substring ( 0 , lastColonIndex ) } :cache` ;
948+ }
949+
931950async function getDockerUsernameAndPassword (
932951 apiClient : CliApiClient ,
933952 deploymentId : string
0 commit comments