Skip to content

Commit

Permalink
Fix resolving argument over mulit-stage build
Browse files Browse the repository at this point in the history
  • Loading branch information
Marthyas committed Feb 7, 2022
1 parent 268ee26 commit 23c348a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 11 additions & 0 deletions integration/dockerfiles/Dockerfile_test_multistage_args_issue_1911
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM alpine:latest as base
ARG NAME="base"
RUN echo "base:: $NAME" >> A.txt

FROM base AS base-dev
ARG NAME="$NAME-dev"
RUN echo "dev:: $NAME" >> A.txt

FROM base-dev as base-custom
ARG NAME
RUN echo "custom::" $NAME >> A.txt
15 changes: 12 additions & 3 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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
Expand Down Expand Up @@ -142,7 +142,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
} else {
s.args = dockerfile.NewBuildArgs(s.opts.BuildArgs)
}
s.args.AddMetaArgs(s.stage.MetaArgs)
return s, nil
}
Expand Down Expand Up @@ -606,8 +610,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
}
Expand Down

0 comments on commit 23c348a

Please sign in to comment.