diff --git a/doc/changelog.md b/doc/changelog.md index db67739b8..e2b217a85 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -6,6 +6,7 @@ - Migrate from joda-time to java.time ([#1025](https://github.com/fabric8io/docker-maven-plugin/issues/1025)) The handling of Y changes when the week straddle the New year ([Stack Overflow](https://stackoverflow.com/questions/26431882/difference-between-year-of-era-and-week-based-year)) - Fix JSON error when parsin tafs (#1354) + - Support to autoPull in build configuration (#626) - Add `skipPush` option to build image configuration ([#1243](https://github.com/fabric8io/docker-maven-plugin/issues/1243)) - docker.container..ip property is no longer set ([#1242](https://github.com/fabric8io/docker-maven-plugin/issues/1242)) - Support `squash` in build options to squash newly built layers into a single layer ([#785](https://github.com/fabric8io/docker-maven-plugin/issues/785)) diff --git a/src/main/asciidoc/inc/build/_configuration.adoc b/src/main/asciidoc/inc/build/_configuration.adoc index 18ba804ec..1b6f69a7c 100644 --- a/src/main/asciidoc/inc/build/_configuration.adoc +++ b/src/main/asciidoc/inc/build/_configuration.adoc @@ -71,6 +71,9 @@ endif::[] A provided `` takes precedence over the name given here. This tag is useful for extensions of this plugin like the https://maven.fabric8.io[fabric8-maven-plugin] which can evaluate the additional information given here. +| *autoPull* +| If the base image should be automatically pulled if not already present. Same semantic as the global `autoPull` option, but specific to this build configuration. + | <> | Definition of a health check as described in <> @@ -139,6 +142,7 @@ remote API. java:8u40 john.doe@example.com + true latest ${project.version} diff --git a/src/main/java/io/fabric8/maven/docker/BuildMojo.java b/src/main/java/io/fabric8/maven/docker/BuildMojo.java index 07080a4e3..602ccea86 100644 --- a/src/main/java/io/fabric8/maven/docker/BuildMojo.java +++ b/src/main/java/io/fabric8/maven/docker/BuildMojo.java @@ -13,6 +13,7 @@ import io.fabric8.maven.docker.access.DockerAccessException; import io.fabric8.maven.docker.config.BuildImageConfiguration; import io.fabric8.maven.docker.config.ImageConfiguration; +import io.fabric8.maven.docker.config.ImagePullPolicy; import io.fabric8.maven.docker.service.BuildService; import io.fabric8.maven.docker.service.ImagePullManager; import io.fabric8.maven.docker.service.JibBuildService; @@ -75,7 +76,7 @@ protected void buildAndTag(ServiceHub hub, ImageConfiguration imageConfig) EnvUtil.storeTimestamp(getBuildTimestampFile(), getBuildTimestamp()); BuildService.BuildContext buildContext = getBuildContext(); - ImagePullManager pullManager = getImagePullManager(determinePullPolicy(imageConfig.getBuildConfiguration()), autoPull); + ImagePullManager pullManager = getImagePullManager(determinePullPolicy(imageConfig.getBuildConfiguration()), determineAutoPull(imageConfig.getBuildConfiguration())); proceedWithBuildProcess(hub, buildContext, imageConfig, pullManager); } @@ -133,6 +134,10 @@ private String determinePullPolicy(BuildImageConfiguration buildConfig) { return buildConfig != null && buildConfig.getImagePullPolicy() != null ? buildConfig.getImagePullPolicy() : imagePullPolicy; } + private String determineAutoPull(BuildImageConfiguration buildConfig) { + return buildConfig != null && buildConfig.getAutoPull() != null ? buildConfig.getAutoPull() : autoPull; + } + /** * Helper method to process an ImageConfiguration. * diff --git a/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java b/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java index 85658f883..f9f6530ab 100644 --- a/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java +++ b/src/main/java/io/fabric8/maven/docker/access/BuildOptions.java @@ -27,14 +27,14 @@ */ public class BuildOptions { - private Map options; + private final Map options; public BuildOptions() { - this(new HashMap()); + this(new HashMap<>()); } public BuildOptions(Map options) { - this.options = options != null ? new HashMap<>(options) : new HashMap(); + this.options = options != null ? new HashMap<>(options) : new HashMap<>(); } public BuildOptions addOption(String key, String value) { @@ -42,6 +42,22 @@ public BuildOptions addOption(String key, String value) { return this; } + public BuildOptions autoPull(String autoPull) { + if (autoPull != null) { + switch (autoPull.toLowerCase()) { + case "always": + case "true" : + case "ifnotpresent": + options.put("pull", Boolean.TRUE.toString()); + break; + case "false": + case "never": + options.put("pull", Boolean.FALSE.toString()); + } + } + return this; + } + public BuildOptions dockerfile(String name) { if (name != null) { options.put("dockerfile", name); diff --git a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java index 3a169f9ea..77581a488 100644 --- a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java +++ b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java @@ -18,6 +18,9 @@ public class BuildImageConfiguration implements Serializable { public static final String DEFAULT_FILTER = "${*}"; public static final String DEFAULT_CLEANUP = "try"; + @Parameter + private String autoPull; + /** * Directory is used as build context. * If not specified, dockerfile's parent directory is used as build context. @@ -185,6 +188,10 @@ public boolean isDockerFileMode() { return dockerFileFile != null; } + public String getAutoPull() { + return autoPull; + } + public String getLoadNamePattern() { return loadNamePattern; } @@ -422,6 +429,11 @@ public Builder(BuildImageConfiguration that) { } } + public Builder autoPull(String autoPull) { + config.autoPull = autoPull; + return this; + } + public Builder contextDir(String dir) { config.contextDir = dir; return this; diff --git a/src/main/java/io/fabric8/maven/docker/service/BuildService.java b/src/main/java/io/fabric8/maven/docker/service/BuildService.java index e878c0977..69ae02178 100644 --- a/src/main/java/io/fabric8/maven/docker/service/BuildService.java +++ b/src/main/java/io/fabric8/maven/docker/service/BuildService.java @@ -12,6 +12,7 @@ import java.util.Properties; import java.util.regex.PatternSyntaxException; +import io.fabric8.maven.docker.config.ImagePullPolicy; import org.apache.maven.plugin.MojoExecutionException; import com.google.common.collect.ImmutableMap; import com.google.gson.JsonObject; @@ -172,6 +173,7 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, // auto is now supported by docker, consider switching? BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()) + .autoPull(calculateAutoPullFromImagePullPolicy(buildConfig.getImagePullPolicy())) .dockerfile(getDockerfileName(buildConfig)) .forceRemove(cleanupMode.isRemove()) .noCache(noCache) @@ -442,6 +444,13 @@ private boolean isEmpty(String str) { return str == null || str.isEmpty(); } + static String calculateAutoPullFromImagePullPolicy(String imagePullPolicy) { + if (imagePullPolicy != null) { + return Boolean.toString(!imagePullPolicy.equals(ImagePullPolicy.Never.name())); + } + return Boolean.FALSE.toString(); + } + // =========================================== diff --git a/src/test/java/io/fabric8/maven/docker/access/BuildConfigTest.java b/src/test/java/io/fabric8/maven/docker/access/BuildConfigTest.java index ecab87538..9532cc435 100644 --- a/src/test/java/io/fabric8/maven/docker/access/BuildConfigTest.java +++ b/src/test/java/io/fabric8/maven/docker/access/BuildConfigTest.java @@ -24,6 +24,8 @@ import io.fabric8.maven.docker.util.JsonFactory; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; /** * @author roland @@ -73,6 +75,15 @@ public void dockerfile() { assertEquals(0, opts.getOptions().size()); } + @Test + public void autoPull() { + BuildOptions opts = new BuildOptions().autoPull("true"); + assertEquals("true", opts.getOptions().get("pull")); + + opts = new BuildOptions().autoPull(null); + assertFalse(opts.getOptions().containsKey("pull")); + } + @Test public void buildArgs() { Map args = Collections.singletonMap("arg1","blub"); @@ -96,23 +107,23 @@ public void override() { @Test public void cacheFrom() { - BuildOptions opts = new BuildOptions().cacheFrom(Arrays.asList("foo/bar:latest")); + BuildOptions opts = new BuildOptions().cacheFrom(Collections.singletonList("foo/bar:latest")); assertEquals("[\"foo/bar:latest\"]", opts.getOptions().get("cachefrom")); opts.cacheFrom(Arrays.asList("foo/bar:latest", "foo/baz:1.0")); assertEquals("[\"foo/bar:latest\",\"foo/baz:1.0\"]", opts.getOptions().get("cachefrom")); - opts.cacheFrom(Arrays.asList()); - assertEquals(null, opts.getOptions().get("cachefrom")); + opts.cacheFrom(Collections.emptyList()); + assertNull(opts.getOptions().get("cachefrom")); opts.cacheFrom(null); - assertEquals(null, opts.getOptions().get("cachefrom")); + assertNull(opts.getOptions().get("cachefrom")); } @Test public void network() { BuildOptions opts = new BuildOptions().network(null); - assertEquals(null, opts.getOptions().get("networkmode")); + assertNull(opts.getOptions().get("networkmode")); opts.network("host"); assertEquals("host", opts.getOptions().get("networkmode"));