3 months ago
I set a MASTER_KEY variable on my service on SERVICE VARIABLE but got 'None' any time Im deploying.
What is my problem to have this variable set before to execute my python service?
Thanks
5 Replies
3 months ago
Hey there! We've found the following might help you get unblocked faster:
🧵 Set environment variable in config as code with
railway.toml
If you find the answer from one of these, please let us know by solving the thread!
3 months ago
Ok so I solved the problem putting my variable reading into a function, in case this help future use case:
Before I have:
fernet = Fernet(os.getenv("MASTER_KEY").encode())
This line detect MASTER_KEY as 'None' as it was executed on runtime as it's a variable
After:
def get_fernet() -> Fernet:
global _fernet
if _fernet is None:
master_key = os.getenv("MASTER_KEY")
if not master_key:
raise RuntimeError(
"MASTER_KEY is not set. Define it as an environment variable."
)
fernet = Fernet(masterkey.encode())
return _fernet
This is ok as it's a method and call after runtime when Variable as setup
Status changed to Solved brody • 3 months ago
blackyugin
Ok so I solved the problem putting my variable reading into a function, in case this help future use case:Before I have:fernet = Fernet(os.getenv("MASTER_KEY").encode())This line detect MASTER_KEY as 'None' as it was executed on runtime as it's a variableAfter:def get_fernet() -> Fernet:global _fernetif _fernet is None:master_key = os.getenv("MASTER_KEY")if not master_key:raise RuntimeError("MASTER_KEY is not set. Define it as an environment variable.")fernet = Fernet(masterkey.encode())return _fernetThis is ok as it's a method and call after runtime when Variable as setup
3 months ago
Ok its working at this tep but when the get fernet is call later (on a API call for exemple) its still failing... I need to explore more solutions, if you got more ideas... please
Status changed to Awaiting Railway Response Railway • 3 months ago
3 months ago
Hey! I’m glad you’ve made some progress. If the variable is still not loading correctly during the API call, you might want to try adding some detailed logging to see exactly where it fails. Also, double-check the configuration and make sure that the environment variable is properly set before the function is called. If you need more help, feel free to share any logs or additional details, and we’ll figure it out together. Keep going, and I’m here to help!
3 months ago
Seem that restarting the service manually reset all variables / build and so on and now its working (by magic xD)...
So solution on using a method seem to work but we have to take care when railway automatically build after a git push as it seem that this redeploy didn't take in account the rebuild itself.
Status changed to Solved blackyugin • 3 months ago