Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.annotations.VisibleForTesting;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/** The container built by Jib. */
public class JibContainer {
Expand Down Expand Up @@ -99,6 +100,29 @@ public Set<String> getTags() {
return tags;
}

/**
* Get the image tags with digest.
*
* @return the image tags with digest
*/
public Set<String> getImageTagsWithDigest() {
return tags.stream()
.map(
tag -> {
StringBuilder imageTagWithDigest =
new StringBuilder()
.append(targetImage.getRegistry())
.append("/")
.append(targetImage.getRepository())
.append(":")
.append(tag)
.append("@")
.append(imageDigest);
return imageTagWithDigest.toString();
})
.collect(Collectors.toSet());
}

@Override
public int hashCode() {
return Objects.hash(targetImage, imageDigest, imageId, tags, imagePushed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.collect.ImmutableSet;
import java.security.DigestException;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -142,4 +143,30 @@ public void testCreation_withBuildContextAndBuildResult() {
Assert.assertEquals(tags1, container.getTags());
Assert.assertTrue(container.isImagePushed());
}

@Test
public void testGetImageTagsWithDigest_constructsCorrectImageTagsWithDigest() {
JibContainer container = new JibContainer(targetImage1, digest1, digest2, tags1, true);

Set<String> expectedImageTagsWithDigest =
tags1.stream()
.map(
tag -> {
StringBuilder imageTagWithDigest =
new StringBuilder()
.append(targetImage1.getRegistry())
.append("/")
.append(targetImage1.getRepository())
.append(":")
.append(tag)
.append("@")
.append(digest1);
return imageTagWithDigest.toString();
})
.collect(Collectors.toSet());

Set<String> imageTagsWithDigest = container.getImageTagsWithDigest();

Assert.assertEquals(expectedImageTagsWithDigest, imageTagsWithDigest);
}
}
1 change: 1 addition & 0 deletions jib-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ Property | Type | Default | Description
`tar` | `File` | `(project-dir)/build/jib-image.tar` | The path of the tarball generated by `jibBuildTar`. Relative paths are resolved relative to the project root.
`digest` | `File` | `(project-dir)/build/jib-image.digest` | The path of the image digest written out during the build. Relative paths are resolved relative to the project root.
`imageId` | `File` | `(project-dir)/build/jib-image.id` | The path of the image ID written out during the build. Relative paths are resolved relative to the project root.
`imageTagDigest` | string | `(project-dir)/build/jib-image-tag.digest` | The path of the image tags with digest written out during the build. Relative paths are resolved relative to the project root.

<a name="dockerclient-closure"></a>`dockerClient` is an object used to configure Docker when building to/from the Docker daemon. It has the following properties:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ public Path getDigestOutputPath() {
return jibExtension.getOutputPaths().getDigestPath();
}

@Override
public Path getImageTagDigestOutputPath() {
return jibExtension.getOutputPaths().getImageTagDigestPath();
}

@Override
public Path getImageIdOutputPath() {
return jibExtension.getOutputPaths().getImageIdPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class OutputPathsParameters {
private final Project project;

private Path digest;
private Path imageTagDigest;
private Path tar;
private Path imageId;
private Path imageJson;
Expand All @@ -38,6 +39,7 @@ public class OutputPathsParameters {
public OutputPathsParameters(Project project) {
this.project = project;
digest = project.getBuildDir().toPath().resolve("jib-image.digest");
imageTagDigest = project.getBuildDir().toPath().resolve("jib-image-tag.digest");
imageId = project.getBuildDir().toPath().resolve("jib-image.id");
imageJson = project.getBuildDir().toPath().resolve("jib-image.json");
tar = project.getBuildDir().toPath().resolve("jib-image.tar");
Expand All @@ -57,6 +59,21 @@ public void setDigest(String digest) {
this.digest = Paths.get(digest);
}

@Input
public String getImageTagDigest() {
return getRelativeToProjectRoot(imageTagDigest, PropertyNames.OUTPUT_PATHS_IMAGE_TAG_DIGEST)
.toString();
}

@Internal
public Path getImageTagDigestPath() {
return getRelativeToProjectRoot(imageTagDigest, PropertyNames.OUTPUT_PATHS_IMAGE_TAG_DIGEST);
}

public void setImageTagDigest(String imageTagDigest) {
this.imageTagDigest = Paths.get(imageTagDigest);
}

@Input
public String getImageId() {
return getRelativeToProjectRoot(imageId, PropertyNames.OUTPUT_PATHS_IMAGE_ID).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public void testGetters() {
.thenReturn(new HashMap<>(ImmutableMap.of("docker", "client")));

Mockito.when(outputPathsParameters.getDigestPath()).thenReturn(Paths.get("digest/path"));
Mockito.when(outputPathsParameters.getImageTagDigestPath())
.thenReturn(Paths.get("image-tag-digest/path"));
Mockito.when(outputPathsParameters.getImageIdPath()).thenReturn(Paths.get("id/path"));
Mockito.when(outputPathsParameters.getImageJsonPath()).thenReturn(Paths.get("json/path"));
Mockito.when(outputPathsParameters.getTarPath()).thenReturn(Paths.get("tar/path"));
Expand Down Expand Up @@ -147,6 +149,8 @@ public void testGetters() {
new HashMap<>(ImmutableMap.of("docker", "client")),
rawConfiguration.getDockerEnvironment());
Assert.assertEquals(Paths.get("digest/path"), rawConfiguration.getDigestOutputPath());
Assert.assertEquals(
Paths.get("image-tag-digest/path"), rawConfiguration.getImageTagDigestOutputPath());
Assert.assertEquals(Paths.get("id/path"), rawConfiguration.getImageIdOutputPath());
Assert.assertEquals(Paths.get("json/path"), rawConfiguration.getImageJsonOutputPath());
Assert.assertEquals(Paths.get("tar/path"), rawConfiguration.getTarOutputPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,15 @@ public void testOutputFiles() {
testJibExtension.outputPaths(
outputFiles -> {
outputFiles.setDigest("/path/to/digest");
outputFiles.setImageTagDigest("/path/to/image-tag-digest");
outputFiles.setImageId("/path/to/id");
outputFiles.setTar("path/to/tar");
});

assertThat(testJibExtension.getOutputPaths().getDigestPath())
.isEqualTo(Paths.get("/path/to/digest").toAbsolutePath());
assertThat(testJibExtension.getOutputPaths().getImageTagDigestPath())
.isEqualTo(Paths.get("/path/to/image-tag-digest").toAbsolutePath());
assertThat(testJibExtension.getOutputPaths().getImageIdPath())
.isEqualTo(Paths.get("/path/to/id").toAbsolutePath());
assertThat(testJibExtension.getOutputPaths().getTarPath())
Expand Down Expand Up @@ -555,6 +558,9 @@ public void testPropertiesOutputPaths() {
System.setProperty("jib.outputPaths.digest", "/digest/path");
assertThat(testJibExtension.getOutputPaths().getDigestPath())
.isEqualTo(Paths.get("/digest/path").toAbsolutePath());
System.setProperty("jib.outputPaths.imageTagDigest", "/image-tag-digest/path");
assertThat(testJibExtension.getOutputPaths().getImageTagDigestPath())
.isEqualTo(Paths.get("/image-tag-digest/path").toAbsolutePath());
System.setProperty("jib.outputPaths.imageId", "/id/path");
assertThat(testJibExtension.getOutputPaths().getImageIdPath())
.isEqualTo(Paths.get("/id/path").toAbsolutePath());
Expand All @@ -565,6 +571,9 @@ public void testPropertiesOutputPaths() {
System.setProperty("jib.outputPaths.digest", "digest/path");
assertThat(testJibExtension.getOutputPaths().getDigestPath())
.isEqualTo(fakeProject.getProjectDir().toPath().resolve("digest/path"));
System.setProperty("jib.outputPaths.imageTagDigest", "image-tag-digest/path");
assertThat(testJibExtension.getOutputPaths().getImageTagDigestPath())
.isEqualTo(fakeProject.getProjectDir().toPath().resolve("image-tag-digest/path"));
System.setProperty("jib.outputPaths.imageId", "id/path");
assertThat(testJibExtension.getOutputPaths().getImageIdPath())
.isEqualTo(fakeProject.getProjectDir().toPath().resolve("id/path"));
Expand Down
1 change: 1 addition & 0 deletions jib-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ Property | Type | Default | Description
`digest` | string | `(project-dir)/target/jib-image.digest` | The path of the image digest written out during the build. Relative paths are resolved relative to the project root.
`imageId` | string | `(project-dir)/target/jib-image.id` | The path of the image ID written out during the build. Relative paths are resolved relative to the project root.
`imageJson` | string | `(project-dir)/target/jib-image.json` | The path of the image metadata json file written out during the build. Relative paths are resolved relative to the project root.
`imageTagDigest` | string | `(project-dir)/target/jib-image-tag.digest` | The path of the image tags with digest written out during the build. Relative paths are resolved relative to the project root.

<a name="dockerclient-object"></a>`dockerClient` is an object used to configure Docker when building to/from the Docker daemon. It has the following properties:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ public static class OutputPathsParameters {

@Nullable @Parameter private File tar;
@Nullable @Parameter private File digest;
@Nullable @Parameter private File imageTagDigest;
@Nullable @Parameter private File imageId;
@Nullable @Parameter private File imageJson;
}
Expand Down Expand Up @@ -776,6 +777,14 @@ Path getDigestOutputPath() {
return getRelativeToProjectRoot(configuredPath, PropertyNames.OUTPUT_PATHS_DIGEST);
}

Path getImageTagDigestOutputPath() {
Path configuredPath =
outputPaths.imageTagDigest == null
? Paths.get(getProject().getBuild().getDirectory()).resolve("jib-image-tag.digest")
: outputPaths.imageTagDigest.toPath();
return getRelativeToProjectRoot(configuredPath, PropertyNames.OUTPUT_PATHS_IMAGE_TAG_DIGEST);
}

Path getImageIdOutputPath() {
Path configuredPath =
outputPaths.imageId == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ public Path getDigestOutputPath() {
return jibPluginConfiguration.getDigestOutputPath();
}

@Override
public Path getImageTagDigestOutputPath() {
return jibPluginConfiguration.getImageTagDigestOutputPath();
}

@Override
public Path getImageIdOutputPath() {
return jibPluginConfiguration.getImageIdOutputPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public void testSystemPropertiesOutputPaths() {
// Absolute paths
sessionSystemProperties.put("jib.outputPaths.digest", "/digest/path");
assertThat(testPluginConfiguration.getDigestOutputPath()).isEqualTo(Paths.get("/digest/path"));
sessionSystemProperties.put("jib.outputPaths.imageTagDigest", "/image-tag-digest/path");
assertThat(testPluginConfiguration.getImageTagDigestOutputPath())
.isEqualTo(Paths.get("/image-tag-digest/path"));
sessionSystemProperties.put("jib.outputPaths.imageId", "/id/path");
assertThat(testPluginConfiguration.getImageIdOutputPath()).isEqualTo(Paths.get("/id/path"));
sessionSystemProperties.put("jib.outputPaths.tar", "/tar/path");
Expand All @@ -213,6 +216,9 @@ public void testSystemPropertiesOutputPaths() {
sessionSystemProperties.put("jib.outputPaths.digest", "digest/path");
assertThat(testPluginConfiguration.getDigestOutputPath())
.isEqualTo(Paths.get("/repository/project/digest/path"));
sessionSystemProperties.put("jib.outputPaths.imageTagDigest", "image-tag-digest/path");
assertThat(testPluginConfiguration.getImageTagDigestOutputPath())
.isEqualTo(Paths.get("/repository/project/image-tag-digest/path"));
sessionSystemProperties.put("jib.outputPaths.imageId", "id/path");
assertThat(testPluginConfiguration.getImageIdOutputPath())
.isEqualTo(Paths.get("/repository/project/id/path"));
Expand Down Expand Up @@ -323,6 +329,9 @@ public void testPomPropertiesExtraDirectories() {
public void testPomPropertiesOutputPaths() {
project.getProperties().setProperty("jib.outputPaths.digest", "/digest/path");
assertThat(testPluginConfiguration.getDigestOutputPath()).isEqualTo(Paths.get("/digest/path"));
project.getProperties().setProperty("jib.outputPaths.imageTagDigest", "/image-tag-digest/path");
assertThat(testPluginConfiguration.getImageTagDigestOutputPath())
.isEqualTo(Paths.get("/image-tag-digest/path"));
project.getProperties().setProperty("jib.outputPaths.imageId", "/id/path");
assertThat(testPluginConfiguration.getImageIdOutputPath()).isEqualTo(Paths.get("/id/path"));
project.getProperties().setProperty("jib.outputPaths.imageJson", "/json/path");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public void testGetters() {
Mockito.when(jibPluginConfiguration.getDockerClientEnvironment())
.thenReturn(new HashMap<>(ImmutableMap.of("docker", "client")));
Mockito.when(jibPluginConfiguration.getDigestOutputPath()).thenReturn(Paths.get("digest/path"));
Mockito.when(jibPluginConfiguration.getImageTagDigestOutputPath())
.thenReturn(Paths.get("image-tag-digest/path"));
Mockito.when(jibPluginConfiguration.getImageIdOutputPath()).thenReturn(Paths.get("id/path"));
Mockito.when(jibPluginConfiguration.getImageJsonOutputPath())
.thenReturn(Paths.get("json/path"));
Expand Down Expand Up @@ -144,6 +146,8 @@ public void testGetters() {
new HashMap<>(ImmutableMap.of("docker", "client")),
rawConfiguration.getDockerEnvironment());
Assert.assertEquals(Paths.get("digest/path"), jibPluginConfiguration.getDigestOutputPath());
Assert.assertEquals(
Paths.get("image-tag-digest/path"), jibPluginConfiguration.getImageTagDigestOutputPath());
Assert.assertEquals(Paths.get("id/path"), jibPluginConfiguration.getImageIdOutputPath());
Assert.assertEquals(Paths.get("json/path"), jibPluginConfiguration.getImageJsonOutputPath());
Assert.assertEquals(Paths.get("tar/path"), jibPluginConfiguration.getTarOutputPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private static void handleRegistryUnauthorizedException(
private final Consumer<LogEvent> logger;
private final HelpfulSuggestions helpfulSuggestions;
@Nullable private Path imageDigestOutputPath;
@Nullable private Path imageTagDigestOutputPath;
@Nullable private Path imageIdOutputPath;
@Nullable private Path imageJsonOutputPath;

Expand Down Expand Up @@ -236,6 +237,10 @@ public JibContainer runBuild()
String imageDigest = jibContainer.getDigest().toString();
Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
}
if (imageTagDigestOutputPath != null) {
Set<String> imageTagsWithDigest = jibContainer.getImageTagsWithDigest();
Files.write(imageTagDigestOutputPath, imageTagsWithDigest);
}
if (imageIdOutputPath != null) {
String imageId = jibContainer.getImageId().toString();
Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
Expand Down Expand Up @@ -304,6 +309,11 @@ public JibBuildRunner writeImageDigest(@Nullable Path imageDigestOutputPath) {
return this;
}

public JibBuildRunner writeImageTagDigest(@Nullable Path imageTagDigestOutputPath) {
this.imageTagDigestOutputPath = imageTagDigestOutputPath;
return this;
}

/**
* Set the location where the image id will be saved. If {@code null} then digest is not saved.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public static JibBuildRunner createJibBuildRunnerForDockerDaemonImage(
targetImageReference,
rawConfiguration.getToTags())
.writeImageDigest(rawConfiguration.getDigestOutputPath())
.writeImageTagDigest(rawConfiguration.getImageTagDigestOutputPath())
.writeImageId(rawConfiguration.getImageIdOutputPath())
.writeImageJson(rawConfiguration.getImageJsonOutputPath());
}
Expand Down Expand Up @@ -235,6 +236,7 @@ public static JibBuildRunner createJibBuildRunnerForTarImage(
helpfulSuggestions,
rawConfiguration.getTarOutputPath())
.writeImageDigest(rawConfiguration.getDigestOutputPath())
.writeImageTagDigest(rawConfiguration.getImageTagDigestOutputPath())
.writeImageId(rawConfiguration.getImageIdOutputPath())
.writeImageJson(rawConfiguration.getImageJsonOutputPath());
}
Expand Down Expand Up @@ -320,6 +322,7 @@ public static JibBuildRunner createJibBuildRunnerForRegistryImage(
targetImageReference,
rawConfiguration.getToTags())
.writeImageDigest(rawConfiguration.getDigestOutputPath())
.writeImageTagDigest(rawConfiguration.getImageTagDigestOutputPath())
.writeImageId(rawConfiguration.getImageIdOutputPath())
.writeImageJson(rawConfiguration.getImageJsonOutputPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class PropertyNames {
public static final String DOCKER_CLIENT_EXECUTABLE = "jib.dockerClient.executable";
public static final String DOCKER_CLIENT_ENVIRONMENT = "jib.dockerClient.environment";
public static final String OUTPUT_PATHS_DIGEST = "jib.outputPaths.digest";
public static final String OUTPUT_PATHS_IMAGE_TAG_DIGEST = "jib.outputPaths.imageTagDigest";
public static final String OUTPUT_PATHS_IMAGE_ID = "jib.outputPaths.imageId";
public static final String OUTPUT_PATHS_IMAGE_JSON = "jib.outputPaths.imageJson";
public static final String OUTPUT_PATHS_TAR = "jib.outputPaths.tar";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ interface CredHelperConfiguration {

Path getDigestOutputPath();

Path getImageTagDigestOutputPath();

Path getImageIdOutputPath();

Path getImageJsonOutputPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,29 @@ public void testBuildImage_writesImageJson() throws Exception {
Assert.assertEquals(tags, ImmutableSet.copyOf(metadataOutput.getTags()));
Assert.assertTrue(metadataOutput.isImagePushed());
}

@Test
public void testBuildImage_writeImageTagDigest() throws Exception {
Set<String> imageTagsDigest =
ImmutableSet.of(
"gcr.io/project/image:latest@sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
"gcr.io/project/image:custom-tag@sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789");

String expectedImageTagsDigestOutput =
"gcr.io/project/image:latest@sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789\n"
+ "gcr.io/project/image:custom-tag@sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789\n";

final Path outputPath = temporaryFolder.newFile("jib-image-tag.digest").toPath();

Mockito.when(mockJibContainer.getImageTagsWithDigest()).thenReturn(imageTagsDigest);
Mockito.when(mockJibContainerBuilder.containerize(mockContainerizer))
.thenReturn(mockJibContainer);

testJibBuildRunner.writeImageTagDigest(outputPath).runBuild();

final String imageTagDigestOutput =
new String(Files.readAllBytes(outputPath), StandardCharsets.UTF_8);

Assert.assertEquals(expectedImageTagsDigestOutput, imageTagDigestOutput);
}
}