Hi all, I’m running into issues trying to pass an ...
# general
d
Hi all, I’m running into issues trying to pass an environment variable as an argument to my pex binary entrypoint. I’ve got the following pex binary
Copy code
pex_binary(
    name="app",
    entry_point="gunicorn",
    args=["-b", ":$PORT", "<http://services.my_service.app.app:create_app()|services.my_service.app.app:create_app()>"],
    dependencies=["services/my_service"],
    layout="loose",
    environment="linux",
)
I think my pex binary is passing through the string
'$PORT'
instead of evaluating the environment variable at runtime because I get
Copy code
Error: '$PORT' is not a valid port number.
I’m deploying to google app engine and it recommends configuring gunicorn to listen on the
$PORT
envvar, which is automatically set by the app engine runtime. https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables Is there any way to achieve what I want without having to wrap this in a helper script?
1
e
Nope. A helper script is the way to go. Pex takes injected env vars and injected args as given. It does not evaluate them in a shell or otherwise parse them and handle
$
specially.
b
Actually recent pants version do allow you to read env vars in BUILD files. I'm not sure how it works completely (what if the car doesn't exist? Etc...) but might get you over the line, so to speak.
e
I don't think so, the docs he points to are about a cloud runtime env var. You don't know PORT at runtime back on your machine at build time.
1
b
Ah yes. Missed that part.
d
thanks both, will figure out how to wrap this in a helper
despite what the documentation says, I think the whole think passing PORT Is redundant as gunicorn will respect this value if present https://docs.gunicorn.org/en/latest/settings.html#server-socket
Copy code
➜  app@environment=macos.pex git:(main) ✗ PORT=8081 ./__main__.py
[2023-08-04 14:41:42 +0100] [5406] [INFO] Starting gunicorn 20.1.0
[2023-08-04 14:41:42 +0100] [5406] [INFO] Listening at: <http://0.0.0.0:8081> (5406)
So none of this is necessary. But appreciate the help regardless as I’ve learnt something 👍