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

support the new docker build --pull option #1191

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<alias>.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))
Expand Down
4 changes: 4 additions & 0 deletions src/main/asciidoc/inc/build/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ endif::[]

A provided `<from>` 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*
| Whether we need to pull a newer version of image.
rhuss marked this conversation as resolved.
Show resolved Hide resolved

| <<build-healthcheck, *healthCheck*>>
| Definition of a health check as described in <<build-healthcheck, Healthcheck>>

Expand Down Expand Up @@ -139,6 +142,7 @@ remote API.
<build>
<from>java:8u40</from>
<maintainer>[email protected]</maintainer>
<autoPull>true</autoPull>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/fabric8/maven/docker/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.ServiceHub;
Expand Down Expand Up @@ -74,7 +75,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()));
BuildService buildService = hub.getBuildService();

File buildArchiveFile = buildService.buildArchive(imageConfig, buildContext, resolveBuildArchiveParameter());
Expand Down Expand Up @@ -117,6 +118,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.
*
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/io/fabric8/maven/docker/access/BuildOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,37 @@
*/
public class BuildOptions {

private Map<String, String> options;
private final Map<String, String> options;

public BuildOptions() {
this(new HashMap<String, String>());
this(new HashMap<>());
}

public BuildOptions(Map<String, String> options) {
this.options = options != null ? new HashMap<>(options) : new HashMap<String, String>();
this.options = options != null ? new HashMap<>(options) : new HashMap<>();
}

public BuildOptions addOption(String key, String value) {
options.put(key,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -185,6 +188,10 @@ public boolean isDockerFileMode() {
return dockerFileFile != null;
}

public String getAutoPull() {
return autoPull;
}

public String getLoadNamePattern() {
return loadNamePattern;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}


// ===========================================

Expand Down
21 changes: 16 additions & 5 deletions src/test/java/io/fabric8/maven/docker/access/BuildConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String,String> args = Collections.singletonMap("arg1","blub");
Expand All @@ -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"));
Expand Down