Skip to content

Commit f15c476

Browse files
committed
executor: expand args in build logs
Expand user,builtin and heading args before logging build steps. Closes: #6327 Signed-off-by: flouthoc <[email protected]>
1 parent 03da7d8 commit f15c476

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

imagebuildah/executor.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,19 @@ func (b *Executor) buildStage(ctx context.Context, cleanupStages map[int]*StageE
600600
prefix += ": "
601601
}
602602
suffix := "\n"
603-
fmt.Fprintf(stageExecutor.executor.out, prefix+format+suffix, args...)
603+
builtinArgs := argsMapToSlice(stage.Builder.BuiltinArgDefaults)
604+
headingArgs := argsMapToSlice(stage.Builder.HeadingArgs)
605+
userArgs := argsMapToSlice(stage.Builder.Args)
606+
userArgs = append(builtinArgs, append(userArgs, headingArgs...)...)
607+
argsCopy := args
608+
for i, _ := range args {
609+
argsCopy[i], err = imagebuilder.ProcessWord(args[i].(string), userArgs)
610+
if err != nil {
611+
argsCopy[i] = args[i]
612+
fmt.Printf("while replacing arg variables with values for format in logging %q: %w\n", args[i], err)
613+
}
614+
}
615+
fmt.Fprintf(stageExecutor.executor.out, prefix+format+suffix, argsCopy...)
604616
}
605617
}
606618
b.stagesLock.Unlock()

tests/bud.bats

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8844,3 +8844,42 @@ _EOF
88448844
run_buildah build --layers ${contextdir}
88458845
run_buildah build ${contextdir}
88468846
}
8847+
8848+
@test "bud build argument expansion in log" {
8849+
_prefetch alpine
8850+
target=build-arg-expand-log
8851+
8852+
# Test basic build argument expansion
8853+
run_buildah build $WITH_POLICY_JSON -t ${target} --build-arg foo=foovalue -f $BUDFILES/build-arg/Dockerfile $BUDFILES/build-arg
8854+
expect_output --substring "RUN echo foovalue"
8855+
8856+
# Test with a more complex build argument
8857+
run_buildah build $WITH_POLICY_JSON -t ${target}-complex --build-arg foo="/usr/local/bin" -f $BUDFILES/build-arg/Dockerfile $BUDFILES/build-arg
8858+
expect_output --substring "RUN echo /usr/local/bin"
8859+
8860+
# Test with multiple build arguments
8861+
contextdir=${TEST_SCRATCH_DIR}/build-arg-expansion
8862+
mkdir -p $contextdir
8863+
8864+
cat > $contextdir/Containerfile << _EOF
8865+
FROM alpine
8866+
ARG NEWROOT
8867+
ARG PREFIX
8868+
RUN mkdir \$NEWROOT
8869+
RUN echo \$PREFIX/bin
8870+
_EOF
8871+
8872+
run_buildah build $WITH_POLICY_JSON -t ${target}-multi --build-arg NEWROOT=/new-root-fs --build-arg PREFIX=/usr $contextdir
8873+
expect_output --substring "RUN mkdir /new-root-fs"
8874+
expect_output --substring "RUN echo /usr/bin"
8875+
8876+
# Test that unexpanded variables show as empty when no build-arg is provided
8877+
cat > $contextdir/Containerfile2 << _EOF
8878+
FROM alpine
8879+
ARG UNDEFINEDVAR
8880+
RUN echo \$UNDEFINEDVAR
8881+
_EOF
8882+
8883+
run_buildah build $WITH_POLICY_JSON -t ${target}-undefined -f $contextdir/Containerfile2 $contextdir
8884+
expect_output --substring "RUN echo "
8885+
}

0 commit comments

Comments
 (0)