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

resolves build args in FROM statements when using withDockerfile #3922

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.zip.GZIPOutputStream;

@Slf4j
Expand Down Expand Up @@ -167,15 +168,31 @@ private void prePullDependencyImages(Set<String> imagesToPull) {
final DockerClient dockerClient = DockerClientFactory.instance().client();

imagesToPull.forEach(imageName -> {
String resolvedImageName = resolveImageNameFromBuildArgs(imageName);
try {
log.info("Pre-emptively checking local images for '{}', referenced via a Dockerfile. If not available, it will be pulled.", imageName);
DockerClientFactory.instance().checkAndPullImage(dockerClient, imageName);
log.info("Pre-emptively checking local images for '{}', referenced via a Dockerfile. If not available, it will be pulled.", resolvedImageName);
DockerClientFactory.instance().checkAndPullImage(dockerClient, resolvedImageName);
} catch (Exception e) {
log.warn("Unable to pre-fetch an image ({}) depended upon by Dockerfile - image build will continue but may fail. Exception message was: {}", imageName, e.getMessage());
log.warn("Unable to pre-fetch an image ({}) depended upon by Dockerfile - image build will continue but may fail. Exception message was: {}", resolvedImageName, e.getMessage());
}
});
}

private String resolveImageNameFromBuildArgs(final String imageName) {
Optional<String> imageBuildArg = buildArgs.keySet()
.stream()
.filter(arg -> imageName.contains(arg))
.findFirst();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will only resolve a single match per FROM statement, right?
It also doesn't resolve the simple $VARIABLE pattern.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep good spot, it would need to check more than one - could well have multiple args for a image depending on how fine grained your need is

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just seen your PR - I had been busy working on other projects and had forgot about this! I saw the notifications a few days ago so was going to see what was needed.
But happy for this to be closed in favour of your PR :)

if (imageBuildArg.isPresent()) {
String buildArg = imageBuildArg.get();
return imageName.replaceAll("\\$\\{" + buildArg + "\\}",
Matcher.quoteReplacement(buildArgs.get(buildArg)));
}
else {
return imageName;
}
}

public ImageFromDockerfile withBuildArg(final String key, final String value) {
this.buildArgs.put(key, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public static Object[][] parameters() {
.withFileFromPath(".", RESOURCE_PATH)
.withDockerfile(RESOURCE_PATH.resolve("Dockerfile-alt"))
},

// Dockerfile build using build args in FROM statements
new Object[]{"test1234",
new ImageFromDockerfile()
.withFileFromPath(".", RESOURCE_PATH)
.withDockerfile(RESOURCE_PATH.resolve("Dockerfile-from-buildarg"))
.withBuildArg("BUILD_IMAGE", "alpine:3.6")
.withBuildArg("BASE_IMAGE_TAG", "3.12")
},
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG BUILD_IMAGE
ARG BASE_IMAGE_TAG

FROM ${BUILD_IMAGE} AS build
COPY localfile.txt /test-build.txt

FROM alpine:${BASE_IMAGE_TAG} AS base
COPY --from=build /test-build.txt /test.txt