Skip to content

Commit

Permalink
fix: arg stated in a FROM clause can reference other args (fabric8io#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinleturc committed Jul 19, 2024
1 parent e980189 commit 9fe7d79
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ static String resolveImageTagFromArgs(String imageTagString, Map<String, String>
String resolvedImageString = imageTagString;
Set<String> foundArgs = findAllArgs(imageTagString);
for (String foundArg : foundArgs) {
// check if the template arg exists in the Dockerfile
if (args.containsKey(foundArg)) {
resolvedImageString = resolvedImageString.replaceFirst(String.format("\\$\\{*%s\\}*", foundArg),
args.get(foundArg));
// resolve args that are referenced in another arg
String arg = resolveImageTagFromArgs(args.get(foundArg), args);
resolvedImageString = resolvedImageString.replaceFirst(String.format("\\$\\{*%s\\}*", foundArg), arg);
}
}
return resolvedImageString;
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,35 @@ void testSimple() throws Exception {
toTest, FixedStringSearchInterpolator.create(), Collections.emptyMap()).get(0));
}

@Test
void testSimpleWithArgs() throws Exception {
File toTest = copyToTempDir("Dockerfile_from_simple_with_args");
// default arg value
Assertions.assertEquals("alpine:latest", DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create(), Collections.emptyMap()).get(0));
// given arg value
Assertions.assertEquals("alpine:3", DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create(), Collections.singletonMap("VERSION", "3")).get(0));
}

@Test
void testSimpleWithChainedArgs() throws Exception {
File toTest = copyToTempDir("Dockerfile_from_simple_with_chained_args");
// default arg value
Assertions.assertEquals("alpine:latest", DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create(), Collections.emptyMap()).get(0));
// given arg value
Assertions.assertEquals("archlinux:latest", DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create(), Collections.singletonMap("IMAGE_NAME", "archlinux")).get(0));
Assertions.assertEquals("alpine:3", DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create(), Collections.singletonMap("IMAGE_TAG", "3")).get(0));
Map<String, String> buildArgsFromConfig = new HashMap<>();
buildArgsFromConfig.put("IMAGE_NAME", "archlinux");
buildArgsFromConfig.put("IMAGE_TAG", "base-devel");
Assertions.assertEquals("archlinux:base-devel", DockerFileUtil.extractBaseImages(
toTest, FixedStringSearchInterpolator.create(), buildArgsFromConfig).get(0));
}

@Test
void testMultiStage() throws Exception {
File toTest = copyToTempDir("Dockerfile_multi_stage");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Dockerfile with a simple FROM clause with ARG
ARG VERSION=latest
FROM alpine:${VERSION}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dockerfile with a simple FROM clause with ARGs
ARG IMAGE_NAME=alpine
ARG IMAGE_TAG=latest
ARG FULL_IMAGE=${IMAGE_NAME}:${IMAGE_TAG}
FROM ${FULL_IMAGE}

0 comments on commit 9fe7d79

Please sign in to comment.