Add pullPolicy option for image building

This commit adds a pullPolicy option to the configuration of the Maven
plugin spring-boot:build-image goal and the Gradle plugin bootBuildImage
task. The new option gives users control over pulling the builder image
and run image from a remote image registry to the local Docker daemon.

See gh-22736
This commit is contained in:
anshlykov
2020-08-03 02:24:07 +03:00
committed by Scott Frederick
parent b35cfb7fb7
commit c7449b57ce
15 changed files with 371 additions and 33 deletions

View File

@@ -34,6 +34,7 @@ import org.gradle.api.tasks.options.Option;
import org.springframework.boot.buildpack.platform.build.BuildRequest;
import org.springframework.boot.buildpack.platform.build.Builder;
import org.springframework.boot.buildpack.platform.build.Creator;
import org.springframework.boot.buildpack.platform.build.PullPolicy;
import org.springframework.boot.buildpack.platform.docker.transport.DockerEngineException;
import org.springframework.boot.buildpack.platform.docker.type.ImageName;
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
@@ -69,6 +70,8 @@ public class BootBuildImage extends DefaultTask {
private boolean verboseLogging;
private PullPolicy pullPolicy;
public BootBuildImage() {
this.jar = getProject().getObjects().fileProperty();
this.targetJavaVersion = getProject().getObjects().property(JavaVersion.class);
@@ -224,6 +227,25 @@ public class BootBuildImage extends DefaultTask {
this.verboseLogging = verboseLogging;
}
/**
* Returns image pull policy that will be used when building the image.
* @return whether images should be pulled
*/
@Input
@Optional
public PullPolicy getPullPolicy() {
return this.pullPolicy;
}
/**
* Sets image pull policy that will be used when building the image.
* @param pullPolicy image pull policy {@link PullPolicy}
*/
@Option(option = "pullPolicy", description = "The image pull policy")
public void setPullPolicy(PullPolicy pullPolicy) {
this.pullPolicy = pullPolicy;
}
@TaskAction
void buildImage() throws DockerEngineException, IOException {
Builder builder = new Builder();
@@ -255,6 +277,7 @@ public class BootBuildImage extends DefaultTask {
request = customizeCreator(request);
request = request.withCleanCache(this.cleanCache);
request = request.withVerboseLogging(this.verboseLogging);
request = customizePullPolicy(request);
return request;
}
@@ -290,6 +313,13 @@ public class BootBuildImage extends DefaultTask {
return request;
}
private BuildRequest customizePullPolicy(BuildRequest request) {
if (this.pullPolicy != null) {
request = request.withPullPolicy(this.pullPolicy);
}
return request;
}
private String translateTargetJavaVersion() {
return this.targetJavaVersion.get().getMajorVersion() + ".*";
}

View File

@@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.buildpack.platform.build.BuildRequest;
import org.springframework.boot.buildpack.platform.build.PullPolicy;
import static org.assertj.core.api.Assertions.assertThat;
@@ -194,4 +195,15 @@ class BootBuildImageTests {
assertThat(this.buildImage.createRequest().getRunImage().getName()).isEqualTo("test/run");
}
@Test
void whenUsingDefaultConfigurationThenRequestHasNoPullDisabled() {
assertThat(this.buildImage.createRequest().getPullPolicy()).isEqualTo(PullPolicy.ALWAYS);
}
@Test
void whenNoPullIsEnabledThenRequestHasNoPullEnabled() {
this.buildImage.setPullPolicy(PullPolicy.NEVER);
assertThat(this.buildImage.createRequest().getPullPolicy()).isEqualTo(PullPolicy.NEVER);
}
}