Add option to create tags for a built image

This commit adds configuration to the Maven and Gradle plugins to
allow specifying multiple tag to be created that refer to the built
image.

See gh-27613
This commit is contained in:
Rafael Ceccone
2021-08-09 16:47:43 +02:00
committed by Scott Frederick
parent ca69c8b98c
commit 66f44b0c7f
22 changed files with 433 additions and 16 deletions

View File

@@ -40,7 +40,6 @@ import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.springframework.boot.buildpack.platform.docker.DockerApi;
import org.springframework.boot.buildpack.platform.docker.type.ImageName;
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
import org.springframework.boot.buildpack.platform.io.FilePermissions;
import org.springframework.boot.gradle.junit.GradleCompatibility;
@@ -54,6 +53,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
* @author Scott Frederick
* @author Rafael Ceccone
*/
@GradleCompatibility(configurationCache = true)
@DisabledIfDockerUnavailable
@@ -235,6 +235,21 @@ class BootBuildImageIntegrationTests {
removeImage(projectName);
}
@TestTemplate
void buildsImageWithTag() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.build("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
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");
assertThat(result.getOutput()).contains("example.com/myapp:latest");
removeImage(projectName);
removeImage("example.com/myapp:latest");
}
@TestTemplate
void buildsImageWithLaunchScript() throws IOException {
writeMainClass();
@@ -300,6 +315,16 @@ class BootBuildImageIntegrationTests {
assertThat(result.getOutput()).contains("'urn:cnb:builder:example/does-not-exist:0.0.1' not found in builder");
}
@TestTemplate
void failsWithInvalidTag() throws IOException {
writeMainClass();
writeLongNameResource();
BuildResult result = this.gradleBuild.buildAndFail("bootBuildImage", "--pullPolicy=IF_NOT_PRESENT");
assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.FAILED);
assertThat(result.getOutput()).containsPattern("Unable to parse image reference")
.containsPattern("example/Invalid-Tag-Name");
}
private void writeMainClass() throws IOException {
File examplePackage = new File(this.gradleBuild.getProjectDir(), "src/main/java/example");
examplePackage.mkdirs();
@@ -423,7 +448,7 @@ class BootBuildImageIntegrationTests {
}
private void removeImage(String name) throws IOException {
ImageReference imageReference = ImageReference.of(ImageName.of(name));
ImageReference imageReference = ImageReference.of(name);
new DockerApi().image().remove(imageReference, false);
}

View File

@@ -31,6 +31,7 @@ import org.springframework.boot.buildpack.platform.build.BuildRequest;
import org.springframework.boot.buildpack.platform.build.BuildpackReference;
import org.springframework.boot.buildpack.platform.build.PullPolicy;
import org.springframework.boot.buildpack.platform.docker.type.Binding;
import org.springframework.boot.buildpack.platform.docker.type.ImageReference;
import org.springframework.boot.gradle.junit.GradleProjectBuilder;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,6 +44,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Scott Frederick
* @author Andrey Shlykov
* @author Jeroen Meijer
* @author Rafael Ceccone
*/
class BootBuildImageTests {
@@ -288,4 +290,34 @@ class BootBuildImageTests {
assertThat(this.buildImage.createRequest().getNetwork()).isEqualTo("test");
}
@Test
void whenNoTagsAreConfiguredThenRequestHasNoTags() {
assertThat(this.buildImage.createRequest().getTags()).isEmpty();
}
@Test
void whenTagsAreConfiguredThenRequestHasTags() {
this.buildImage.setTags(
Arrays.asList("my-app:latest", "example.com/my-app:0.0.1-SNAPSHOT", "example.com/my-app:latest"));
assertThat(this.buildImage.createRequest().getTags()).containsExactly(ImageReference.of("my-app:latest"),
ImageReference.of("example.com/my-app:0.0.1-SNAPSHOT"), ImageReference.of("example.com/my-app:latest"));
}
@Test
void whenEntriesAreAddedToTagsThenRequestHasTags() {
this.buildImage
.tags(Arrays.asList("my-app:latest", "example.com/my-app:0.0.1-SNAPSHOT", "example.com/my-app:latest"));
assertThat(this.buildImage.createRequest().getTags()).containsExactly(ImageReference.of("my-app:latest"),
ImageReference.of("example.com/my-app:0.0.1-SNAPSHOT"), ImageReference.of("example.com/my-app:latest"));
}
@Test
void whenIndividualEntriesAreAddedToTagsThenRequestHasTags() {
this.buildImage.tag("my-app:latest");
this.buildImage.tag("example.com/my-app:0.0.1-SNAPSHOT");
this.buildImage.tag("example.com/my-app:latest");
assertThat(this.buildImage.createRequest().getTags()).containsExactly(ImageReference.of("my-app:latest"),
ImageReference.of("example.com/my-app:0.0.1-SNAPSHOT"), ImageReference.of("example.com/my-app:latest"));
}
}