Skip to content

Commit a540da1

Browse files
Merge branch '2.4.x' into 2.5.x
Closes gh-27993
2 parents 2c02f5f + c8ff874 commit a540da1

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/Builder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void build(BuildRequest request) throws DockerEngineException, IOExceptio
105105
assertStackIdsMatch(runImage, builderImage);
106106
BuildOwner buildOwner = BuildOwner.fromEnv(builderImage.getConfig().getEnv());
107107
Buildpacks buildpacks = getBuildpacks(request, imageFetcher, builderMetadata);
108-
EphemeralBuilder ephemeralBuilder = new EphemeralBuilder(buildOwner, builderImage, builderMetadata,
109-
request.getCreator(), request.getEnv(), buildpacks);
108+
EphemeralBuilder ephemeralBuilder = new EphemeralBuilder(buildOwner, builderImage, request.getName(),
109+
builderMetadata, request.getCreator(), request.getEnv(), buildpacks);
110110
this.docker.image().load(ephemeralBuilder.getArchive(), UpdateListener.none());
111111
try {
112112
executeLifecycle(request, ephemeralBuilder);

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/build/EphemeralBuilder.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
*/
3535
class EphemeralBuilder {
3636

37+
static final String BUILDER_FOR_LABEL_NAME = "org.springframework.boot.builderFor";
38+
3739
private final BuildOwner buildOwner;
3840

3941
private final BuilderMetadata builderMetadata;
@@ -45,21 +47,24 @@ class EphemeralBuilder {
4547
/**
4648
* Create a new {@link EphemeralBuilder} instance.
4749
* @param buildOwner the build owner
48-
* @param builderImage the image
50+
* @param builderImage the base builder image
51+
* @param targetImage the image being built
4952
* @param builderMetadata the builder metadata
5053
* @param creator the builder creator
5154
* @param env the builder env
5255
* @param buildpacks an optional set of buildpacks to apply
5356
* @throws IOException on IO error
5457
*/
55-
EphemeralBuilder(BuildOwner buildOwner, Image builderImage, BuilderMetadata builderMetadata, Creator creator,
56-
Map<String, String> env, Buildpacks buildpacks) throws IOException {
58+
EphemeralBuilder(BuildOwner buildOwner, Image builderImage, ImageReference targetImage,
59+
BuilderMetadata builderMetadata, Creator creator, Map<String, String> env, Buildpacks buildpacks)
60+
throws IOException {
5761
ImageReference name = ImageReference.random("pack.local/builder/").inTaggedForm();
5862
this.buildOwner = buildOwner;
5963
this.creator = creator;
6064
this.builderMetadata = builderMetadata.copy(this::updateMetadata);
6165
this.archive = ImageArchive.from(builderImage, (update) -> {
6266
update.withUpdatedConfig(this.builderMetadata::attachTo);
67+
update.withUpdatedConfig((config) -> config.withLabel(BUILDER_FOR_LABEL_NAME, targetImage.toString()));
6368
update.withTag(name);
6469
if (env != null && !env.isEmpty()) {
6570
update.withNewLayer(getEnvLayer(env));

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/build/EphemeralBuilderTests.java

+27-14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.util.FileCopyUtils;
4949

5050
import static org.assertj.core.api.Assertions.assertThat;
51+
import static org.assertj.core.api.Assertions.entry;
5152

5253
/**
5354
* Tests for {@link EphemeralBuilder}.
@@ -64,6 +65,8 @@ class EphemeralBuilderTests extends AbstractJsonTests {
6465

6566
private Image image;
6667

68+
private ImageReference targetImage;
69+
6770
private BuilderMetadata metadata;
6871

6972
private Map<String, String> env;
@@ -75,6 +78,7 @@ class EphemeralBuilderTests extends AbstractJsonTests {
7578
@BeforeEach
7679
void setup() throws Exception {
7780
this.image = Image.of(getContent("image.json"));
81+
this.targetImage = ImageReference.of("my-image:latest");
7882
this.metadata = BuilderMetadata.fromImage(this.image);
7983
this.env = new HashMap<>();
8084
this.env.put("spring", "boot");
@@ -83,18 +87,18 @@ void setup() throws Exception {
8387

8488
@Test
8589
void getNameHasRandomName() throws Exception {
86-
EphemeralBuilder b1 = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, this.env,
87-
this.buildpacks);
88-
EphemeralBuilder b2 = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, this.env,
89-
this.buildpacks);
90+
EphemeralBuilder b1 = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
91+
this.creator, this.env, this.buildpacks);
92+
EphemeralBuilder b2 = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
93+
this.creator, this.env, this.buildpacks);
9094
assertThat(b1.getName().toString()).startsWith("pack.local/builder/").endsWith(":latest");
9195
assertThat(b1.getName().toString()).isNotEqualTo(b2.getName().toString());
9296
}
9397

9498
@Test
9599
void getArchiveHasCreatedByConfig() throws Exception {
96-
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, this.env,
97-
this.buildpacks);
100+
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
101+
this.creator, this.env, this.buildpacks);
98102
ImageConfig config = builder.getArchive().getImageConfig();
99103
BuilderMetadata ephemeralMetadata = BuilderMetadata.fromImageConfig(config);
100104
assertThat(ephemeralMetadata.getCreatedBy().getName()).isEqualTo("Spring Boot");
@@ -103,16 +107,16 @@ void getArchiveHasCreatedByConfig() throws Exception {
103107

104108
@Test
105109
void getArchiveHasTag() throws Exception {
106-
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, this.env,
107-
this.buildpacks);
110+
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
111+
this.creator, this.env, this.buildpacks);
108112
ImageReference tag = builder.getArchive().getTag();
109113
assertThat(tag.toString()).startsWith("pack.local/builder/").endsWith(":latest");
110114
}
111115

112116
@Test
113117
void getArchiveHasFixedCreateDate() throws Exception {
114-
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, this.env,
115-
this.buildpacks);
118+
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
119+
this.creator, this.env, this.buildpacks);
116120
Instant createInstant = builder.getArchive().getCreateDate();
117121
OffsetDateTime createDateTime = OffsetDateTime.ofInstant(createInstant, ZoneId.of("UTC"));
118122
assertThat(createDateTime.getYear()).isEqualTo(1980);
@@ -125,22 +129,31 @@ void getArchiveHasFixedCreateDate() throws Exception {
125129

126130
@Test
127131
void getArchiveContainsEnvLayer() throws Exception {
128-
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, this.env,
129-
this.buildpacks);
132+
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
133+
this.creator, this.env, this.buildpacks);
130134
File directory = unpack(getLayer(builder.getArchive(), 0), "env");
131135
assertThat(new File(directory, "platform/env/spring")).usingCharset(StandardCharsets.UTF_8).hasContent("boot");
132136
assertThat(new File(directory, "platform/env/empty")).usingCharset(StandardCharsets.UTF_8).hasContent("");
133137
}
134138

139+
@Test
140+
void getArchiveHasBuilderForLabel() throws Exception {
141+
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
142+
this.creator, this.env, this.buildpacks);
143+
ImageConfig config = builder.getArchive().getImageConfig();
144+
assertThat(config.getLabels())
145+
.contains(entry(EphemeralBuilder.BUILDER_FOR_LABEL_NAME, this.targetImage.toString()));
146+
}
147+
135148
@Test
136149
void getArchiveContainsBuildpackLayers() throws Exception {
137150
List<Buildpack> buildpackList = new ArrayList<>();
138151
buildpackList.add(new TestBuildpack("example/buildpack1", "0.0.1"));
139152
buildpackList.add(new TestBuildpack("example/buildpack2", "0.0.2"));
140153
buildpackList.add(new TestBuildpack("example/buildpack3", "0.0.3"));
141154
this.buildpacks = Buildpacks.of(buildpackList);
142-
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.metadata, this.creator, null,
143-
this.buildpacks);
155+
EphemeralBuilder builder = new EphemeralBuilder(this.owner, this.image, this.targetImage, this.metadata,
156+
this.creator, null, this.buildpacks);
144157
assertBuildpackLayerContent(builder, 0, "/cnb/buildpacks/example_buildpack1/0.0.1/buildpack.toml");
145158
assertBuildpackLayerContent(builder, 1, "/cnb/buildpacks/example_buildpack2/0.0.2/buildpack.toml");
146159
assertBuildpackLayerContent(builder, 2, "/cnb/buildpacks/example_buildpack3/0.0.3/buildpack.toml");

0 commit comments

Comments
 (0)