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

@@ -193,6 +193,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`
| `--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 JavaPlugin's `targetCompatibility` property.

View File

@@ -260,6 +260,16 @@ public abstract class BootBuildImage extends DefaultTask {
action.execute(this.launchCache);
}
/**
* Returns the date that will be used as the {@code Created} date of the image. When
* {@code null}, a fixed date that enables build reproducibility will be used.
* @return the created date
*/
@Input
@Optional
@Option(option = "createdDate", description = "The date to use as the created date of the image")
public abstract Property<String> getCreatedDate();
/**
* Returns the Docker configuration the builder will use.
* @return docker configuration.
@@ -305,6 +315,7 @@ public abstract class BootBuildImage extends DefaultTask {
request = customizeTags(request);
request = customizeCaches(request);
request = request.withNetwork(getNetwork().getOrNull());
request = customizeCreatedDate(request);
return request;
}
@@ -387,4 +398,12 @@ public abstract class BootBuildImage extends DefaultTask {
return request;
}
private BuildRequest customizeCreatedDate(BuildRequest request) {
String createdDate = getCreatedDate().getOrNull();
if (createdDate != null) {
return request.withCreatedDate(createdDate);
}
return request;
}
}

View File

@@ -26,6 +26,7 @@ import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.time.OffsetDateTime;
import java.util.Random;
import java.util.Set;
@@ -42,6 +43,7 @@ import org.junit.jupiter.api.condition.OS;
import org.springframework.boot.buildpack.platform.docker.DockerApi;
import org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi;
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.ImageReference;
import org.springframework.boot.buildpack.platform.docker.type.VolumeName;
import org.springframework.boot.buildpack.platform.io.FilePermissions;
@@ -140,12 +142,15 @@ class BootBuildImageIntegrationTests {
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT",
"--imageName=example/test-image-cmd",
"--builder=projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1",
"--runImage=projects.registry.vmware.com/springboot/run:tiny-cnb");
"--builder=projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2",
"--runImage=projects.registry.vmware.com/springboot/run:tiny-cnb",
"--createdDate=2020-07-01T12:34:56Z");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-cmd");
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
Image image = new DockerApi().image().inspect(ImageReference.of("example/test-image-cmd"));
assertThat(image.getCreated()).isEqualTo("2020-07-01T12:34:56Z");
removeImages("example/test-image-cmd");
}
@@ -290,6 +295,49 @@ class BootBuildImageIntegrationTests {
deleteVolumes("cache-" + projectName + ".build", "cache-" + projectName + ".launch");
}
@TestTemplate
void buildsImageWithCreatedDate() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
String projectName = this.gradleBuild.getProjectDir().getName();
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
Image image = new DockerApi().image().inspect(ImageReference.of("docker.io/library/" + projectName));
assertThat(image.getCreated()).isEqualTo("2020-07-01T12:34:56Z");
removeImages(projectName);
}
@TestTemplate
void buildsImageWithCurrentCreatedDate() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
String projectName = this.gradleBuild.getProjectDir().getName();
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
assertThat(result.getOutput()).contains("---> Test Info buildpack building");
assertThat(result.getOutput()).contains("---> Test Info buildpack done");
Image image = new DockerApi().image().inspect(ImageReference.of("docker.io/library/" + projectName));
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());
removeImages(projectName);
}
@TestTemplate
void failsWithInvalidCreatedDate() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.buildAndFail("bootBuildImage");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.FAILED);
assertThat(result.getOutput()).contains("Error parsing 'invalid date' as an image created date");
}
@TestTemplate
void failsWithBuilderError() throws IOException {
writeMainClass();

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
bindings = [ "${projectDir}/bindings/ca-certificates:/platform/bindings/certificates" as String ]
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
buildpacks = [ "spring-boot/test-info" ]
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
buildpacks = [ "file://${projectDir}/buildpack/hello-world" as String ]
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
buildpacks = [ "file://${projectDir}/hello-world.tgz" as String ]
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
buildpacks = ["projects.registry.vmware.com/springboot/test-info:latest"]
}

View File

@@ -0,0 +1,10 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
createdDate = "2020-07-01T12:34:56Z"
}

View File

@@ -0,0 +1,10 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
createdDate = "now"
}

View File

@@ -8,7 +8,7 @@ targetCompatibility = '1.8'
bootBuildImage {
imageName = "example/test-image-custom"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
runImage = "projects.registry.vmware.com/springboot/run:tiny-cnb"
pullPolicy = "IF_NOT_PRESENT"
}

View File

@@ -8,6 +8,6 @@ targetCompatibility = '1.8'
bootBuildImage {
imageName = "example/test-image-name"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
}

View File

@@ -11,6 +11,6 @@ bootJar {
}
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
}

View File

@@ -11,7 +11,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
network = "none"
}

View File

@@ -13,6 +13,6 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = PullPolicy.ALWAYS
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
tags = [ "example.com/myapp:latest" ]
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
buildCache {
volume {

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
archiveFile = bootWar.archiveFile
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
buildCache {
volume {
name = "build-cache-volume1"

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
environment = ["FORCE_FAILURE": "true"]
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
buildpacks = [ "urn:cnb:builder:example/does-not-exist:0.0.1" ]
}

View File

@@ -0,0 +1,10 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
createdDate = "invalid date"
}

View File

@@ -7,7 +7,7 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
tags = [ "example/Invalid-Tag-Name" ]
}

View File

@@ -11,6 +11,6 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
pullPolicy = "IF_NOT_PRESENT"
}

View File

@@ -7,6 +7,6 @@ sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.1"
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
publish = true
}