Fix resolving arguments over multi-stage build (#1928)

* Fix resolving argument over mulit-stage build

- Building multi-stage Dockerfiles did not correctly resolve
  ARG commands over different stages. Now, each stage depends on the
  build arguments set by the stage before.

Closes: #1911

* Fix resolving empty arg commands
This commit is contained in:
Gabriel Nützi
2022-02-10 09:22:21 -08:00
committed by GitHub
parent 3589382378
commit 2d94d56af7
3 changed files with 33 additions and 5 deletions
+12 -3
View File
@@ -84,7 +84,7 @@ type stageBuilder struct {
}
// newStageBuilder returns a new type stageBuilder which contains all the information required to build the stage
func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage, crossStageDeps map[int][]string, dcm map[string]string, sid map[string]string, stageNameToIdx map[string]string, fileContext util.FileContext) (*stageBuilder, error) {
func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, stage config.KanikoStage, crossStageDeps map[int][]string, dcm map[string]string, sid map[string]string, stageNameToIdx map[string]string, fileContext util.FileContext) (*stageBuilder, error) {
sourceImage, err := image_util.RetrieveSourceImage(stage, opts)
if err != nil {
return nil, err
@@ -143,7 +143,11 @@ func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage, cross
s.cmds = append(s.cmds, command)
}
s.args = dockerfile.NewBuildArgs(s.opts.BuildArgs)
if args != nil {
s.args = args.Clone()
} else {
s.args = dockerfile.NewBuildArgs(s.opts.BuildArgs)
}
s.args.AddMetaArgs(s.stage.MetaArgs)
return s, nil
}
@@ -615,8 +619,13 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
}
logrus.Infof("Built cross stage deps: %v", crossStageDependencies)
var args *dockerfile.BuildArgs
for index, stage := range kanikoStages {
sb, err := newStageBuilder(opts, stage, crossStageDependencies, digestToCacheKey, stageIdxToDigest, stageNameToIdx, fileContext)
sb, err := newStageBuilder(args, opts, stage, crossStageDependencies, digestToCacheKey, stageIdxToDigest, stageNameToIdx, fileContext)
args = sb.args
if err != nil {
return nil, err
}