diff --git a/task.go b/task.go index d4fd215..fdbc00d 100644 --- a/task.go +++ b/task.go @@ -66,7 +66,12 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro var imagePaths []string for _, t := range cfg.AdditionalTargets { - targetArgs := append(buildctlArgs, "--opt", "target="+t) + // prevent re-use of the buildctlArgs slice as it is appended to later on, + // and that would clobber args for all targets if the slice was re-used + targetArgs := make([]string, len(buildctlArgs)) + copy(targetArgs, buildctlArgs) + + targetArgs = append(targetArgs, "--opt", "target="+t) targetDir := filepath.Join(outputsDir, t) diff --git a/task_test.go b/task_test.go index 4559025..1398312 100644 --- a/task_test.go +++ b/task_test.go @@ -319,6 +319,32 @@ func (s *TaskSuite) TestMultiTarget() { s.Equal("additional-target", additionalCfg.Config.Labels["target"]) } +func (s *TaskSuite) TestMultiTargetExplicitTarget() { + s.req.Config.ContextDir = "testdata/multi-target" + s.req.Config.AdditionalTargets = []string{"additional-target"} + s.req.Config.Target = "final-target" + + err := os.Mkdir(s.outputPath("additional-target"), 0755) + s.NoError(err) + + _, err = s.build() + s.NoError(err) + + finalImage, err := tarball.ImageFromPath(s.imagePath("image.tar"), nil) + s.NoError(err) + + finalCfg, err := finalImage.ConfigFile() + s.NoError(err) + s.Equal("final-target", finalCfg.Config.Labels["target"]) + + additionalImage, err := tarball.ImageFromPath(s.outputPath("additional-target", "image.tar"), nil) + s.NoError(err) + + additionalCfg, err := additionalImage.ConfigFile() + s.NoError(err) + s.Equal("additional-target", additionalCfg.Config.Labels["target"]) +} + func (s *TaskSuite) TestMultiTargetDigest() { s.req.Config.ContextDir = "testdata/multi-target" s.req.Config.AdditionalTargets = []string{"additional-target"} diff --git a/testdata/multi-target/Dockerfile b/testdata/multi-target/Dockerfile index a05996b..47744ae 100644 --- a/testdata/multi-target/Dockerfile +++ b/testdata/multi-target/Dockerfile @@ -5,7 +5,7 @@ ADD Dockerfile /Dockerfile.banana ENV PATH=/darkness ENV BA=nana -FROM scratch +FROM scratch AS final-target LABEL target=final-target USER orange ADD Dockerfile /Dockerfile.orange