From c86869627140019f3a39f61662a5a9d9ad3d900e Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Fri, 22 Jan 2021 13:05:36 -0500 Subject: [PATCH] Use build-arg ENV val from local environment if set If a user sets the ENV to be used with the build-arg flag in the local environment by exporting it, look it up and use the value set there for that ENV. Add tests to cover this use case as well. Signed-off-by: Urvashi Mohnani --- cmd/buildah/bud.go | 7 ++++++- tests/bud.bats | 10 ++++++++-- tests/bud/build-arg/Dockerfile | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/buildah/bud.go b/cmd/buildah/bud.go index 6c29c87217f..fb9e194e788 100644 --- a/cmd/buildah/bud.go +++ b/cmd/buildah/bud.go @@ -129,7 +129,12 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error { if len(av) > 1 { args[av[0]] = av[1] } else { - delete(args, av[0]) + // check if the env is set in the local environment and use that value if it is + if val, present := os.LookupEnv(av[0]); present { + args[av[0]] = val + } else { + delete(args, av[0]) + } } } } diff --git a/tests/bud.bats b/tests/bud.bats index 293eacf7afa..70eabc4e454 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -2154,9 +2154,15 @@ EOM } @test "bud with --build-arg" { - _prefetch alpine - run_buildah --log-level "warn" bud --signature-policy ${TESTSDIR}/policy.json -t test ${TESTSDIR}/bud/build-arg + _prefetch alpine busybox + target=busybox-image + run_buildah --log-level "warn" bud --signature-policy ${TESTSDIR}/policy.json -t ${target} ${TESTSDIR}/bud/build-arg expect_output --substring 'missing .+ build argument' + run_buildah bud --signature-policy ${TESTSDIR}/policy.json -t ${target} --build-arg foo=bar ${TESTSDIR}/bud/build-arg + expect_output --substring "bar" + export foo=hello-world + run_buildah bud --signature-policy ${TESTSDIR}/policy.json -t ${target} --build-arg foo ${TESTSDIR}/bud/build-arg + expect_output --substring "hello-world" } @test "bud arg and env var with same name" { diff --git a/tests/bud/build-arg/Dockerfile b/tests/bud/build-arg/Dockerfile index 4aa37238935..fea6ee94f8b 100644 --- a/tests/bud/build-arg/Dockerfile +++ b/tests/bud/build-arg/Dockerfile @@ -1,3 +1,3 @@ FROM busybox - ARG foo +RUN echo $foo