Skip to content

Commit 1b3fda9

Browse files
rhussrohanKanojia
authored andcommitted
Merge branch 'master' of https://github.com/zaramckeown/docker-maven-plugin into pr/626-build-pull
1 parent dc82c82 commit 1b3fda9

File tree

9 files changed

+58
-11
lines changed

9 files changed

+58
-11
lines changed

src/main/asciidoc/inc/build/_configuration.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ A provided `<from>` takes precedence over the name given here. This tag is usefu
8181
| *maintainer*
8282
| The author (`MAINTAINER`) field for the generated image
8383

84+
| *pull*
85+
| Always attempt to pull a newer version of the image. This can be overwritten by setting a system property `docker.pull` when running Maven.
86+
8487
| *nocache*
8588
| Don't use Docker's build cache. This can be overwritten by setting a system property `docker.nocache` when running Maven.
8689

@@ -125,6 +128,7 @@ remote API.
125128
<build>
126129
<from>java:8u40</from>
127130
<maintainer>[email protected]</maintainer>
131+
<pull>true</pull>
128132
<tags>
129133
<tag>latest</tag>
130134
<tag>${project.version}</tag>

src/main/asciidoc/inc/external/_property_configuration.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ when a `docker.from` or a `docker.fromExt` is set.
6464
| *docker.assembly.dockerFileDir*
6565
| specifies a directory containing an external Dockerfile that will be used to create the image. This is deprecated please use `docker.dockerFileDir` or `docker.dockerFile` instead.
6666

67+
| *docker.pull*
68+
| Always attempt to pull a newer version of the image, This can be overwritten by setting a system property `docker.pull` when running Maven.
69+
6770
| *docker.nocache*
68-
| Don't use Docker's build cache.This can be overwritten by setting a system property `docker.nocache` when running Maven.
71+
| Don't use Docker's build cache. This can be overwritten by setting a system property `docker.nocache` when running Maven.
6972

7073
| *docker.bind.idx*
7174
| Sets a list of paths to bind/expose in the container

src/main/java/io/fabric8/maven/docker/access/BuildOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public BuildOptions noCache(boolean noCache) {
6060
return this;
6161
}
6262

63+
public BuildOptions pull(boolean pull) {
64+
options.put("pull", pull ? "1" : "0");
65+
return this;
66+
}
67+
6368
public BuildOptions buildArgs(Map<String, String> buildArgs) {
6469
if (buildArgs != null && buildArgs.size() > 0) {
6570
options.put("buildargs", JsonFactory.newJsonObject(buildArgs).toString());

src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ public class BuildImageConfiguration implements Serializable {
9393
private Boolean nocache;
9494

9595
@Parameter
96-
private Boolean optimise;
96+
private Boolean pull = false;
97+
98+
@Parameter
99+
private Boolean optimise = false;
97100

98101
@Parameter
99102
private List<String> volumes;
@@ -249,6 +252,10 @@ public CleanupMode cleanupMode() {
249252
return CleanupMode.parse(cleanup != null ? cleanup : DEFAULT_CLEANUP);
250253
}
251254

255+
public boolean pull() {
256+
return pull;
257+
}
258+
252259
public boolean nocache() {
253260
return nocache != null ? nocache : false;
254261
}
@@ -457,6 +464,20 @@ public Builder nocache(Boolean nocache) {
457464
return this;
458465
}
459466

467+
public Builder pull(Boolean pull) {
468+
if (pull != null) {
469+
config.pull = pull;
470+
}
471+
return this;
472+
}
473+
474+
public Builder nocache(String nocache) {
475+
if (nocache != null) {
476+
config.nocache = Boolean.valueOf(nocache);
477+
}
478+
return this;
479+
}
480+
460481
public Builder optimise(Boolean optimise) {
461482
config.optimise = optimise;
462483
return this;

src/main/java/io/fabric8/maven/docker/config/handler/property/ConfigKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public enum ConfigKey {
4444
CPUSHARES,
4545
CPUS,
4646
CPUSET,
47+
PULL,
4748
NOCACHE,
4849
OPTIMISE,
4950
CMD,

src/main/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private BuildImageConfiguration extractBuildConfiguration(ImageConfiguration fro
128128
return new BuildImageConfiguration.Builder()
129129
.cmd(extractArguments(valueProvider, CMD, config == null ? null : config.getCmd()))
130130
.cleanup(valueProvider.getString(CLEANUP, config == null ? null : config.getCleanup()))
131+
.pull(valueProvider.getBoolean(PULL, config == null ? null : config.pull()))
131132
.nocache(valueProvider.getBoolean(NOCACHE, config == null ? null : config.getNoCache()))
132133
.optimise(valueProvider.getBoolean(OPTIMISE, config == null ? null : config.getOptimise()))
133134
.entryPoint(extractArguments(valueProvider, ENTRYPOINT, config == null ? null : config.getEntryPoint()))

src/main/java/io/fabric8/maven/docker/service/BuildService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePul
6262
autoPullBaseImage(imageConfig, imagePullManager, buildContext);
6363
}
6464

65-
buildImage(imageConfig, buildContext.getMojoParameters(), checkForNocache(imageConfig), addBuildArgs(buildContext));
65+
buildImage(imageConfig, buildContext.getMojoParameters(), checkForNoPull(imageConfig), checkForNocache(imageConfig), addBuildArgs(buildContext));
6666
}
6767

6868
public void tagImage(String imageName, ImageConfiguration imageConfig) throws DockerAccessException {
@@ -86,12 +86,13 @@ public void tagImage(String imageName, ImageConfiguration imageConfig) throws Do
8686
*
8787
* @param imageConfig the image configuration
8888
* @param params mojo params for the project
89+
* @param pull if not null, always attempt to pull a newer version of the image.
8990
* @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
9091
* @param buildArgs
9192
* @throws DockerAccessException
9293
* @throws MojoExecutionException
9394
*/
94-
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs)
95+
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean pull, boolean noCache, Map<String, String> buildArgs)
9596
throws DockerAccessException, MojoExecutionException {
9697

9798
String imageName = imageConfig.getName();
@@ -125,6 +126,7 @@ protected void buildImage(ImageConfiguration imageConfig, MojoParameters params,
125126
.dockerfile(getDockerfileName(buildConfig))
126127
.forceRemove(cleanupMode.isRemove())
127128
.noCache(noCache)
129+
.pull(pull)
128130
.buildArgs(mergedBuildMap);
129131
String newImageId = doBuildImage(imageName, dockerArchive, opts);
130132
log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
@@ -278,6 +280,16 @@ private List<String> extractBaseFromDockerfile(BuildImageConfiguration buildConf
278280
return fromImage;
279281
}
280282

283+
private boolean checkForNoPull(ImageConfiguration imageConfig) {
284+
String pull = System.getProperty("docker.pull");
285+
if (pull != null) {
286+
return pull.length() == 0 || Boolean.valueOf(pull);
287+
} else {
288+
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
289+
return buildConfig.pull();
290+
}
291+
}
292+
281293
private boolean checkForNocache(ImageConfiguration imageConfig) {
282294
String nocache = System.getProperty("docker.nocache");
283295
if (nocache != null) {

src/test/java/io/fabric8/maven/docker/service/BuildServiceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void setup() throws Exception {
8181
public void testBuildImageWithCleanup() throws Exception {
8282
givenAnImageConfiguration(true);
8383
givenImageIds(OLD_IMAGE_ID, NEW_IMAGE_ID);
84-
whenBuildImage(true,false);
84+
whenBuildImage(true, false, false);
8585
thenImageIsBuilt();
8686
thenOldImageIsRemoved();
8787
}
@@ -90,7 +90,7 @@ public void testBuildImageWithCleanup() throws Exception {
9090
public void testBuildImageWithNoCleanup() throws Exception {
9191
givenAnImageConfiguration(false);
9292
givenImageIds(OLD_IMAGE_ID, NEW_IMAGE_ID);
93-
whenBuildImage(false,false);
93+
whenBuildImage(false, false, false);
9494
thenImageIsBuilt();
9595
thenOldImageIsNotRemoved();
9696
}
@@ -99,7 +99,7 @@ public void testBuildImageWithNoCleanup() throws Exception {
9999
public void testCleanupCachedImage() throws Exception {
100100
givenAnImageConfiguration(true);
101101
givenImageIds(OLD_IMAGE_ID, OLD_IMAGE_ID);
102-
whenBuildImage(false, false);
102+
whenBuildImage(false, false, false);
103103
thenImageIsBuilt();
104104
thenOldImageIsNotRemoved();
105105
}
@@ -108,7 +108,7 @@ public void testCleanupCachedImage() throws Exception {
108108
public void testCleanupNoExistingImage() throws Exception {
109109
givenAnImageConfiguration(true);
110110
givenImageIds(null, NEW_IMAGE_ID);
111-
whenBuildImage(false, false);
111+
whenBuildImage(false, false, false);
112112
thenImageIsBuilt();
113113
thenOldImageIsNotRemoved();
114114
}
@@ -190,7 +190,7 @@ private void thenOldImageIsRemoved() throws DockerAccessException {
190190
}};
191191
}
192192

193-
private void whenBuildImage(boolean cleanup, boolean nocache) throws DockerAccessException, MojoExecutionException {
193+
private void whenBuildImage(boolean cleanup, boolean pull, boolean nocache) throws DockerAccessException, MojoExecutionException {
194194
new Expectations() {{
195195
docker.buildImage(withEqual(imageConfig.getName()), (File) any, (BuildOptions) any);
196196
}};
@@ -200,7 +200,7 @@ private void whenBuildImage(boolean cleanup, boolean nocache) throws DockerAcces
200200
}};
201201
}
202202

203-
buildService.buildImage(imageConfig, params, nocache, Collections.<String, String>emptyMap());
203+
buildService.buildImage(imageConfig, params, pull, nocache, Collections.<String, String>emptyMap());
204204

205205
}
206206
}

src/test/java/io/fabric8/maven/docker/service/LoadImageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void givenAnImageConfiguration() {
8484
}
8585

8686
private void whenBuildImage() throws DockerAccessException, MojoExecutionException {
87-
buildService.buildImage(imageConfig, params, false, Collections.<String, String>emptyMap());
87+
buildService.buildImage(imageConfig, params, false, false, Collections.<String, String>emptyMap());
8888
}
8989

9090
private void thenImageIsBuilt() throws DockerAccessException {

0 commit comments

Comments
 (0)