Skip to content

Commit

Permalink
Support autoPull on the build configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Nov 9, 2019
1 parent 5f1fed9 commit 5a4969e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
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.31-SNAPSHOT**
- Update to jnr-unixsocket 0.23
- Support to autoPull in build configuration #626
- Add null check for null instance in config.json for email #1262
- Allow merging of image configurations using <imagesMap> ([#360](https://github.com/fabric8io/docker-maven-plugin/issues/360))
- Treat bridged and default network mode the same #1234
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.

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

Expand Down Expand Up @@ -133,6 +136,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
13 changes: 12 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 @@ -83,8 +84,18 @@ protected void buildAndTag(ServiceHub hub, ImageConfiguration imageConfig)
protected Date getReferenceDate() {
return new Date();
}

private String determinePullPolicy(BuildImageConfiguration buildConfig) {
return buildConfig != null && buildConfig.getImagePullPolicy() != null ? buildConfig.getImagePullPolicy() : imagePullPolicy;
if (buildConfig != null) {
if (buildConfig.getImagePullPolicy() != null) {
return buildConfig.getImagePullPolicy();
} else if (buildConfig.getAutoPull() != null) {
return buildConfig.getAutoPull().equalsIgnoreCase("TRUE") ?
ImagePullPolicy.IfNotPresent.name() :
ImagePullPolicy.Never.name();
}
}
return imagePullPolicy;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/fabric8/maven/docker/access/BuildOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 @@ -178,6 +181,10 @@ public boolean isDockerFileMode() {
return dockerFileFile != null;
}

public String getAutoPull() {
return autoPull;
}

public String getLoadNamePattern() {
return loadNamePattern;
}
Expand Down Expand Up @@ -392,6 +399,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 @@ -137,6 +137,7 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params,
// auto is now supported by docker, consider switching?
BuildOptions opts =
new BuildOptions(buildConfig.getBuildOptions())
.autoPull(buildConfig.getAutoPull())
.dockerfile(getDockerfileName(buildConfig))
.forceRemove(cleanupMode.isRemove())
.noCache(noCache)
Expand Down
10 changes: 10 additions & 0 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,7 @@
import io.fabric8.maven.docker.util.JsonFactory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
* @author roland
Expand Down Expand Up @@ -61,6 +62,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 Down

0 comments on commit 5a4969e

Please sign in to comment.