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 (BuildService) : Image Build Config BuildArgs should be passed while pulling images (#1756) #1759

Merged
merged 1 commit into from
Feb 24, 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
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
Loading