https://pantsbuild.org/ logo
#general
Title
# general
r

refined-addition-53644

08/31/2022, 2:28 PM
Hi, is it possible to set env vars passed to docker using
build_arg
to have some default value. The use case is we set such env vars only in our CI/CD but for local run it can be any random value. https://www.pantsbuild.org/docs/tagging-docker-images#using-env-vars-to-include-dynamic-data-in-tags
Or do I need to write my own plugin?
c

curved-television-6568

08/31/2022, 2:49 PM
It may be a bit tricky to get optional defaults, but if you provide a default value in your Dockerfile for the build arg, that should get picked up, if I remember correctly.
If that doesn’t work out, or is too clunky (like you need it in X files) then open a ticket. I’ve been down this path before, but before I had a real use case for it…
r

refined-addition-53644

08/31/2022, 2:51 PM
This is mostly about tagging the docker image using
build_args
in pants. Pants will complain currently
Copy code
16:50:37.01 [ERROR] 1 Exception encountered:

  DockerBuildContextError: Undefined value for build arg on the src/pyfleet-charge-plan:charge_plan_image target: The Docker environment variable 'ECR_REPO_URI' is undefined. You may provide a value for this variable either in `[docker].env_vars` or in Pants's own environment.

If you did not intend to inherit the value for this build arg from the environment, provide a default value with the option `[docker].build_args` or in the `extra_build_args` field on the target definition. Alternatively, you may also provide a default value on the `ARG` instruction directly in the `Dockerfile`.
c

curved-television-6568

08/31/2022, 2:52 PM
Oh look at that. It even mentioned the default in Dockerfile I was referring you to now.. 😆
❤️ 1
So provide the build arg, without value in your BUILD file or config, and set your default value on the ARG in Dockerfile, then you can override it from env 😉
(I sincerely hope 😛 )
r

refined-addition-53644

08/31/2022, 2:54 PM
My confusion is around tagging related args. The kind of build args which you are talking about is set inside the dockerfile. Will they also be available outside when tagging?
c

curved-television-6568

08/31/2022, 2:54 PM
yes
as long as you list them in
build_args
or
extra_build_args
(if without value, it will default to the default given in the Dockerfile, if any)
👍 1
r

refined-addition-53644

08/31/2022, 2:58 PM
This doesn’t work if I don’t export GIT_COMMIT and ECR_REPO_URI • Dockerfile
Copy code
FROM public.ecr.aws/docker/library/python:3.9.13-slim-bullseye

ARG GIT_COMMIT '123'
ARG ECR_REPO_URI 'test.repo'
• pants.toml
Copy code
[docker]
build_args = ["GIT_COMMIT", "ECR_REPO_URI"]
n

nutritious-hair-72580

11/24/2022, 12:44 PM
@curved-television-6568 Just checking there's no way to set a default for GIT_COMMIT when running locally? I guess can just tell other to set it in their env.
c

curved-television-6568

11/24/2022, 12:48 PM
In recent versions of the
pants
script there’s an option to provide a
.pants.bootstrap
script that can setup local env vars for you before invoking pants (name is customizable): https://github.com/pantsbuild/setup/blob/ab8581bb52d92fcb3fda1b1dfb20204ae8888a90/pants#L19-L23 so you can have:
Copy code
# .pants.bootstrap
: ${GIT_COMMIT:=$(git rev-parse HEAD)}
to set GIT_COMMIT if not already set.
🙌 1
n

nutritious-hair-72580

11/24/2022, 12:48 PM
Yes, that would work, thanks!
c

curved-television-6568

11/24/2022, 12:48 PM
ok, cool 🙂
This has come up before, and I feel it’s such a fundamentally useful feature so persisted it in the Q/A section now: https://github.com/pantsbuild/pants/discussions/17633 🙂
2 Views