@@ -81,7 +81,7 @@ export function extractDockerfile(dockerfile: string): Dockerfile {
8181 } as Dockerfile ;
8282}
8383
84- export function findUserStatement ( dockerfile : Dockerfile , buildArgs : Record < string , string > , baseImageEnv : Record < string , string > , target : string | undefined ) {
84+ export function findUserStatement ( dockerfile : Dockerfile , buildArgs : Record < string , string > , baseImageEnv : Record < string , string > , globalBuildxPlatformArgs : Record < string , string > = { } , target : string | undefined ) {
8585 let stage : Stage | undefined = target ? dockerfile . stagesByLabel [ target ] : dockerfile . stages [ dockerfile . stages . length - 1 ] ;
8686 const seen = new Set < Stage > ( ) ;
8787 while ( stage ) {
@@ -92,15 +92,15 @@ export function findUserStatement(dockerfile: Dockerfile, buildArgs: Record<stri
9292
9393 const i = findLastIndex ( stage . instructions , i => i . instruction === 'USER' ) ;
9494 if ( i !== - 1 ) {
95- return replaceVariables ( dockerfile , buildArgs , baseImageEnv , stage . instructions [ i ] . name , stage , i ) || undefined ;
95+ return replaceVariables ( dockerfile , buildArgs , baseImageEnv , globalBuildxPlatformArgs , stage . instructions [ i ] . name , stage , i ) || undefined ;
9696 }
97- const image = replaceVariables ( dockerfile , buildArgs , baseImageEnv , stage . from . image , dockerfile . preamble , dockerfile . preamble . instructions . length ) ;
97+ const image = replaceVariables ( dockerfile , buildArgs , baseImageEnv , globalBuildxPlatformArgs , stage . from . image , dockerfile . preamble , dockerfile . preamble . instructions . length ) ;
9898 stage = dockerfile . stagesByLabel [ image ] ;
9999 }
100100 return undefined ;
101101}
102102
103- export function findBaseImage ( dockerfile : Dockerfile , buildArgs : Record < string , string > , target : string | undefined ) {
103+ export function findBaseImage ( dockerfile : Dockerfile , buildArgs : Record < string , string > , target : string | undefined , globalBuildxPlatformArgs : Record < string , string > = { } ) {
104104 let stage : Stage | undefined = target ? dockerfile . stagesByLabel [ target ] : dockerfile . stages [ dockerfile . stages . length - 1 ] ;
105105 const seen = new Set < Stage > ( ) ;
106106 while ( stage ) {
@@ -109,7 +109,7 @@ export function findBaseImage(dockerfile: Dockerfile, buildArgs: Record<string,
109109 }
110110 seen . add ( stage ) ;
111111
112- const image = replaceVariables ( dockerfile , buildArgs , /* not available in FROM instruction */ { } , stage . from . image , dockerfile . preamble , dockerfile . preamble . instructions . length ) ;
112+ const image = replaceVariables ( dockerfile , buildArgs , /* not available in FROM instruction */ { } , globalBuildxPlatformArgs , stage . from . image , dockerfile . preamble , dockerfile . preamble . instructions . length ) ;
113113 const nextStage = dockerfile . stagesByLabel [ image ] ;
114114 if ( ! nextStage ) {
115115 return image ;
@@ -155,12 +155,12 @@ function getExpressionValue(option: string, isSet: boolean, word: string, value:
155155 return operations [ option ] ( isSet , word , value ) . replace ( / ^ [ ' " ] | [ ' " ] $ / g, '' ) ; // remove quotes from start and end of the string
156156}
157157
158- function replaceVariables ( dockerfile : Dockerfile , buildArgs : Record < string , string > , baseImageEnv : Record < string , string > , str : string , stage : { from ?: From ; instructions : Instruction [ ] } , beforeInstructionIndex : number ) {
158+ function replaceVariables ( dockerfile : Dockerfile , buildArgs : Record < string , string > , baseImageEnv : Record < string , string > , globalBuildxPlatformArgs : Record < string , string > = { } , str : string , stage : { from ?: From ; instructions : Instruction [ ] } , beforeInstructionIndex : number ) {
159159 return [ ...str . matchAll ( argumentExpression ) ]
160160 . map ( match => {
161161 const variable = match . groups ! . variable ;
162162 const isVarExp = match . groups ! . isVarExp ? true : false ;
163- let value = findValue ( dockerfile , buildArgs , baseImageEnv , variable , stage , beforeInstructionIndex ) || '' ;
163+ let value = findValue ( dockerfile , buildArgs , baseImageEnv , globalBuildxPlatformArgs , variable , stage , beforeInstructionIndex ) || '' ;
164164 if ( isVarExp ) {
165165 // Handle replacing variable expressions (${var:+word}) if they exist
166166 const option = match . groups ! . option ;
@@ -178,7 +178,7 @@ function replaceVariables(dockerfile: Dockerfile, buildArgs: Record<string, stri
178178 . reduce ( ( str , { begin, end, value } ) => str . substring ( 0 , begin ) + value + str . substring ( end ) , str ) ;
179179}
180180
181- function findValue ( dockerfile : Dockerfile , buildArgs : Record < string , string > , baseImageEnv : Record < string , string > , variable : string , stage : { from ?: From ; instructions : Instruction [ ] } , beforeInstructionIndex : number ) : string | undefined {
181+ function findValue ( dockerfile : Dockerfile , buildArgs : Record < string , string > , baseImageEnv : Record < string , string > , globalBuildxPlatformArgs : Record < string , string > = { } , variable : string , stage : { from ?: From ; instructions : Instruction [ ] } , beforeInstructionIndex : number ) : string | undefined {
182182 let considerArg = true ;
183183 const seen = new Set < typeof stage > ( ) ;
184184 while ( true ) {
@@ -191,22 +191,22 @@ function findValue(dockerfile: Dockerfile, buildArgs: Record<string, string>, ba
191191 if ( i !== - 1 ) {
192192 const instruction = stage . instructions [ i ] ;
193193 if ( instruction . instruction === 'ENV' ) {
194- return replaceVariables ( dockerfile , buildArgs , baseImageEnv , instruction . value ! , stage , i ) ;
194+ return replaceVariables ( dockerfile , buildArgs , baseImageEnv , globalBuildxPlatformArgs , instruction . value ! , stage , i ) ;
195195 }
196196 if ( instruction . instruction === 'ARG' ) {
197- return replaceVariables ( dockerfile , buildArgs , baseImageEnv , buildArgs [ instruction . name ] ?? instruction . value , stage , i ) ;
197+ return replaceVariables ( dockerfile , buildArgs , baseImageEnv , globalBuildxPlatformArgs , buildArgs [ instruction . name ] ?? instruction . value , stage , i ) ;
198198 }
199199 }
200200
201201 if ( ! stage . from ) {
202- const value = baseImageEnv [ variable ] ;
202+ const value = baseImageEnv [ variable ] ?? globalBuildxPlatformArgs [ variable ] ;
203203 if ( typeof value === 'string' ) {
204204 return value ;
205205 }
206206 return undefined ;
207207 }
208208
209- const image = replaceVariables ( dockerfile , buildArgs , baseImageEnv , stage . from . image , dockerfile . preamble , dockerfile . preamble . instructions . length ) ;
209+ const image = replaceVariables ( dockerfile , buildArgs , baseImageEnv , globalBuildxPlatformArgs , stage . from . image , dockerfile . preamble , dockerfile . preamble . instructions . length ) ;
210210 stage = dockerfile . stagesByLabel [ image ] || dockerfile . preamble ;
211211 beforeInstructionIndex = stage . instructions . length ;
212212 considerArg = stage === dockerfile . preamble ;
0 commit comments