Skip to content

Commit

Permalink
fix : RegistryService should also consider buildArgs while resolving …
Browse files Browse the repository at this point in the history
…base images

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jul 14, 2024
1 parent ac5e09a commit e980189
Show file tree
Hide file tree
Showing 21 changed files with 550 additions and 263 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* **0.45-SNAPSHOT**:
- 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))

* **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
56 changes: 56 additions & 0 deletions it/dockerfile-push-build-arg-from/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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>dockerfile-push-build-arg-from</artifactId>
<version>0.45-SNAPSHOT</version>
<name>dmp-it-dockerfile-build-arg-from</name>

<properties>
<docker.buildArg.baseImage>busybox</docker.buildArg.baseImage>
</properties>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>ttl.sh/%a:5m</name>
<alias>dockerfile</alias>
<build>
<contextDir>${project.basedir}/src/main/docker</contextDir>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>build</id>
<goals>
<goal>build</goal>
</goals>
<phase>install</phase>
</execution>
<execution>
<id>push</id>
<goals>
<goal>push</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions it/dockerfile-push-build-arg-from/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ARG baseImage
FROM ${baseImage}
1 change: 1 addition & 0 deletions it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>dockerfile</module>
<module>dockerfile-base-as-arg</module>
<module>dockerfile-base-as-arg-buildconfig</module>
<module>dockerfile-push-build-arg-from</module>
<module>dockerignore</module>
<module>graceful-container-removal</module>
<module>healthcheck</module>
Expand Down
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefire.version>3.0.0-M2</surefire.version>
<system-stubs.version>2.0.1</system-stubs.version>
<version.assertj-core>3.25.3</version.assertj-core>
</properties>

<dependencies>
Expand Down Expand Up @@ -212,6 +213,13 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${version.assertj-core}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
Expand Down

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/io/fabric8/maven/docker/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.fabric8.maven.docker.log.LogDispatcher;
import io.fabric8.maven.docker.log.LogOutputSpecFactory;
import io.fabric8.maven.docker.model.Container;
import io.fabric8.maven.docker.service.BuildService;
import io.fabric8.maven.docker.service.DockerAccessFactory;
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.service.QueryService;
Expand Down Expand Up @@ -242,6 +243,12 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Context

@Parameter
protected Map<String, String> buildArgs;
// ==============================================================================================================
// Parameters required from Maven when building an assembly. They cannot be injected directly
// into DockerAssemblyCreator.
// See also here: http://maven.40175.n5.nabble.com/Mojo-Java-1-5-Component-MavenProject-returns-null-vs-JavaDoc-parameter-expression-quot-project-quot-s-td5733805.html
@Parameter(property = "docker.pull.registry")
String pullRegistry;

// Images resolved with external image resolvers and hooks for subclass to
// mangle the image configurations.
Expand Down Expand Up @@ -315,6 +322,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

protected BuildService.BuildContext getBuildContext() {
return new BuildService.BuildContext.Builder()
.buildArgs(buildArgs)
.mojoParameters(createMojoParameters())
.registryConfig(getRegistryConfig(pullRegistry))
.build();
}

private void logException(Exception exp) {
if (exp.getCause() != null) {
log.error("%s [%s]", exp.getMessage(), exp.getCause().getMessage());
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/io/fabric8/maven/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.fabric8.maven.docker.service.ImagePullManager;
import io.fabric8.maven.docker.service.JibBuildService;
import io.fabric8.maven.docker.service.ServiceHub;
import io.fabric8.maven.docker.service.helper.BuildArgResolver;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.Logger;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -24,6 +25,7 @@
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import static io.fabric8.maven.docker.service.RegistryService.createCompleteAuthConfigList;

Expand All @@ -34,7 +36,7 @@
* @since 28.07.14
*/
@Mojo(name = "build", defaultPhase = LifecyclePhase.INSTALL, requiresDependencyResolution = ResolutionScope.TEST)
public class BuildMojo extends AbstractBuildSupportMojo {
public class BuildMojo extends AbstractDockerMojo {

public static final String DMP_PLUGIN_DESCRIPTOR = "META-INF/maven/io.fabric8/dmp-plugin";
public static final String DOCKER_EXTRA_DIR = "docker-extra";
Expand Down Expand Up @@ -101,7 +103,9 @@ private void proceedWithDockerBuild(ServiceHub hub, BuildService.BuildContext bu
File buildArchiveFile = buildService.buildArchive(imageConfig, buildContext, resolveBuildArchiveParameter());
if (Boolean.FALSE.equals(shallBuildArchiveOnly())) {
if (imageConfig.isBuildX()) {
hub.getBuildXService().build(createProjectPaths(), imageConfig, null, createCompleteAuthConfigList(false, imageConfig, getRegistryConfig(pullRegistry), createMojoParameters()), buildArchiveFile);
BuildArgResolver buildArgResolver = new BuildArgResolver(log);
Map<String, String> buildArgsFromExternalSources = buildArgResolver.resolveBuildArgs(buildContext);
hub.getBuildXService().build(createProjectPaths(), imageConfig, null, createCompleteAuthConfigList(false, imageConfig, getRegistryConfig(pullRegistry), createMojoParameters(), buildArgsFromExternalSources), buildArchiveFile);
} else {
buildService.buildImage(imageConfig, pullManager, buildContext, buildArchiveFile);
if (!skipTag && !imageConfig.getBuildConfiguration().skipTag()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/fabric8/maven/docker/PushMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoEx
}

private void executeDockerPush(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
hub.getRegistryService().pushImages(createProjectPaths(), getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag, createMojoParameters());
hub.getRegistryService().pushImages(createProjectPaths(), getResolvedImages(), retries, getRegistryConfig(pushRegistry), skipTag, getBuildContext());
}

private void executeJibPush(ServiceHub hub) throws MojoExecutionException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/fabric8/maven/docker/SourceMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @since 25/10/15
*/
@Mojo(name = "source", defaultPhase = LifecyclePhase.PACKAGE)
public class SourceMojo extends AbstractBuildSupportMojo {
public class SourceMojo extends AbstractDockerMojo {

@Component
private MavenProjectHelper projectHelper;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/fabric8/maven/docker/WatchMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @since 16/06/15
*/
@Mojo(name = "watch")
public class WatchMojo extends AbstractBuildSupportMojo {
public class WatchMojo extends AbstractDockerMojo {

/**
* Watching mode for rebuilding images
Expand Down
Loading

0 comments on commit e980189

Please sign in to comment.