diff --git a/imagebuildah/executor.go b/imagebuildah/executor.go index 69ad23bb7d7..5f23a5826a2 100644 --- a/imagebuildah/executor.go +++ b/imagebuildah/executor.go @@ -470,33 +470,33 @@ func (b *Executor) buildStage(ctx context.Context, cleanupStages map[int]*StageE output := "" if stageIndex == len(stages)-1 { output = b.output - } - // Check if any labels were passed in via the API, and add a final line - // to the Dockerfile that would provide the same result. - // Reason: Docker adds label modification as a last step which can be - // processed like regular steps, and if no modification is done to - // layers, its easier to re-use cached layers. - if len(b.labels) > 0 { - var labelLine string - labels := append([]string{}, b.labels...) - for _, labelSpec := range labels { - label := strings.SplitN(labelSpec, "=", 2) - key := label[0] - value := "" - if len(label) > 1 { - value = label[1] - } - // check only for an empty key since docker allows empty values - if key != "" { - labelLine += fmt.Sprintf(" %q=%q", key, value) + // Check if any labels were passed in via the API, and add a final line + // to the Dockerfile that would provide the same result. + // Reason: Docker adds label modification as a last step which can be + // processed like regular steps, and if no modification is done to + // layers, its easier to re-use cached layers. + if len(b.labels) > 0 { + var labelLine string + labels := append([]string{}, b.labels...) + for _, labelSpec := range labels { + label := strings.SplitN(labelSpec, "=", 2) + key := label[0] + value := "" + if len(label) > 1 { + value = label[1] + } + // check only for an empty key since docker allows empty values + if key != "" { + labelLine += fmt.Sprintf(" %q=%q", key, value) + } } - } - if len(labelLine) > 0 { - additionalNode, err := imagebuilder.ParseDockerfile(strings.NewReader("LABEL" + labelLine + "\n")) - if err != nil { - return "", nil, fmt.Errorf("while adding additional LABEL step: %w", err) + if len(labelLine) > 0 { + additionalNode, err := imagebuilder.ParseDockerfile(strings.NewReader("LABEL" + labelLine + "\n")) + if err != nil { + return "", nil, fmt.Errorf("while adding additional LABEL step: %w", err) + } + stage.Node.Children = append(stage.Node.Children, additionalNode.Children...) } - stage.Node.Children = append(stage.Node.Children, additionalNode.Children...) } } diff --git a/tests/bud.bats b/tests/bud.bats index 39fa5d16aca..064381bc88b 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -1965,6 +1965,17 @@ _EOF done run_buildah inspect --format '{{ index .Docker.Config.Labels "somefancylabel"}}' imagetwo expect_output "" + + # build another multi-stage image with different label, it should use stages from cache from previous build + run_buildah build --layers $WITH_POLICY_JSON --label anotherfancylabel=true -t imagethree -f Dockerfile.name $BUDFILES/multi-stage-builds + # must use all steps from cache but should not contain label + for i in 2 6;do + expect_output --substring --from="${lines[$i]}" "Using cache" + done + run_buildah inspect --format '{{ index .Docker.Config.Labels "somefancylabel"}}' imagetwo + expect_output "" + run_buildah inspect --format '{{ index .Docker.Config.Labels "anotherfancylabel"}}' imagethree + expect_output "true" } @test "bud-multi-stage-builds" {