Skip to content

Commit

Permalink
docker: Only push latest tag when no others where specified (#1496)
Browse files Browse the repository at this point in the history
  • Loading branch information
Postremus committed Oct 20, 2021
1 parent 9c1d3be commit edfce28
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/changelog.md
Expand Up @@ -2,6 +2,7 @@

* **0.37-SNAPSHOT**:
- Allow replacement in tags. Added a new replacement `%T` which always adds a timestamp. ([#1491](https://github.com/fabric8io/docker-maven-plugin/pull/1491))
- Only push the `latest` tag if no other tags where specified in docker mode. This can break your build, if you rely on the automatic `latest` tag. ([#1496](https://github.com/fabric8io/docker-maven-plugin/pull/1496))
- Only push the `latest` tag if no other tags where specified in jib mode. This can break your build, if you rely on the automatic `latest` tag. ([#1498](https://github.com/fabric8io/docker-maven-plugin/pull/1498))

* **0.37.0** (2021-08-15)
Expand Down
Expand Up @@ -59,16 +59,16 @@ public void pushImages(Collection<ImageConfiguration> imageConfigs,

AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);

long start = System.currentTimeMillis();
docker.pushImage(name, authConfig, configuredRegistry, retries);
log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));

if (!skipTag) {
for (String tag : imageConfig.getBuildConfiguration().getTags()) {
if (!skipTag && !buildConfig.getTags().isEmpty()) {
for (String tag : buildConfig.getTags()) {
if (tag != null) {
docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
}
}
} else {
long start = System.currentTimeMillis();
docker.pushImage(name, authConfig, configuredRegistry, retries);
log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
}
}
}
Expand Down
Expand Up @@ -2,8 +2,12 @@

import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.fabric8.maven.docker.access.AuthConfig;
Expand All @@ -14,11 +18,14 @@
import io.fabric8.maven.docker.util.AutoPullMode;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import mockit.Expectations;
import mockit.Mocked;
import mockit.Verifications;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -201,7 +208,7 @@ public void tagForCustomRegistry() throws DockerAccessException {
public void pushImage() throws DockerAccessException {
givenAnImageConfiguration("user/test:1.0.1");

whenPushImage();
whenPushImageTagSkipped();

thenImageHasBeenPushed();
thenNoExceptionThrown();
Expand All @@ -211,7 +218,7 @@ public void pushImage() throws DockerAccessException {
public void pushImageWithoutBuildConfig() throws DockerAccessException {
givenAnImageConfigurationWithoutBuildConfig("user/test:1.0.1");

whenPushImage();
whenPushImageTagSkipped();

thenImageHasNotBeenPushed();
thenNoExceptionThrown();
Expand All @@ -222,12 +229,38 @@ public void pushImageSkipped() throws DockerAccessException {
givenAnImageConfiguration("user/test:1.0.1");
givenPushSkipped(true);

whenPushImage();
whenPushImageTagSkipped();

thenImageHasNotBeenPushed();
thenNoExceptionThrown();
}

@Test
public void testPushedImageTags() throws MojoExecutionException, DockerAccessException {

List<String> imageNames = new ArrayList<>();
new Expectations() {{
docker.pushImage(withCapture(imageNames), (AuthConfig) withNotNull(), anyString, anyInt);
}};


givenAnImageConfiguration("without-tags");
whenPushImageTagSkipped();
// latest tag is used, because no other tags are specified
assertEquals(imageConfiguration.getName(), imageNames.get(0));


givenAnImageConfigurationWithTags("with-tags");
whenPushImageTagSkipped(true);
whenPushImageTagSkipped();
// latest tag is used, because skipTag = true
assertEquals(imageConfiguration.getName(), imageNames.get(1));

// skipTag = false => both specified tags have to be pushed
assertEquals(imageConfiguration.getName()+":foo", imageNames.get(2));
assertEquals(imageConfiguration.getName()+":bar", imageNames.get(3));
}

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

private void thenNoExceptionThrown() {
Expand Down Expand Up @@ -296,13 +329,17 @@ private void whenAutoPullImage() {
}
}

private void whenPushImage() {
private void whenPushImageTagSkipped() {
whenPushImageTagSkipped(false);
}

private void whenPushImageTagSkipped(boolean skipTag) {
try {
RegistryService.RegistryConfig.Builder registryConfigBuilder =
new RegistryService.RegistryConfig.Builder()
.authConfigFactory(authConfigFactory)
.authConfig(authConfig);
registryService.pushImages(Collections.singleton(imageConfiguration), 1, registryConfigBuilder.build(), false);
registryService.pushImages(Collections.singleton(imageConfiguration), 1, registryConfigBuilder.build(), skipTag);
} catch (Exception e) {
this.actualException = e;
}
Expand Down Expand Up @@ -345,6 +382,11 @@ private void givenAnImageConfiguration(String imageName) {
imageConfiguration = new ImageConfiguration.Builder().name(imageName).buildConfig(buildImageConfiguration).build();
}

private void givenAnImageConfigurationWithTags(String imageName) {
final BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder().tags(Arrays.asList("foo", "bar")).build();
imageConfiguration = new ImageConfiguration.Builder().name(imageName).buildConfig(buildImageConfiguration).build();
}

private void givenAnImageConfigurationWithoutBuildConfig(String imageName) {
imageConfiguration = new ImageConfiguration.Builder().name(imageName).buildConfig(null).build();
}
Expand Down

0 comments on commit edfce28

Please sign in to comment.