Skip to content

Commit

Permalink
fix: arg stated in a FROM clause can reference other args (#1800)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinleturc authored and rohanKanojia committed Jul 20, 2024
1 parent e980189 commit 7971f73
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Automatically create parent directories of portPropertyFile path
- Added support for `platform` attribute of a container in the docker-compose configuration.
- `docker:push` failed with build `ARG` in `FROM` ([1778](https://github.com/fabric8io/docker-maven-plugin/issues/1778))
- `FROM` can reference `ARG` that references other `ARG` ([1800](https://github.com/fabric8io/docker-maven-plugin/issues/1800))

* **0.44.0** (2024-02-17):
- Add new option "useDefaultExclusion" for build configuration to handle exclusion of hidden files ([1708](https://github.com/fabric8io/docker-maven-plugin/issues/1708))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ static String resolveImageTagFromArgs(String imageTagString, Map<String, String>
String resolvedImageString = imageTagString;
Set<String> foundArgs = findAllArgs(imageTagString);
for (String foundArg : foundArgs) {
// check if the arg referenced in imageTagString exists in the Dockerfile
if (args.containsKey(foundArg)) {
resolvedImageString = resolvedImageString.replaceFirst(String.format("\\$\\{*%s\\}*", foundArg),
args.get(foundArg));
// resolve args that are referenced by this arg
String arg = resolveImageTagFromArgs(args.get(foundArg), args);
// replace the arg by its value
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 7971f73

Please sign in to comment.