How can I insert environment variables, variables ...
# general
b
How can I insert environment variables, variables defined in
pants.toml
or in a
BUILD
files into a string? Is there a f-string style syntax I can use?
Copy code
docker_image(
    name="my_service",
    dependencies=[":my_pex"],
    instructions=[
        f"FROM python:{PYTHON_VERSION_MAJOR}.{PYTHON_VERSION_MINOR}.{PYTHON_VERSION_MICRO}-slim-bullseye",
        "COPY my_pex.pex .",
    ],
)
I currently do this, but I want to pass in the version number from single location into many pants rules.
Copy code
docker_image(
    name="my_service",
    dependencies=[":my_pex"],
    extra_build_args=[
        "PYTHON_VERSION_MAJOR=3",
        "PYTHON_VERSION_MINOR=9",
        "PYTHON_VERSION_MICRO=15",
    ],
    instructions=[
        "ARG PYTHON_VERSION_MAJOR",
        "ARG PYTHON_VERSION_MINOR",
        "ARG PYTHON_VERSION_MICRO",
        "FROM python:{PYTHON_VERSION_MAJOR}.{PYTHON_VERSION_MINOR}.{PYTHON_VERSION_MICRO}-slim-bullseye",
        "COPY my_pex.pex .",
    ],
)
h
Yup build args is the right way to do it if you had a Dockerfile you wanted to dynamically update.
Alternatively, since BUILD files are just python, you can import libraries like os there and likely extract env vars
I haven’t tried it though.
f
Environment variables are available via an
env
function in
BUILD
files. https://www.pantsbuild.org/v2.16/docs/targets#environment-variables
BUILD files do not support importing modules. They are Python the language, not Python the runtime environment.