Skip to content

Commit

Permalink
stage_executor: create layer if only empty layer is present
Browse files Browse the repository at this point in the history
A build case where base is `scratch` and only a `label` is added then no
layer is created, its like tagging empty image, in such cases create a
layer.

Closes: containers/podman#19860

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed Sep 7, 2023
1 parent 1a001de commit 25ef8bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,13 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
}
}
} else {
emptyLayer := !s.stepRequiresLayer(step)
// If lastIntruction of this stage and there was only single step and base image is `scratch` then commit this layer
// instead of ignoring this as an `emptyLayer`. i.e do not create `emptyLayer` if only single layer is present in the
// entire build.
if len(stage.Node.Children) == 1 && lastInstruction && s.builder.FromImage == "" && s.builder.FromImageID == "" && emptyLayer {
emptyLayer = false
}
// We're not going to find any more cache hits, so we
// can stop looking for them.
checkForLayers = false
Expand All @@ -1420,7 +1427,7 @@ func (s *StageExecutor) Execute(ctx context.Context, base string) (imgID string,
// because at this point we want to save history for
// layers even if its a squashed build so that they
// can be part of build-cache.
imgID, ref, err = s.commit(ctx, s.getCreatedBy(node, addedContentSummary), !s.stepRequiresLayer(step), commitName, false)
imgID, ref, err = s.commit(ctx, s.getCreatedBy(node, addedContentSummary), emptyLayer, commitName, false)
if err != nil {
return "", nil, fmt.Errorf("committing container for step %+v: %w", *step, err)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5782,6 +5782,12 @@ _EOF
run_buildah rmi -f ${target}
}

@test "layer must be created on scratch and label" {
run_buildah build --layers --label "label1=value1" -t test -f $BUDFILES/from-scratch/Containerfile
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' test
expect_output "1" "layer must exist"
}


@test "bud-from-relabel" {
_prefetch alpine busybox
Expand Down

0 comments on commit 25ef8bc

Please sign in to comment.