Allow image Created date to be configurable

A `createdDate` option on the Maven `spring-boot:build-image` goal
and the Gradle `bootBuildImage` task can be used to set the `Created`
metadata field on a generated OCI image to a specified date or to
the current date.

Closes gh-28798
This commit is contained in:
Scott Frederick
2023-04-05 13:05:48 -05:00
parent cacc563fc0
commit 5817c8441d
68 changed files with 542 additions and 71 deletions

View File

@@ -203,6 +203,12 @@ The values provided to the `tags` option should be full image references in the
| A cache containing layers created by buildpacks and used by the image launching process.
| A named volume in the Docker daemon, with a name derived from the image name.
| `createdDate` +
(`spring-boot.build-image.createdDate`)
| A date that will be used to set the `Created` field in the generated image's metadata.
The value must be a string in the ISO 8601 instant format, or `now` to use the current date and time.
| A fixed date that enables https://buildpacks.io/docs/features/reproducibility/[build reproducibility].
|===
NOTE: The plugin detects the target Java compatibility of the project using the compiler's plugin configuration or the `maven.compiler.target` property.

View File

@@ -21,6 +21,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.util.Random;
import java.util.stream.IntStream;
@@ -29,6 +30,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.buildpack.platform.docker.DockerApi;
import org.springframework.boot.buildpack.platform.docker.DockerApi.VolumeApi;
import org.springframework.boot.buildpack.platform.docker.type.Image;
import org.springframework.boot.buildpack.platform.docker.type.ImageName;
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
import org.springframework.boot.buildpack.platform.docker.type.VolumeName;
@@ -240,14 +242,18 @@ class BuildImageTests extends AbstractArchiveIntegrationTests {
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.systemProperty("spring-boot.build-image.imageName", "example.com/test/cmd-property-name:v1")
.systemProperty("spring-boot.build-image.builder",
"projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1")
"projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2")
.systemProperty("spring-boot.build-image.runImage", "projects.registry.vmware.com/springboot/run:tiny-cnb")
.systemProperty("spring-boot.build-image.createdDate", "2020-07-01T12:34:56Z")
.execute((project) -> {
assertThat(buildLog(project)).contains("Building image")
.contains("example.com/test/cmd-property-name:v1")
.contains("---> Test Info buildpack building")
.contains("---> Test Info buildpack done")
.contains("Successfully built image");
Image image = new DockerApi().image()
.inspect(ImageReference.of("example.com/test/cmd-property-name:v1"));
assertThat(image.getCreated()).isEqualTo("2020-07-01T12:34:56Z");
removeImage("example.com/test/cmd-property-name", "v1");
});
}
@@ -387,6 +393,47 @@ class BuildImageTests extends AbstractArchiveIntegrationTests {
});
}
@TestTemplate
void whenBuildImageIsInvokedWithCreatedDate(MavenBuild mavenBuild) {
String testBuildId = randomString();
mavenBuild.project("build-image-created-date")
.goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.systemProperty("test-build-id", testBuildId)
.execute((project) -> {
assertThat(buildLog(project)).contains("Building image")
.contains("docker.io/library/build-image-created-date:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
Image image = new DockerApi().image()
.inspect(ImageReference.of("docker.io/library/build-image-created-date:0.0.1.BUILD-SNAPSHOT"));
assertThat(image.getCreated()).isEqualTo("2020-07-01T12:34:56Z");
removeImage("build-image-created-date", "0.0.1.BUILD-SNAPSHOT");
});
}
@TestTemplate
void whenBuildImageIsInvokedWithCurrentCreatedDate(MavenBuild mavenBuild) {
String testBuildId = randomString();
mavenBuild.project("build-image-current-created-date")
.goals("package")
.systemProperty("spring-boot.build-image.pullPolicy", "IF_NOT_PRESENT")
.systemProperty("test-build-id", testBuildId)
.execute((project) -> {
assertThat(buildLog(project)).contains("Building image")
.contains("docker.io/library/build-image-current-created-date:0.0.1.BUILD-SNAPSHOT")
.contains("Successfully built image");
Image image = new DockerApi().image()
.inspect(ImageReference
.of("docker.io/library/build-image-current-created-date:0.0.1.BUILD-SNAPSHOT"));
OffsetDateTime createdDateTime = OffsetDateTime.parse(image.getCreated());
OffsetDateTime current = OffsetDateTime.now();
assertThat(createdDateTime.getYear()).isEqualTo(current.getYear());
assertThat(createdDateTime.getMonth()).isEqualTo(current.getMonth());
assertThat(createdDateTime.getDayOfMonth()).isEqualTo(current.getDayOfMonth());
removeImage("build-image-current-created-date", "0.0.1.BUILD-SNAPSHOT");
});
}
@TestTemplate
void failsWhenBuildImageIsInvokedOnMultiModuleProjectWithBuildImageGoal(MavenBuild mavenBuild) {
mavenBuild.project("build-image-multi-module")

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<buildpacks>
<buildpack>urn:cnb:builder:example/does-not-exist:0.0.1</buildpack>
</buildpacks>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<bindings>
<binding>${basedir}/bindings/ca-certificates:/platform/bindings/ca-certificates</binding>
</bindings>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<env>
<FORCE_FAILURE>true</FORCE_FAILURE>
</env>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<buildCache>
<volume>
<name>build-cache-volume1</name>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<buildCache>
<volume>
<name>cache-${test-build-id}.build</name>

View File

@@ -45,7 +45,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -39,7 +39,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -29,7 +29,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -19,7 +19,7 @@
<version>@project.version@</version>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</plugin>

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-image-created-date</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<createdDate>2020-07-01T12:34:56Z</createdDate>
</image>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.test;
public class SampleApplication {
public static void main(String[] args) throws Exception {
System.out.println("Launched");
synchronized(args) {
args.wait(); // Prevent exit"
}
}
}

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot.maven.it</groupId>
<artifactId>build-image-current-created-date</artifactId>
<version>0.0.1.BUILD-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>@java.version@</maven.compiler.source>
<maven.compiler.target>@java.version@</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build-image-no-fork</goal>
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<createdDate>now</createdDate>
</image>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.test;
public class SampleApplication {
public static void main(String[] args) throws Exception {
System.out.println("Launched");
synchronized(args) {
args.wait(); // Prevent exit"
}
}
}

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<runImage>projects.registry.vmware.com/springboot/run:tiny-cnb</runImage>
</image>
</configuration>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<buildpacks>
<buildpack>urn:cnb:builder:spring-boot/test-info</buildpack>
</buildpacks>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<name>example.com/test/build-image:${project.version}</name>
</image>
</configuration>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<env>
<EMPTY_KEY></EMPTY_KEY>
</env>

View File

@@ -25,7 +25,7 @@
<configuration>
<finalName>final-name</finalName>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -31,7 +31,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<network>none</network>
</image>
</configuration>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<publish>true</publish>
</image>
</configuration>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
<tags>
<tag>${project.artifactId}:latest</tag>
</tags>

View File

@@ -24,7 +24,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -29,7 +29,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -26,7 +26,7 @@
<configuration>
<layout>ZIP</layout>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</plugin>

View File

@@ -23,7 +23,7 @@
</goals>
<configuration>
<image>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1</builder>
<builder>projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2</builder>
</image>
</configuration>
</execution>

View File

@@ -155,6 +155,14 @@ public abstract class BuildImageMojo extends AbstractPackagerMojo {
@Parameter(property = "spring-boot.build-image.network", readonly = true)
String network;
/**
* Alias for {@link Image#createdDate} to support configuration through command-line
* property.
* @since 3.1.0
*/
@Parameter(property = "spring-boot.build-image.createdDate", readonly = true)
String createdDate;
/**
* Docker configuration options.
* @since 2.4.0
@@ -253,6 +261,9 @@ public abstract class BuildImageMojo extends AbstractPackagerMojo {
if (image.network == null && this.network != null) {
image.setNetwork(this.network);
}
if (image.createdDate == null && this.createdDate != null) {
image.setCreatedDate(this.createdDate);
}
return customize(image.getBuildRequest(this.project.getArtifact(), content));
}

View File

@@ -73,6 +73,8 @@ public class Image {
CacheInfo launchCache;
String createdDate;
/**
* The name of the created image.
* @return the image name
@@ -173,6 +175,18 @@ public class Image {
this.network = network;
}
/**
* Returns the created date for the image.
* @return the created date
*/
public String getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
BuildRequest getBuildRequest(Artifact artifact, Function<Owner, TarArchive> applicationContent) {
return customize(BuildRequest.of(getOrDeduceName(artifact), applicationContent));
}
@@ -221,6 +235,9 @@ public class Image {
if (this.launchCache != null) {
request = request.withLaunchCache(this.launchCache.asCache());
}
if (StringUtils.hasText(this.createdDate)) {
request = request.withCreatedDate(this.createdDate);
}
return request;
}

View File

@@ -185,6 +185,14 @@ class ImageTests {
assertThat(request.getLaunchCache()).isEqualTo(Cache.volume("launch-cache-vol"));
}
@Test
void getBuildRequestWhenHasCreatedDateUsesCreatedDate() {
Image image = new Image();
image.createdDate = "2020-07-01T12:34:56Z";
BuildRequest request = image.getBuildRequest(createArtifact(), mockApplicationContent());
assertThat(request.getCreatedDate()).isEqualTo("2020-07-01T12:34:56Z");
}
private Artifact createArtifact() {
return new DefaultArtifact("com.example", "my-app", VersionRange.createFromVersion("0.0.1-SNAPSHOT"), "compile",
"jar", null, new DefaultArtifactHandler());