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

Jib: Only push the latest tag if no other tags where specified. #1498

Merged
merged 1 commit into from
Oct 20, 2021
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 @@ -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 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)
- Fix stop mojo by taking container name pattern into account (#1168)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void push(Collection<ImageConfiguration> imageConfigs, int retries, Regis
JibServiceUtil.jibPush(
imageConfiguration,
getRegistryCredentials(registryConfig, true, imageConfiguration, log),
getBuildTarArchive(imageConfiguration, mojoParameters),
getBuildTarArchive(imageConfiguration, mojoParameters), skipTag,
log
);
}
Expand Down
27 changes: 11 additions & 16 deletions src/main/java/io/fabric8/maven/docker/util/JibServiceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ public static String getFullImageName(ImageConfiguration imageConfiguration, Str
* @param tarArchive tar archive built during build goal
* @param log Logger
*/
public static void jibPush(ImageConfiguration imageConfiguration, Credential pushCredentials, File tarArchive, Logger log) {
BuildImageConfiguration buildImageConfiguration = imageConfiguration.getBuildConfiguration();
String imageName = getFullImageName(imageConfiguration, null);
public static void jibPush(ImageConfiguration imageConfiguration, Credential pushCredentials, File tarArchive, boolean skipTag, Logger log) {
BuildImageConfiguration buildConfig = imageConfiguration.getBuildConfiguration();
try {
for (String tag : getAllImageTags(buildImageConfiguration.getTags(), imageName)) {
String imageNameWithTag = getFullImageName(imageConfiguration, tag);
log.info("Pushing image: %s", imageNameWithTag);
pushImage(TarImage.at(tarArchive.toPath()), imageNameWithTag, pushCredentials, log);
if (!skipTag && !buildConfig.getTags().isEmpty()) {
for (String tag : buildConfig.getTags()) {
String imageNameWithTag = getFullImageName(imageConfiguration, tag);
log.info("Pushing image: %s", imageNameWithTag);
pushImage(TarImage.at(tarArchive.toPath()), imageNameWithTag, pushCredentials, log);
}
} else {
String imageName = getFullImageName(imageConfiguration, null);
pushImage(TarImage.at(tarArchive.toPath()), imageName, pushCredentials, log);
}
} catch (IllegalStateException e) {
log.error("Exception occurred while pushing the image: %s", imageConfiguration.getName());
Expand Down Expand Up @@ -194,15 +198,6 @@ private static JibContainerBuilder populateContainerBuilderFromImageConfiguratio
return containerBuilder;
}

static Set<String> getAllImageTags(List<String> tags, String imageName) {
ImageName tempImage = new ImageName(imageName);
Set<String> tagSet = tags.stream().filter(Objects::nonNull).collect(Collectors.toSet());
if (!tempImage.getTag().isEmpty()) {
tagSet.add(tempImage.getTag());
}
return tagSet;
}

static ImageFormat getImageFormat(String jibImageFormat) {
if (jibImageFormat != null && jibImageFormat.toLowerCase().equalsIgnoreCase("oci")) {
return ImageFormat.OCI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void testPushWithNoConfigurations(@Mocked JibServiceUtil jibServiceUtil)
// Then
// @formatter:off
new Verifications() {{
JibServiceUtil.jibPush((ImageConfiguration) any, (Credential) any, (File) any, logger); times = 0;
JibServiceUtil.jibPush((ImageConfiguration) any, (Credential) any, (File) any, false, logger); times = 0;
}};
// @formatter:on
}
Expand All @@ -164,6 +164,7 @@ public void testPushWithConfiguration(@Mocked JibServiceUtil jibServiceUtil) thr
imageConfiguration,
Credential.from("testuserpush", "testpass"),
(File)any,
false,
logger);
times = 1;
}};
Expand Down
75 changes: 61 additions & 14 deletions src/test/java/io/fabric8/maven/docker/util/JibServiceUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package io.fabric8.maven.docker.util;

import com.google.cloud.tools.jib.api.CacheDirectoryCreationException;
import com.google.cloud.tools.jib.api.Containerizer;
import com.google.cloud.tools.jib.api.Credential;
import com.google.cloud.tools.jib.api.Jib;
import com.google.cloud.tools.jib.api.JibContainerBuilder;
import com.google.cloud.tools.jib.api.RegistryException;
import com.google.cloud.tools.jib.api.TarImage;
import com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath;
import com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer;
import com.google.cloud.tools.jib.api.buildplan.ImageFormat;
Expand All @@ -11,6 +17,11 @@
import io.fabric8.maven.docker.config.AssemblyConfiguration;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import mockit.Capturing;
import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import mockit.Verifications;
import org.apache.maven.plugins.assembly.model.Assembly;
Expand All @@ -21,12 +32,15 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import static io.fabric8.maven.docker.util.JibServiceUtil.BUSYBOX;
import static io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration;
Expand All @@ -36,6 +50,12 @@
import static org.junit.Assert.assertTrue;

public class JibServiceUtilTest {
@Mocked
private Logger logger;

@Capturing
JibContainerBuilder jibContainerBuilder;

@Test
public void testGetBaseImageWithNullBuildConfig() {
assertEquals(BUSYBOX, JibServiceUtil.getBaseImage(new ImageConfiguration.Builder().build()));
Expand All @@ -50,6 +70,47 @@ public void testGetBaseImageWithNotNullBuildConfig() {
.build()));
}

@Test
public void testPushedImageTags() {
File buildArchive = new File("docker-build.tar");

ImageConfiguration imageWithoutTags = new ImageConfiguration.Builder()
.buildConfig(new BuildImageConfiguration.Builder()
.from("quay.io/jkubeio/jkube-test-image:0.0.1")
.build())
.name("without-tags")
.build();

ImageConfiguration imageWithTags = new ImageConfiguration.Builder()
.buildConfig(new BuildImageConfiguration.Builder()
.from("quay.io/jkubeio/jkube-test-image:0.0.1")
.tags(Arrays.asList("foo", "bar"))
.build())
.name("with-tags")
.build();

List<String> imageNames = new ArrayList<>();
new MockUp<JibServiceUtil>() {
@Mock
public void pushImage(TarImage baseImage, String targetImageName, Credential credential, Logger logger) {
imageNames.add(targetImageName);
}
};

JibServiceUtil.jibPush(imageWithoutTags, null, buildArchive, false, logger);
JibServiceUtil.jibPush(imageWithTags, null, buildArchive, true, logger);
JibServiceUtil.jibPush(imageWithTags, null, buildArchive, false, logger);

// latest tag is used, because no other tags are specified
assertEquals(imageWithoutTags.getName()+":latest", imageNames.get(0));
// latest tag is used, because skipTag = true
assertEquals(imageWithTags.getName()+":latest", imageNames.get(1));

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

@Test
public void testContainerFromImageConfiguration(@Mocked JibContainerBuilder containerBuilder) throws Exception {
// Given
Expand Down Expand Up @@ -103,20 +164,6 @@ public void testCopyToContainer(@Mocked JibContainerBuilder containerBuilder) th
}};
}

@Test
public void testAppendOriginalImageNameTagIfApplicable() {
// Given
List<String> imageTagList = Arrays.asList("0.0.1", "0.0.1-SNAPSHOT");

// When
Set<String> result = JibServiceUtil.getAllImageTags(imageTagList, "test-project");

// Then
assertNotNull(result);
assertEquals(3, result.size());
assertArrayEquals(new String[]{"0.0.1-SNAPSHOT", "0.0.1", "latest"}, result.toArray());
}

@Test
public void testGetFullImageNameWithDefaultTag() {
assertEquals("test/test-project:latest", JibServiceUtil.getFullImageName(getSampleImageConfiguration(), null));
Expand Down