Skip to content

Commit

Permalink
fix (BuildService) : Image Build Config BuildArgs should be passed wh…
Browse files Browse the repository at this point in the history
…ile pulling images (fabric8io#1756)

Fixes regression introduced by fabric8io#1731

In fabric8io#1731, we added support for specifying docker build args from
maven/system properties. However, I missed it in review that build args
specified in plugin image build configuration are no longer passed to
autoPullBaseImage.

Ensure we merge image build configuration build args with args provided
via maven/system properties before passing them to autoPullBaseImage

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Feb 24, 2024
1 parent e8c5826 commit 8fa0c7b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
46 changes: 46 additions & 0 deletions it/dockerfile-base-as-arg-buildconfig/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.fabric8.dmp.itests</groupId>
<artifactId>dmp-it-parent</artifactId>
<version>0.45-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dmp-it-dockerfile-base-as-arg-buildconfig</artifactId>
<version>0.45-SNAPSHOT</version>
<name>dmp-it-dockerfile-base-as-arg-buildconfig</name>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>fabric8:dmp-it-dockerfile-base-as-arg-buildconfig</name>
<build>
<dockerFile>Dockerfile</dockerFile>
<args>
<FROM_IMAGE>openjdk:21-slim</FROM_IMAGE>
</args>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>build</id>
<goals>
<goal>build</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ARG FROM_IMAGE
FROM ${FROM_IMAGE} AS jlink
1 change: 1 addition & 0 deletions it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<module>docker-compose-dependon</module>
<module>dockerfile</module>
<module>dockerfile-base-as-arg</module>
<module>dockerfile-base-as-arg-buildconfig</module>
<module>dockerignore</module>
<module>healthcheck</module>
<module>helloworld</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePul

Map<String, String> buildArgs = addBuildArgs(buildContext);
if (imagePullManager != null) {
autoPullBaseImage(imageConfig, imagePullManager, buildContext, buildArgs);
autoPullBaseImage(imageConfig, imagePullManager, buildContext, prepareBuildArgs(buildArgs, imageConfig.getBuildConfiguration()));
autoPullCacheFromImage(imageConfig, imagePullManager, buildContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.Properties;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -62,6 +63,9 @@ class BuildServiceTest {
@Mock
MavenProject mavenProject;

@Mock
MavenSession mavenSession;

@Mock
private QueryService queryService;

Expand Down Expand Up @@ -191,6 +195,35 @@ void testBuildImageWithCacheFrom_ShouldPullImage() throws Exception {
verifyImagePull(buildConfig, pullManager, buildContext, "fabric8/s1i-java");
}

@Test
void testDockerfileWithBuildArgsInBuildConfig_ShouldPullImage() throws Exception {
BuildImageConfiguration buildConfig = new BuildImageConfiguration.Builder()
.dockerFile(getClass().getResource("/io/fabric8/maven/docker/util/Dockerfile_from_build_arg").getPath())
.args(Collections.singletonMap("FROM_IMAGE", "sample/base-image:latest"))
.build();

buildConfig.initAndValidate(logger);

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

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

Mockito.when(mojoParameters.getSession()).thenReturn(mavenSession);
mockMavenProject();

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

//verify that tries to pull both images
verifyImagePull(buildConfig, pullManager, buildContext, "sample/base-image:latest");
}

@Test
void testBuildImagePullsDefaultImageWhenNoFromImage() throws Exception {
BuildImageConfiguration buildConfig = new BuildImageConfiguration.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ARG FROM_IMAGE
FROM ${FROM_IMAGE} AS jlink

0 comments on commit 8fa0c7b

Please sign in to comment.