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

Pull FROM images in relative path Dockerfiles #1824

Merged
merged 1 commit into from
Sep 22, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private String extractBaseFromConfiguration(BuildImageConfiguration buildConfig)
}

static List<String> extractBaseFromDockerfile(BuildImageConfiguration buildConfig, MojoParameters mojoParameters, Map<String, String> buildArgs) {
if (buildConfig.getDockerFile() == null || !buildConfig.getDockerFile().exists()) {
if (buildConfig.getDockerFile() == null || !buildConfig.getAbsoluteDockerFilePath(mojoParameters).exists()) {
if (buildConfig.getFrom() != null && !buildConfig.getFrom().isEmpty()) {
return Collections.singletonList(buildConfig.getFrom());
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/io/fabric8/maven/docker/MojoTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ protected void givenMavenProject(AbstractDockerMojo mojo) {
mojo.project = mavenProject;
mojo.log = ansiLogger;
mojo.outputDirectory= "target/docker";
mojo.sourceDirectory = "src/main/docker";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this to prevent a NullPointerException in the test buildUsingConfiguredBuildxWithContext. It sets a contextDir on the BuildImageConfiguration, which causes a default dockerFileFile to be set. This causes the BuildService to attempt to get the FROM images from the Dockerfile, and the source directory needs to be set to calculate the absolute path to the file.

mojo.authConfigFactory= authConfigFactory;
mojo.session = session;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -224,6 +225,38 @@ void testDockerfileWithBuildArgsInBuildConfig_ShouldPullImage() throws Exception
verifyImagePull(buildConfig, pullManager, buildContext, "sample/base-image:latest");
}

@Test
void testRelativeDockerfile_ShouldPullImage() throws Exception {
final BuildImageConfiguration buildConfig = new BuildImageConfiguration.Builder()
.cleanup("false")
.dockerFile("Dockerfile_from_simple")
.filter("false")
.build();

Mockito.when(mavenProject.getBasedir()).thenReturn(new File(getClass().getResource("/").getPath()));
Mockito.when(mojoParameters.getSourceDirectory()).thenReturn("io/fabric8/maven/docker/util");

buildConfig.initAndValidate(logger);

imageConfig = new ImageConfiguration.Builder()
.name("build-image")
.alias("build-alias")
.buildConfig(buildConfig)
.build();

final ImagePullManager pullManager = new ImagePullManager(null, null, null);
final BuildService.BuildContext buildContext = new BuildService.BuildContext.Builder()
.mojoParameters(mojoParameters)
.build();

mockMavenProject();

final File buildArchive = buildService.buildArchive(imageConfig, buildContext, "");
buildService.buildImage(imageConfig, pullManager, buildContext, buildArchive);

verifyImagePull(buildConfig, pullManager, buildContext, "fabric8/s2i-java");
}

@Test
void testBuildImagePullsDefaultImageWhenNoFromImage() throws Exception {
BuildImageConfiguration buildConfig = new BuildImageConfiguration.Builder()
Expand Down
Loading