Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolving arguments over multi-stage build #1928

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions integration/dockerfiles/Dockerfile_test_multistage_args_issue_1911
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ARG OTHER="fix"
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" >> B.txt

FROM base-dev as base-custom
ARG NAME
RUN echo "custom:: $NAME" >> C.txt
RUN echo "custom:: $OTHER" >> C.txt

FROM base-custom as base-custom2
ARG OTHER
RUN echo "custom:: $NAME" >> D.txt
RUN echo "custom:: $OTHER" >> D.txt
5 changes: 3 additions & 2 deletions pkg/commands/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func ParseArg(key string, val *string, env []string, ba *dockerfile.BuildArgs) (
}
resolvedValue = &value
} else {
meta := ba.GetAllMeta()
if value, ok := meta[resolvedKey]; ok {
if value, ok := ba.GetAllAllowed()[resolvedKey]; ok {
resolvedValue = &value
} else if value, ok := ba.GetAllMeta()[resolvedKey]; ok {
resolvedValue = &value
}
}
Expand Down
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.Clone()
} 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