Skip to content

Commit 889cef3

Browse files
Merge pull request #6699 from jlebon/pr/empty-final-layer
imagebuildah: avoid empty layer in single-layer build path
2 parents 73f12be + d5cf595 commit 889cef3

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

imagebuildah/stage_executor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,7 @@ func (s *stageExecutor) execute(ctx context.Context, base string) (imgID string,
14121412
logImageID(imgID)
14131413
}
14141414

1415+
executedLayerStep := false
14151416
for i, node := range children {
14161417
logRusage()
14171418
moreInstructions := i < len(children)-1
@@ -1521,6 +1522,9 @@ func (s *stageExecutor) execute(ctx context.Context, base string) (imgID string,
15211522
// instruction.
15221523
if !s.executor.layers {
15231524
s.didExecute = true
1525+
if s.stepRequiresLayer(step) {
1526+
executedLayerStep = true
1527+
}
15241528
err := ib.Run(step, s, noRunsRemaining)
15251529
if err != nil {
15261530
logrus.Debugf("Error building at step %+v: %v", *step, err)
@@ -1555,7 +1559,7 @@ func (s *stageExecutor) execute(ctx context.Context, base string) (imgID string,
15551559
if err != nil {
15561560
return "", nil, false, fmt.Errorf("unable to get createdBy for the node: %w", err)
15571561
}
1558-
imgID, commitResults, err = s.commit(ctx, createdBy, false, s.output, s.executor.squash, lastStage && lastInstruction)
1562+
imgID, commitResults, err = s.commit(ctx, createdBy, !executedLayerStep, s.output, s.executor.squash, lastStage && lastInstruction)
15591563
if err != nil {
15601564
return "", nil, false, fmt.Errorf("committing container for step %+v: %w", *step, err)
15611565
}

tests/bud.bats

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ _EOF
141141
expect_output "0" "layer should not exist"
142142
}
143143

144+
@test "no empty layer for metadata-only instructions without --layers" {
145+
_prefetch alpine
146+
imgname="img-$(safename)"
147+
148+
# Get the base image layer count
149+
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' alpine
150+
base_layers="$output"
151+
152+
# Build without --layers (single-layer mode) with only metadata instructions
153+
run_buildah build --layers=false -t $imgname -f $BUDFILES/metadata-only/Containerfile
154+
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' $imgname
155+
expect_output "$base_layers" "metadata-only instructions should not add a layer"
156+
}
157+
144158
@test "bud and test --inherit-annotations" {
145159
base=quay.io/libpod/testimage:20241011
146160
_prefetch $base
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM alpine
2+
LABEL testlabel=testvalue
3+
ENV TESTVAR=testvalue
4+
USER nobody
5+
CMD ["/bin/sh"]

0 commit comments

Comments
 (0)