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

Add skipPush option to build image configuration (#1243) #1368

Merged
merged 5 commits into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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)
- Add `skipPush` option to build image configuration ([#1243](https://github.com/fabric8io/docker-maven-plugin/issues/1243))
- Support `squash` in build options to squash newly built layers into a single layer ([#785](https://github.com/fabric8io/docker-maven-plugin/issues/785))

* **0.33.0** (2020-01-21)
Expand Down
3 changes: 3 additions & 0 deletions src/main/asciidoc/inc/build/_configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public class BuildImageConfiguration implements Serializable {
@Parameter
private Boolean skip;

@Parameter
private Boolean skipPush;

@Parameter
private ArchiveCompression compression = ArchiveCompression.none;

Expand Down Expand Up @@ -335,6 +338,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;
}
Expand All @@ -351,6 +358,10 @@ public Boolean getSkip() {
return skip;
}

public Boolean getSkipPush() {
return skipPush;
}

public ArchiveCompression getCompression() {
return compression;
}
Expand Down Expand Up @@ -602,6 +613,11 @@ public Builder skip(Boolean skip) {
return this;
}

public Builder skipPush(Boolean skipPush) {
config.skipPush = skipPush;
return this;
}

public Builder buildOptions(Map<String,String> buildOptions) {
config.buildOptions = buildOptions;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public enum ConfigKey {
SECURITY_OPTS,
SHMSIZE,
SKIP_BUILD("skip.build"),
SKIP_PUSH,
SKIP_RUN("skip.run"),
STOP_NAME_PATTERN,
TAGS(ValueCombinePolicy.Merge),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void pushImages(Collection<ImageConfiguration> imageConfigs,
int retries, RegistryConfig registryConfig, boolean skipTag) throws DockerAccessException, MojoExecutionException {
for (ImageConfiguration imageConfig : imageConfigs) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
if (buildConfig.skipPush()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there is a test missing in RegistryServiceTest for pushImages, but it would be cool if you could add one to test this change, too (then the Sonar check would be happy, too). It shouldn't be hard to add, as it should work the same as the pull test, too.

log.info("%s : Skipped pushing", imageConfig.getDescription());
continue;
}

String name = imageConfig.getName();
if (buildConfig != null) {
String configuredRegistry = EnvUtil.firstRegistryOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -28,6 +31,7 @@ public class RegistryServiceTest {

private Exception actualException;

// pull
private String imageName;
private ImagePullPolicy imagePullPolicy;
private TestCacheStore cacheStore;
Expand All @@ -37,6 +41,9 @@ public class RegistryServiceTest {
private String registry;
private Map authConfig;

// push
private ImageConfiguration imageConfiguration;

@Mocked
private DockerAccess docker;

Expand Down Expand Up @@ -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();
}

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

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down