Resolving nested meta ARGs against themselves and build ARGs
This commit is contained in:
parent
cb11a9982c
commit
3dcac1b906
|
|
@ -58,6 +58,11 @@ func ParseStages(opts *config.KanikoOptions) ([]instructions.Stage, []instructio
|
||||||
return nil, nil, errors.Wrap(err, "parsing dockerfile")
|
return nil, nil, errors.Wrap(err, "parsing dockerfile")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metaArgs, err = expandNested(metaArgs, opts.BuildArgs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, errors.Wrap(err, "expanding meta ARGs")
|
||||||
|
}
|
||||||
|
|
||||||
return stages, metaArgs, nil
|
return stages, metaArgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,6 +102,25 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) {
|
||||||
return stages, metaArgs, nil
|
return stages, metaArgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// expandNestedArgs tries to resolve nested ARG value against the previously defined ARGs
|
||||||
|
func expandNested(metaArgs []instructions.ArgCommand, buildArgs []string) ([]instructions.ArgCommand, error) {
|
||||||
|
prevArgs := make([]string, 0)
|
||||||
|
for i := range metaArgs {
|
||||||
|
arg := metaArgs[i]
|
||||||
|
v := arg.Value
|
||||||
|
if v != nil {
|
||||||
|
val, err := util.ResolveEnvironmentReplacement(*v, append(prevArgs, buildArgs...), false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
prevArgs = append(prevArgs, arg.Key+"="+val)
|
||||||
|
arg.Value = &val
|
||||||
|
metaArgs[i] = arg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return metaArgs, nil
|
||||||
|
}
|
||||||
|
|
||||||
// stripEnclosingQuotes removes quotes enclosing the value of each instructions.ArgCommand in a slice
|
// stripEnclosingQuotes removes quotes enclosing the value of each instructions.ArgCommand in a slice
|
||||||
// if the quotes are escaped it leaves them
|
// if the quotes are escaped it leaves them
|
||||||
func stripEnclosingQuotes(metaArgs []instructions.ArgCommand) ([]instructions.ArgCommand, error) {
|
func stripEnclosingQuotes(metaArgs []instructions.ArgCommand) ([]instructions.ArgCommand, error) {
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,9 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
|
||||||
dockerfile := `
|
dockerfile := `
|
||||||
ARG IMAGE="ubuntu:16.04"
|
ARG IMAGE="ubuntu:16.04"
|
||||||
ARG FOO=bar
|
ARG FOO=bar
|
||||||
|
ARG HELLO="Hello"
|
||||||
|
ARG WORLD="World"
|
||||||
|
ARG NESTED="$HELLO $WORLD"
|
||||||
FROM ${IMAGE}
|
FROM ${IMAGE}
|
||||||
RUN echo hi > /hi
|
RUN echo hi > /hi
|
||||||
|
|
||||||
|
|
@ -65,11 +68,11 @@ func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
|
||||||
t.Fatal("length of stages expected to be greater than zero, but was zero")
|
t.Fatal("length of stages expected to be greater than zero, but was zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(metaArgs) != 2 {
|
if len(metaArgs) != 5 {
|
||||||
t.Fatalf("length of stage meta args expected to be 2, but was %d", len(metaArgs))
|
t.Fatalf("length of stage meta args expected to be 2, but was %d", len(metaArgs))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, expectedVal := range []string{"ubuntu:16.04", "bar"} {
|
for i, expectedVal := range []string{"ubuntu:16.04", "bar", "Hello", "World", "Hello World"} {
|
||||||
if metaArgs[i].ValueString() != expectedVal {
|
if metaArgs[i].ValueString() != expectedVal {
|
||||||
t.Fatalf("expected metaArg %d val to be %s but was %s", i, expectedVal, metaArgs[i].ValueString())
|
t.Fatalf("expected metaArg %d val to be %s but was %s", i, expectedVal, metaArgs[i].ValueString())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue