executor: honor default ARG value while eval base name

While PR https://github.com/containers/buildah/pull/3947 added support
for evaluating `--build-args` in base image names for a builds but it
missed processing default value if any. So for scenarios where `ARG`
already has a default value in Containerfile via `ARG key=value` but
was not specified with `--build-arg key=value` the processing ignored
the default value. Following commit just adds support for that.

Makes below Containerfile functional without any external `--build-arg`
value from CLI

```Dockerfile
ARG my_env=a

FROM alpine as stage_a
RUN /bin/true

FROM stage_${my_env} as stage_b
RUN /bin/true
```

Closes: https://github.com/containers/buildah/issues/4312

Signed-off-by: Aditya R <arajan@redhat.com>
This commit is contained in:
Aditya R 2022-11-01 11:11:28 +05:30
parent c2cf9fa47a
commit a237085fe0
No known key found for this signature in database
GPG Key ID: 8E5A8A19DF7C8673
3 changed files with 16 additions and 0 deletions

View File

@ -690,7 +690,12 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image
base = child.Next.Value
}
}
headingArgs := argsMapToSlice(stage.Builder.HeadingArgs)
userArgs := argsMapToSlice(stage.Builder.Args)
// append heading args so if --build-arg key=value is not
// specified but default value is set in Containerfile
// via `ARG key=value` so default value can be used.
userArgs = append(headingArgs, userArgs...)
baseWithArg, err := imagebuilder.ProcessWord(base, userArgs)
if err != nil {
return "", nil, fmt.Errorf("while replacing arg variables with values for format %q: %w", base, err)

View File

@ -2810,6 +2810,10 @@ _EOF
_prefetch busybox:musl
target=leading-args
run_buildah build $WITH_POLICY_JSON -t ${target} --build-arg=VERSION=musl -f $BUDFILES/leading-args/Dockerfile $BUDFILES/leading-args
#Verify https://github.com/containers/buildah/issues/4312
# stage `FROM stage_${my_env}` must be resolved with default arg value and build should be successful.
run_buildah build $WITH_POLICY_JSON -t source -f $BUDFILES/multi-stage-builds/Dockerfile.arg_in_stage
}
@test "bud-with-healthcheck" {

View File

@ -0,0 +1,7 @@
ARG my_env=a
FROM alpine as stage_a
RUN /bin/true
FROM stage_${my_env} as stage_b
RUN /bin/true