(dockerfile: Dockerfile, buildArgs: Record<string, string>, baseImageEnv: Record<string, string>, globalBuildxPlatformArgs: Record<string, string> = {}, variable: string, stage: { from?: From; instructions: Instruction[] }, beforeInstructionIndex: number)
| 179 | } |
| 180 | |
| 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 { |
| 182 | let considerArg = true; |
| 183 | const seen = new Set<typeof stage>(); |
| 184 | while (true) { |
| 185 | if (seen.has(stage)) { |
| 186 | return undefined; |
| 187 | } |
| 188 | seen.add(stage); |
| 189 | |
| 190 | const i = findLastIndex(stage.instructions, i => i.name === variable && (i.instruction === 'ENV' || (considerArg && typeof (buildArgs[i.name] ?? i.value) === 'string')), beforeInstructionIndex - 1); |
| 191 | if (i !== -1) { |
| 192 | const instruction = stage.instructions[i]; |
| 193 | if (instruction.instruction === 'ENV') { |
| 194 | return replaceVariables(dockerfile, buildArgs, baseImageEnv, globalBuildxPlatformArgs, instruction.value!, stage, i); |
| 195 | } |
| 196 | if (instruction.instruction === 'ARG') { |
| 197 | return replaceVariables(dockerfile, buildArgs, baseImageEnv, globalBuildxPlatformArgs, buildArgs[instruction.name] ?? instruction.value, stage, i); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (!stage.from) { |
| 202 | const value = baseImageEnv[variable] ?? globalBuildxPlatformArgs[variable]; |
| 203 | if (typeof value === 'string') { |
| 204 | return value; |
| 205 | } |
| 206 | return undefined; |
| 207 | } |
| 208 | |
| 209 | const image = replaceVariables(dockerfile, buildArgs, baseImageEnv, globalBuildxPlatformArgs, stage.from.image, dockerfile.preamble, dockerfile.preamble.instructions.length); |
| 210 | stage = dockerfile.stagesByLabel[image] || dockerfile.preamble; |
| 211 | beforeInstructionIndex = stage.instructions.length; |
| 212 | considerArg = stage === dockerfile.preamble; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | function findLastIndex<T>(array: T[], predicate: (value: T, index: number, obj: T[]) => boolean, position = array.length - 1): number { |
| 217 | for (let i = position; i >= 0; i--) { |
no test coverage detected