Add runImage option for image building

This commit adds a runImage property to the Maven plugin build-image
goal and the Gradle bootBuildImage task. The property allows the user
to override the run image reference provided in the builder metadata
with an alternate run image. The runImage property can be specified
in the build file or on the command line.

Fixes gh-21534
This commit is contained in:
Scott Frederick
2020-06-11 16:20:39 -05:00
parent 025d7aaac8
commit 6119d69679
27 changed files with 427 additions and 89 deletions

View File

@@ -50,6 +50,11 @@ The following table summarizes the available properties and their default values
| Name of the Builder image to use.
| `gcr.io/paketo-buildpacks/builder:base-platform-api-0.3`
| `runImage`
| `--runImage`
| Name of the run image to use.
| No default value, indicating the run image specified in Builder metadata should be used.
| `imageName`
| `--imageName`
| {spring-boot-api}/buildpack/platform/docker/type/ImageReference.html#of-java.lang.String-[Image name] for the generated image.
@@ -79,8 +84,8 @@ The following table summarizes the available properties and their default values
[[build-image-example-custom-image-builder]]
==== Custom Image Builder
If you need to customize the builder used to create the image, configure the task as shown in the following example:
==== Custom Image Builder and Run Image
If you need to customize the builder used to create the image or the run image used to launch the built image, configure the task as shown in the following example:
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
.Groovy
@@ -94,13 +99,13 @@ include::../gradle/packaging/boot-build-image-builder.gradle[tags=builder]
include::../gradle/packaging/boot-build-image-builder.gradle.kts[tags=builder]
----
This configuration will use a builder image with the name `mine/java-cnb-builder` and the tag `latest`.
This configuration will use a builder image with the name `mine/java-cnb-builder` and the tag `latest`, and the run image named `mine/java-cnb-run` and the tag `latest`.
The builder can be specified on the command line as well, as shown in this example:
The builder and run image can be specified on the command line as well, as shown in this example:
[indent=0]
----
$ gradle bootBuildImage --builder=mine/java-cnb-builder
$ gradle bootBuildImage --builder=mine/java-cnb-builder --runImage=mine/java-cnb-run
----

View File

@@ -10,5 +10,6 @@ bootJar {
// tag::builder[]
bootBuildImage {
builder = "mine/java-cnb-builder"
runImage = "mine/java-cnb-run"
}
// end::builder[]

View File

@@ -12,5 +12,6 @@ tasks.getByName<BootJar>("bootJar") {
// tag::builder[]
tasks.getByName<BootBuildImage>("bootBuildImage") {
builder = "mine/java-cnb-builder"
runImage = "mine/java-cnb-run"
}
// end::builder[]

View File

@@ -61,6 +61,8 @@ public class BootBuildImage extends DefaultTask {
private String builder;
private String runImage;
private Map<String, String> environment = new HashMap<>();
private boolean cleanCache;
@@ -133,6 +135,26 @@ public class BootBuildImage extends DefaultTask {
this.builder = builder;
}
/**
* Returns the run image that will be included in the built image. When {@code null},
* the run image bundled with the builder will be used.
* @return the run image
*/
@Input
@Optional
public String getRunImage() {
return this.runImage;
}
/**
* Sets the run image that will be included in the built image.
* @param runImage the run image
*/
@Option(option = "runImage", description = "The name of the run image to use")
public void setRunImage(String runImage) {
this.runImage = runImage;
}
/**
* Returns the environment that will be used when building the image.
* @return the environment
@@ -228,6 +250,7 @@ public class BootBuildImage extends DefaultTask {
private BuildRequest customize(BuildRequest request) {
request = customizeBuilder(request);
request = customizeRunImage(request);
request = customizeEnvironment(request);
request = customizeCreator(request);
request = request.withCleanCache(this.cleanCache);
@@ -242,6 +265,13 @@ public class BootBuildImage extends DefaultTask {
return request;
}
private BuildRequest customizeRunImage(BuildRequest request) {
if (StringUtils.hasText(this.runImage)) {
return request.withRunImage(ImageReference.of(this.runImage));
}
return request;
}
private BuildRequest customizeEnvironment(BuildRequest request) {
if (this.environment != null && !this.environment.isEmpty()) {
request = request.withEnv(this.environment);

View File

@@ -89,13 +89,14 @@ class BootBuildImageIntegrationTests {
}
@TestTemplate
void buildsImageWithCustomBuilder() throws IOException {
void buildsImageWithCustomBuilderAndRunImage() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-custom");
assertThat(result.getOutput()).contains("paketo-buildpacks/builder:full-cf-platform-api-0.3");
assertThat(result.getOutput()).contains("paketo-buildpacks/run:full-cnb-cf");
ImageReference imageReference = ImageReference.of(ImageName.of("example/test-image-custom"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
@@ -110,10 +111,12 @@ class BootBuildImageIntegrationTests {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage", "--imageName=example/test-image-cmd",
"--builder=gcr.io/paketo-buildpacks/builder:full-cf-platform-api-0.3");
"--builder=gcr.io/paketo-buildpacks/builder:full-cf-platform-api-0.3",
"--runImage=gcr.io/paketo-buildpacks/run:full-cnb-cf");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example/test-image-cmd");
assertThat(result.getOutput()).contains("paketo-buildpacks/builder:full-cf-platform-api-0.3");
assertThat(result.getOutput()).contains("paketo-buildpacks/run:full-cnb-cf");
ImageReference imageReference = ImageReference.of(ImageName.of("example/test-image-cmd"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();

View File

@@ -183,4 +183,15 @@ class BootBuildImageTests {
assertThat(this.buildImage.createRequest().getBuilder().getName()).isEqualTo("test/builder");
}
@Test
void whenNoRunImageIsConfiguredThenRequestUsesDefaultRunImage() {
assertThat(this.buildImage.createRequest().getRunImage()).isNull();
}
@Test
void whenRunImageIsConfiguredThenRequestUsesSpecifiedRunImage() {
this.buildImage.setRunImage("example.com/test/run:1.0");
assertThat(this.buildImage.createRequest().getRunImage().getName()).isEqualTo("test/run");
}
}

View File

@@ -1,12 +0,0 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
imageName = "example/test-image-custom"
builder = "gcr.io/paketo-buildpacks/builder:full-cf-platform-api-0.3"
}

View File

@@ -0,0 +1,13 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
imageName = "example/test-image-custom"
builder = "gcr.io/paketo-buildpacks/builder:full-cf-platform-api-0.3"
runImage = "gcr.io/paketo-buildpacks/run:full-cnb-cf"
}

View File

@@ -6,6 +6,6 @@ plugins {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
imageName = "example/test-image-name"
}
bootBuildImage {
imageName = "example/test-image-name"
}

View File

@@ -6,6 +6,6 @@ plugins {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
environment = ["BP_JVM_VERSION" : "13.9.9"]
}
bootBuildImage {
environment = ["BP_JVM_VERSION": "13.9.9"]
}