Add support for build workspace option when building images
Closes gh-37478
This commit is contained in:
@@ -193,14 +193,22 @@ The value supplied will be passed unvalidated to Docker when creating the builde
|
||||
The values provided to the `tags` option should be full image references in the form of `[image name]:[tag]` or `[repository]/[image name]:[tag]`.
|
||||
|
|
||||
|
||||
| `buildWorkspace`
|
||||
|
|
||||
| A temporary workspace that will be used by the builder and buildpacks to store files during image building.
|
||||
The value can be a named volume or a bind mount location.
|
||||
| A named volume in the Docker daemon, with a name derived from the image name.
|
||||
|
||||
| `buildCache`
|
||||
|
|
||||
| A cache containing layers created by buildpacks and used by the image building process.
|
||||
The value can be a named volume or a bind mount location.
|
||||
| A named volume in the Docker daemon, with a name derived from the image name.
|
||||
|
||||
| `launchCache`
|
||||
|
|
||||
| A cache containing layers created by buildpacks and used by the image launching process.
|
||||
The value can be a named volume or a bind mount location.
|
||||
| A named volume in the Docker daemon, with a name derived from the image name.
|
||||
|
||||
| `createdDate`
|
||||
@@ -420,7 +428,7 @@ The publish option can be specified on the command line as well, as shown in thi
|
||||
----
|
||||
|
||||
[[build-image.examples.caches]]
|
||||
=== Builder Cache Configuration
|
||||
=== Builder Cache and Workspace Configuration
|
||||
|
||||
The CNB builder caches layers that are used when building and launching an image.
|
||||
By default, these caches are stored as named volumes in the Docker daemon with names that are derived from the full name of the target image.
|
||||
@@ -440,7 +448,10 @@ include::../gradle/packaging/boot-build-image-caches.gradle[tags=caches]
|
||||
include::../gradle/packaging/boot-build-image-caches.gradle.kts[tags=caches]
|
||||
----
|
||||
|
||||
The caches can be configured to use bind mounts instead of named volumes, as shown in the following example:
|
||||
Builders and buildpacks need a location to store temporary files during image building.
|
||||
By default, this temporary build workspace is stored in a named volume.
|
||||
|
||||
The caches and the build workspace can be configured to use bind mounts instead of named volumes, as shown in the following example:
|
||||
|
||||
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
|
||||
.Groovy
|
||||
|
||||
@@ -9,6 +9,11 @@ tasks.named("bootJar") {
|
||||
|
||||
// tag::caches[]
|
||||
tasks.named("bootBuildImage") {
|
||||
buildWorkspace {
|
||||
bind {
|
||||
source = "/tmp/cache-${rootProject.name}.work"
|
||||
}
|
||||
}
|
||||
buildCache {
|
||||
bind {
|
||||
source = "/tmp/cache-${rootProject.name}.build"
|
||||
@@ -24,6 +29,7 @@ tasks.named("bootBuildImage") {
|
||||
|
||||
tasks.register("bootBuildImageCaches") {
|
||||
doFirst {
|
||||
bootBuildImage.buildWorkspace.asCache().with { print "buildWorkspace=$source" }
|
||||
bootBuildImage.buildCache.asCache().with { println "buildCache=$source" }
|
||||
bootBuildImage.launchCache.asCache().with { println "launchCache=$source" }
|
||||
}
|
||||
|
||||
@@ -7,6 +7,11 @@ plugins {
|
||||
|
||||
// tag::caches[]
|
||||
tasks.named<BootBuildImage>("bootBuildImage") {
|
||||
buildWorkspace {
|
||||
bind {
|
||||
source.set("/tmp/cache-${rootProject.name}.work")
|
||||
}
|
||||
}
|
||||
buildCache {
|
||||
bind {
|
||||
source.set("/tmp/cache-${rootProject.name}.build")
|
||||
@@ -22,6 +27,7 @@ tasks.named<BootBuildImage>("bootBuildImage") {
|
||||
|
||||
tasks.register("bootBuildImageCaches") {
|
||||
doFirst {
|
||||
println("buildWorkspace=" + tasks.getByName<BootBuildImage>("bootBuildImage").buildWorkspace.asCache().bind.source)
|
||||
println("buildCache=" + tasks.getByName<BootBuildImage>("bootBuildImage").buildCache.asCache().bind.source)
|
||||
println("launchCache=" + tasks.getByName<BootBuildImage>("bootBuildImage").launchCache.asCache().bind.source)
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ public abstract class BootBuildImage extends DefaultTask {
|
||||
|
||||
private final String projectName;
|
||||
|
||||
private final CacheSpec buildWorkspace;
|
||||
|
||||
private final CacheSpec buildCache;
|
||||
|
||||
private final CacheSpec launchCache;
|
||||
@@ -91,6 +93,7 @@ public abstract class BootBuildImage extends DefaultTask {
|
||||
getCleanCache().convention(false);
|
||||
getVerboseLogging().convention(false);
|
||||
getPublish().convention(false);
|
||||
this.buildWorkspace = getProject().getObjects().newInstance(CacheSpec.class);
|
||||
this.buildCache = getProject().getObjects().newInstance(CacheSpec.class);
|
||||
this.launchCache = getProject().getObjects().newInstance(CacheSpec.class);
|
||||
this.docker = getProject().getObjects().newInstance(DockerSpec.class);
|
||||
@@ -222,6 +225,25 @@ public abstract class BootBuildImage extends DefaultTask {
|
||||
@Option(option = "network", description = "Connect detect and build containers to network")
|
||||
public abstract Property<String> getNetwork();
|
||||
|
||||
/**
|
||||
* Returns the build temporary workspace that will be used when building the image.
|
||||
* @return the cache
|
||||
*/
|
||||
@Nested
|
||||
@Optional
|
||||
public CacheSpec getBuildWorkspace() {
|
||||
return this.buildWorkspace;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customizes the {@link CacheSpec} for the build temporary workspace using the given
|
||||
* {@code action}.
|
||||
* @param action the action
|
||||
*/
|
||||
public void buildWorkspace(Action<CacheSpec> action) {
|
||||
action.execute(this.buildWorkspace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the build cache that will be used when building the image.
|
||||
* @return the cache
|
||||
@@ -400,6 +422,9 @@ public abstract class BootBuildImage extends DefaultTask {
|
||||
}
|
||||
|
||||
private BuildRequest customizeCaches(BuildRequest request) {
|
||||
if (this.buildWorkspace.asCache() != null) {
|
||||
request = request.withBuildWorkspace((this.buildWorkspace.asCache()));
|
||||
}
|
||||
if (this.buildCache.asCache() != null) {
|
||||
request = request.withBuildCache(this.buildCache.asCache());
|
||||
}
|
||||
|
||||
@@ -343,7 +343,8 @@ class PackagingDocumentationTests {
|
||||
void bootBuildImageWithBindCaches() {
|
||||
BuildResult result = this.gradleBuild.script("src/docs/gradle/packaging/boot-build-image-bind-caches")
|
||||
.build("bootBuildImageCaches");
|
||||
assertThat(result.getOutput()).containsPattern("buildCache=/tmp/cache-gradle-[\\d]+.build")
|
||||
assertThat(result.getOutput()).containsPattern("buildWorkspace=/tmp/cache-gradle-[\\d]+.work")
|
||||
.containsPattern("buildCache=/tmp/cache-gradle-[\\d]+.build")
|
||||
.containsPattern("launchCache=/tmp/cache-gradle-[\\d]+.launch");
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,11 @@ java {
|
||||
bootBuildImage {
|
||||
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
|
||||
pullPolicy = "IF_NOT_PRESENT"
|
||||
buildWorkspace {
|
||||
bind {
|
||||
source = System.getProperty('java.io.tmpdir') + "/junit-image-pack-${rootProject.name}-work"
|
||||
}
|
||||
}
|
||||
buildCache {
|
||||
bind {
|
||||
source = System.getProperty('java.io.tmpdir') + "/junit-image-cache-${rootProject.name}-build"
|
||||
|
||||
@@ -11,6 +11,11 @@ java {
|
||||
bootBuildImage {
|
||||
builder = "projects.registry.vmware.com/springboot/spring-boot-cnb-builder:0.0.2"
|
||||
pullPolicy = "IF_NOT_PRESENT"
|
||||
buildWorkspace {
|
||||
volume {
|
||||
name = "pack-${rootProject.name}.work"
|
||||
}
|
||||
}
|
||||
buildCache {
|
||||
volume {
|
||||
name = "cache-${rootProject.name}.build"
|
||||
|
||||
Reference in New Issue
Block a user