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

proud-appointment-36730

06/22/2022, 7:11 PM
Is there a way to use environment variables from within your
BUILD
files? For context, looking to configure the
repositories
field of a
python_distribution
Or is this sort of an anti-pattern because it breaks hermeticity?
More context: Trying to avoid using a
.pypirc
file as a centralized place for storing repo config, but also trying to avoid hardcoding the
repository
field in a bunch of
BUILD
files
p

proud-dentist-22844

06/22/2022, 8:28 PM
https://www.pantsbuild.org/docs/python-publish-goal#environment-variables Yup. I think environment variables are a recommended way to manage twine credentials. As far as adding the
repositories
field, I already had a macro that wraps
python_distribution
so I added it there - one place for the whole repo. https://github.com/st2sandbox/st2/blob/pants/pants-plugins/macros.py#L100-L104
That said, I haven't hardly used
publish
, but it looks like it would be awkward to switch from prod to test pypi indexes. Using env vars or a cli arg or something could be nice for that as well. Once
__defaults__
is merged, then that would provide an easy way to set
repository
without relying on a macro. https://github.com/pantsbuild/pants/pull/15836
p

proud-appointment-36730

06/22/2022, 9:12 PM
Ended up just using a
.pypirc
file to get unstuck but this macros thing is an interesting feature that I haven't touched yet
c

curved-television-6568

06/22/2022, 9:27 PM
Do you need to publish to different repositories from time to time? Otherwise using a repo alias in the BUILD file, like
Copy code
python_distribution(..., repositories=["@some_repo"])
Would use this section from your
.pypirc
Copy code
[some_repo]
repository = https://...
username = __token__
password = pypi-...
as example..
p

proud-appointment-36730

06/22/2022, 10:07 PM
Yeah that's what I ended up doing. I'm just trying to make my repo as portable as possible, so wanted to avoid writing out a file somewhere during build environment setup. But, the file works fine
c

curved-television-6568

06/22/2022, 10:12 PM
You can hardcode the repo url in the build file and provide the credentials using env vars, so no additional file required in that case.
p

proud-appointment-36730

06/22/2022, 10:16 PM
Yeah but then the repo URL is hardcoded all over the place in a bunch of
BUILD
files, which I was trying to avoid. I was hoping to pull the
repository
argument of a
python_distribution
target in from an environment variable, but using a file that I write out at setup time is turning out to work just fine
👍 1
c

curved-television-6568

06/23/2022, 8:05 AM
Ah, right. But that should work too… using the
TWINE_REPOSITORY_URL_<repository>
variable.
Not saying you need to change anything, just trying to find out if what you went for initially actually works or not 😉