in plugins/docker/component-image/src/main/java/co/elastic/gradle/dockercomponent/JibActions.java [222:283]
public void buildArchive(
Architecture architecture,
RegularFile imageArchive,
RegularFile imageId,
RegularFile createdAtFile,
List<ContainerImageBuildInstruction> instructions
) {
try {
final Optional<From> fromImageRef = instructions.stream()
.filter(instruction -> instruction instanceof From)
.map(it -> (From) it)
.findFirst();
final JibContainerBuilder jibBuilder;
if (fromImageRef.isPresent()) {
try {
jibBuilder = Jib.from(
getAuthenticatedRegistryImage(fromImageRef.get().getReference().get())
);
} catch (InvalidImageReferenceException e) {
throw new GradleException("Invalid from image format", e);
}
} else {
jibBuilder = Jib.fromScratch();
}
// We configure this explicitly because in our implementation the per platform instructions can be different,
// e.g. adding a platform specific binary to the image and Jib only supports creating multi-platform images
// when the instructions are identical.
jibBuilder.setPlatforms(Set.of(new Platform(architecture.dockerName(), "linux")));
processInstructions(
jibBuilder,
imageArchive.getAsFile().toPath().getParent().resolve("context"),
instructions
);
Instant createdAt = Instant.now();
jibBuilder.setCreationTime(createdAt);
final JibContainer container;
try (FileSystem memFS = Jimfs.newFileSystem(Configuration.unix())) {
final Path imagePath = memFS.getPath("/foo");
container = jibBuilder.containerize(
getContainerizer(TarImage.at(imagePath).named("detached"))
);
try (InputStream image = Files.newInputStream(imagePath); ZstdCompressorOutputStream compressedOut = new ZstdCompressorOutputStream(
new BufferedOutputStream(Files.newOutputStream(imageArchive.getAsFile().toPath())))) {
IOUtils.copy(image, compressedOut);
}
}
Files.writeString(
imageId.getAsFile().toPath(),
container.getImageId().getHash()
);
Files.writeString(
createdAtFile.getAsFile().toPath(),
createdAt.toString()
);
} catch (InterruptedException | RegistryException | IOException | CacheDirectoryCreationException | ExecutionException | InvalidImageReferenceException e) {
throw new GradleException("Failed to build component image", e);
}
}