Commit 3f378e12 authored by Scott Frederick's avatar Scott Frederick

Add command-line options for Gradle bootBuildImage parameters

This commit adds support for setting the image name and builder
parameters of the Gradle bootBuildImage task using command-line
options as an alternative to DSL configuration.

See gh-20520
parent b30e5a60
...@@ -17,25 +17,30 @@ Task properties can be used to configure how the builder should operate on the p ...@@ -17,25 +17,30 @@ Task properties can be used to configure how the builder should operate on the p
The following table summarizes the available properties and their default values: The following table summarizes the available properties and their default values:
|=== |===
| Property | Description | Default value | Property | Command-line option | Description | Default value
| `builder` | `builder`
| `--builder`
| Name of the Builder image to use. | Name of the Builder image to use.
| `cloudfoundry/cnb:0.0.53-bionic` | `cloudfoundry/cnb:0.0.53-bionic`
| `imageName` | `imageName`
| `--imageName`
| {spring-boot-api}/buildpack/platform/docker/type/ImageReference.html#of-java.lang.String-[Image name] for the generated image. | {spring-boot-api}/buildpack/platform/docker/type/ImageReference.html#of-java.lang.String-[Image name] for the generated image.
| `docker.io/library/${project.artifactId}:${project.version}` | `docker.io/library/${project.artifactId}:${project.version}`
| `environment` | `environment`
|
| Environment variables that should be passed to the builder. | Environment variables that should be passed to the builder.
| |
| `cleanCache` | `cleanCache`
|
| Whether to clean the cache before building. | Whether to clean the cache before building.
| `false` | `false`
| `verboseLogging` | `verboseLogging`
|
| Enables verbose logging of builder operations. | Enables verbose logging of builder operations.
| `false` | `false`
|=== |===
......
...@@ -29,6 +29,7 @@ import org.gradle.api.provider.Property; ...@@ -29,6 +29,7 @@ import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.springframework.boot.buildpack.platform.build.BuildRequest; import org.springframework.boot.buildpack.platform.build.BuildRequest;
import org.springframework.boot.buildpack.platform.build.Builder; import org.springframework.boot.buildpack.platform.build.Builder;
...@@ -107,6 +108,7 @@ public class BootBuildImage extends DefaultTask { ...@@ -107,6 +108,7 @@ public class BootBuildImage extends DefaultTask {
* Sets the name of the image that will be built. * Sets the name of the image that will be built.
* @param imageName name of the image * @param imageName name of the image
*/ */
@Option(option = "imageName", description = "The name of the image to generate")
public void setImageName(String imageName) { public void setImageName(String imageName) {
this.imageName = imageName; this.imageName = imageName;
} }
...@@ -126,6 +128,7 @@ public class BootBuildImage extends DefaultTask { ...@@ -126,6 +128,7 @@ public class BootBuildImage extends DefaultTask {
* Sets the builder that will be used to build the image. * Sets the builder that will be used to build the image.
* @param builder the builder * @param builder the builder
*/ */
@Option(option = "builder", description = "The name of the builder image to use")
public void setBuilder(String builder) { public void setBuilder(String builder) {
this.builder = builder; this.builder = builder;
} }
......
...@@ -58,9 +58,28 @@ class BootBuildImageIntegrationTests { ...@@ -58,9 +58,28 @@ class BootBuildImageIntegrationTests {
writeMainClass(); writeMainClass();
writeLongNameResource(); writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage"); BuildResult result = this.gradleBuild.build("bootBuildImage");
String projectName = this.gradleBuild.getProjectDir().getName();
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("docker.io/library/" + projectName);
assertThat(result.getOutput()).contains("cloudfoundry/cnb:0.0.53-bionic"); assertThat(result.getOutput()).contains("cloudfoundry/cnb:0.0.53-bionic");
ImageReference imageReference = ImageReference.of(ImageName.of(this.gradleBuild.getProjectDir().getName())); ImageReference imageReference = ImageReference.of(ImageName.of(projectName));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
}
finally {
new DockerApi().image().remove(imageReference, false);
}
}
@TestTemplate
void buildsImageWithCustomName() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example.com/test-image-name");
assertThat(result.getOutput()).contains("cloudfoundry/cnb:0.0.53-bionic");
ImageReference imageReference = ImageReference.of(ImageName.of("example.com/test-image-name"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) { try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start(); container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
} }
...@@ -74,9 +93,29 @@ class BootBuildImageIntegrationTests { ...@@ -74,9 +93,29 @@ class BootBuildImageIntegrationTests {
writeMainClass(); writeMainClass();
writeLongNameResource(); writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage"); 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("cloudfoundry/cnb:0.0.43-bionic");
ImageReference imageReference = ImageReference.of(ImageName.of(projectName));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
}
finally {
new DockerApi().image().remove(imageReference, false);
}
}
@TestTemplate
void buildsImageWithCommandLineOptions() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage", "--imageName=example.com/test-image-name",
"--builder=cloudfoundry/cnb:0.0.43-bionic");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("example.com/test-image-name");
assertThat(result.getOutput()).contains("cloudfoundry/cnb:0.0.43-bionic"); assertThat(result.getOutput()).contains("cloudfoundry/cnb:0.0.43-bionic");
ImageReference imageReference = ImageReference.of(ImageName.of(this.gradleBuild.getProjectDir().getName())); ImageReference imageReference = ImageReference.of(ImageName.of("example.com/test-image-name"));
try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) { try (GenericContainer<?> container = new GenericContainer<>(imageReference.toString())) {
container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start(); container.waitingFor(Wait.forLogMessage("Launched\\n", 1)).start();
} }
......
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
bootBuildImage {
imageName = "example.com/test-image-name"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment