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:
		
							parent
							
								
									c2cf9fa47a
								
							
						
					
					
						commit
						a237085fe0
					
				|  | @ -690,7 +690,12 @@ func (b *Executor) Build(ctx context.Context, stages imagebuilder.Stages) (image | ||||||
| 									base = child.Next.Value | 									base = child.Next.Value | ||||||
| 								} | 								} | ||||||
| 							} | 							} | ||||||
|  | 							headingArgs := argsMapToSlice(stage.Builder.HeadingArgs) | ||||||
| 							userArgs := argsMapToSlice(stage.Builder.Args) | 							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) | 							baseWithArg, err := imagebuilder.ProcessWord(base, userArgs) | ||||||
| 							if err != nil { | 							if err != nil { | ||||||
| 								return "", nil, fmt.Errorf("while replacing arg variables with values for format %q: %w", base, err) | 								return "", nil, fmt.Errorf("while replacing arg variables with values for format %q: %w", base, err) | ||||||
|  |  | ||||||
|  | @ -2810,6 +2810,10 @@ _EOF | ||||||
|   _prefetch busybox:musl |   _prefetch busybox:musl | ||||||
|   target=leading-args |   target=leading-args | ||||||
|   run_buildah build $WITH_POLICY_JSON -t ${target} --build-arg=VERSION=musl -f $BUDFILES/leading-args/Dockerfile $BUDFILES/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" { | @test "bud-with-healthcheck" { | ||||||
|  |  | ||||||
|  | @ -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 | ||||||
		Loading…
	
		Reference in New Issue