Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: arg stated in a FROM clause can reference other args (#1800) #1801

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
Loading