From 0d176e5ea015abe0593c8eb346276f040e7c7c68 Mon Sep 17 00:00:00 2001 From: Matt Nelson Date: Thu, 27 Aug 2020 15:44:20 -0500 Subject: [PATCH 1/2] Add skipPush option to build image configuration (#1243) --- src/main/asciidoc/inc/build/_configuration.adoc | 3 +++ .../docker/config/BuildImageConfiguration.java | 16 ++++++++++++++++ .../config/handler/property/ConfigKey.java | 1 + .../handler/property/PropertyConfigHandler.java | 1 + .../maven/docker/service/RegistryService.java | 5 +++++ .../property/PropertyConfigHandlerTest.java | 8 ++++++++ 6 files changed, 34 insertions(+) diff --git a/src/main/asciidoc/inc/build/_configuration.adoc b/src/main/asciidoc/inc/build/_configuration.adoc index 0aba181e7..c7cfffcd5 100644 --- a/src/main/asciidoc/inc/build/_configuration.adoc +++ b/src/main/asciidoc/inc/build/_configuration.adoc @@ -107,6 +107,9 @@ a| Scan the archive specified in `dockerArchive` and find the actual repository | *skip* | if set to true disables building of the image. This config option is best used together with a maven property +| *skipPush* +| if set to true disables pushing of the image. This config option is best used together with a maven property + | *skipTag* | If set to `true` this plugin won't add any tags to images. Property: `docker.skip.tag` 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 b7f130366..866f8329b 100644 --- a/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java +++ b/src/main/java/io/fabric8/maven/docker/config/BuildImageConfiguration.java @@ -164,6 +164,9 @@ public class BuildImageConfiguration implements Serializable { @Parameter private Boolean skip; + @Parameter + private Boolean skipPush; + @Parameter private ArchiveCompression compression = ArchiveCompression.none; @@ -325,6 +328,10 @@ public boolean skip() { return skip != null ? skip : false; } + public boolean skipPush() { + return skipPush != null ? skipPush : false; + } + public Boolean getNoCache() { return noCache != null ? noCache : nocache; } @@ -337,6 +344,10 @@ public Boolean getSkip() { return skip; } + public Boolean getSkipPush() { + return skipPush; + } + public ArchiveCompression getCompression() { return compression; } @@ -583,6 +594,11 @@ public Builder skip(Boolean skip) { return this; } + public Builder skipPush(Boolean skipPush) { + config.skipPush = skipPush; + return this; + } + public Builder buildOptions(Map buildOptions) { config.buildOptions = buildOptions; return this; diff --git a/src/main/java/io/fabric8/maven/docker/config/handler/property/ConfigKey.java b/src/main/java/io/fabric8/maven/docker/config/handler/property/ConfigKey.java index 431df0421..9f242554a 100644 --- a/src/main/java/io/fabric8/maven/docker/config/handler/property/ConfigKey.java +++ b/src/main/java/io/fabric8/maven/docker/config/handler/property/ConfigKey.java @@ -108,6 +108,7 @@ public enum ConfigKey { SECURITY_OPTS, SHMSIZE, SKIP_BUILD("skip.build"), + SKIP_PUSH, SKIP_RUN("skip.run"), STOP_NAME_PATTERN, TAGS(ValueCombinePolicy.Merge), diff --git a/src/main/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandler.java b/src/main/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandler.java index 941189488..b56442b05 100644 --- a/src/main/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandler.java +++ b/src/main/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandler.java @@ -158,6 +158,7 @@ private BuildImageConfiguration extractBuildConfiguration(ImageConfiguration fro .maintainer(valueProvider.getString(MAINTAINER, config == null ? null : config.getMaintainer())) .workdir(valueProvider.getString(WORKDIR, config == null ? null : config.getWorkdir())) .skip(valueProvider.getBoolean(SKIP_BUILD, config == null ? null : config.getSkip())) + .skipPush(valueProvider.getBoolean(SKIP_PUSH, config == null ? null : config.getSkipPush())) .imagePullPolicy(valueProvider.getString(IMAGE_PULL_POLICY_BUILD, config == null ? null : config.getImagePullPolicy())) .contextDir(valueProvider.getString(CONTEXT_DIR, config == null ? null : config.getContextDirRaw())) .dockerArchive(valueProvider.getString(DOCKER_ARCHIVE, config == null ? null : config.getDockerArchiveRaw())) diff --git a/src/main/java/io/fabric8/maven/docker/service/RegistryService.java b/src/main/java/io/fabric8/maven/docker/service/RegistryService.java index 43df148ad..3452bb52c 100644 --- a/src/main/java/io/fabric8/maven/docker/service/RegistryService.java +++ b/src/main/java/io/fabric8/maven/docker/service/RegistryService.java @@ -45,6 +45,11 @@ public void pushImages(Collection imageConfigs, int retries, RegistryConfig registryConfig, boolean skipTag) throws DockerAccessException, MojoExecutionException { for (ImageConfiguration imageConfig : imageConfigs) { BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration(); + if (buildConfig.skipPush()) { + log.info("%s : Skipped pushing", imageConfig.getDescription()); + continue; + } + String name = imageConfig.getName(); if (buildConfig != null) { String configuredRegistry = EnvUtil.firstRegistryOf( diff --git a/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java b/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java index 36221c93d..a713b1d41 100644 --- a/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java +++ b/src/test/java/io/fabric8/maven/docker/config/handler/property/PropertyConfigHandlerTest.java @@ -80,6 +80,14 @@ public void testSkipBuild() { assertFalse(resolveExternalImageConfig(new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.FROM), "busybox"}).getBuildConfiguration().skip()); } + @Test + public void testSkipPush() { + assertFalse(resolveExternalImageConfig(getSkipTestData(ConfigKey.SKIP_PUSH, false)).getBuildConfiguration().skipPush()); + assertTrue(resolveExternalImageConfig(getSkipTestData(ConfigKey.SKIP_PUSH, true)).getBuildConfiguration().skipPush()); + + assertFalse(resolveExternalImageConfig(new String[] {k(ConfigKey.NAME), "image", k(ConfigKey.FROM), "busybox"}).getBuildConfiguration().skipPush()); + } + @Test public void testSkipRun() { assertFalse(resolveExternalImageConfig(getSkipTestData(ConfigKey.SKIP_RUN, false)).getRunConfiguration().skip()); From 2dba1fb6ceb1196880f09cea3e02ed2742b82785 Mon Sep 17 00:00:00 2001 From: Matt Nelson Date: Mon, 31 Aug 2020 14:16:38 -0500 Subject: [PATCH 2/2] add changelog, add registry push test --- doc/changelog.md | 1 + .../docker/service/RegistryServiceTest.java | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/doc/changelog.md b/doc/changelog.md index fd9f8a6ee..84b1a7c38 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) + - Add `skipPush` option to build image configuration ([#1243](https://github.com/fabric8io/docker-maven-plugin/issues/1243)) * **0.33.0** (2020-01-21) - Update to jnr-unixsocket 0.25 to solve concurrency issues ([#552](https://github.com/fabric8io/docker-maven-plugin/issues/552)) diff --git a/src/test/java/io/fabric8/maven/docker/service/RegistryServiceTest.java b/src/test/java/io/fabric8/maven/docker/service/RegistryServiceTest.java index e97218004..0ea726313 100644 --- a/src/test/java/io/fabric8/maven/docker/service/RegistryServiceTest.java +++ b/src/test/java/io/fabric8/maven/docker/service/RegistryServiceTest.java @@ -1,5 +1,8 @@ package io.fabric8.maven.docker.service; +import io.fabric8.maven.docker.config.BuildImageConfiguration; +import io.fabric8.maven.docker.config.ImageConfiguration; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -28,6 +31,7 @@ public class RegistryServiceTest { private Exception actualException; + // pull private String imageName; private ImagePullPolicy imagePullPolicy; private TestCacheStore cacheStore; @@ -37,6 +41,9 @@ public class RegistryServiceTest { private String registry; private Map authConfig; + // push + private ImageConfiguration imageConfiguration; + @Mocked private DockerAccess docker; @@ -190,7 +197,26 @@ public void tagForCustomRegistry() throws DockerAccessException { thenNoExceptionThrown(); } + @Test + public void pushImage() throws DockerAccessException { + givenAnImageConfiguration("user/test:1.0.1"); + + whenPushImage(); + + thenImageHasBeenPushed(); + thenNoExceptionThrown(); + } + + @Test + public void pushImageSkipped() throws DockerAccessException { + givenAnImageConfiguration("user/test:1.0.1"); + givenPushSkipped(true); + whenPushImage(); + + thenImageHasNotBeenPushed(); + thenNoExceptionThrown(); + } // ==================================================================================================== @@ -203,6 +229,18 @@ private void thenImageHasNotBeenPulled() throws DockerAccessException { }}; } + private void thenImageHasNotBeenPushed() throws DockerAccessException { + new Verifications() {{ + docker.pushImage(anyString, (AuthConfig) withNotNull(), anyString, anyInt); times = 0; + }}; + } + + private void thenImageHasBeenPushed() throws DockerAccessException { + new Verifications() {{ + docker.pushImage(anyString, (AuthConfig) withNotNull(), anyString, anyInt); + }}; + } + private void thenImageHasBeenTagged() throws DockerAccessException { new Verifications() {{ docker.tag(new ImageName(imageName).getFullName(registry), imageName, false); @@ -248,6 +286,18 @@ private void whenAutoPullImage() { } } + private void whenPushImage() { + try { + RegistryService.RegistryConfig.Builder registryConfigBuilder = + new RegistryService.RegistryConfig.Builder() + .authConfigFactory(authConfigFactory) + .authConfig(authConfig); + registryService.pushImages(Collections.singleton(imageConfiguration), 1, registryConfigBuilder.build(), false); + } catch (Exception e) { + this.actualException = e; + } + } + private void givenImagePullPolicy(ImagePullPolicy policy) { this.imagePullPolicy = policy; } @@ -280,6 +330,16 @@ private void givenAnImage(String imageName) { this.imageName = imageName; } + private void givenAnImageConfiguration(String imageName) { + final BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder().build(); + imageConfiguration = new ImageConfiguration.Builder().name(imageName).buildConfig(buildImageConfiguration).build(); + } + + private void givenPushSkipped(boolean skipPush) { + final BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder().skipPush(skipPush).build(); + imageConfiguration = new ImageConfiguration.Builder(imageConfiguration).buildConfig(buildImageConfiguration).build(); + } + private class TestCacheStore implements ImagePullManager.CacheStore { String cache;