10 months ago
I'm trying to access railway environment variables during the build process using python.
Some of the python requirements use API keys for access that are under the GITHUB_TOKEN variable.
My attempted workaround for not having the API keys in the requirements file, it generates a requirements.txt with python during the Docker build process.
However, when accessing the environment variables in generate_requirements.py, it doesn't output any of the railway service variables.
Is there any way of accessing these variables during the build process?
Thanks in advance.
The docker file looks like this:
FROM python:3.13-slim
ENV DOCKER_ENV=1
RUN mkdir -p /app
WORKDIR /app
# Install system dependencies for Python and Django
RUN apt-get update
RUN apt-get install -y --no-install-recommends git build-essential libpq-dev curl
RUN rm -rf /var/lib/apt/lists/*
# Install Node.js and Sass
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g sass
# Copy project files
COPY . .
# Install pip and other dependencies
RUN python -m ensurepip && python -m pip install --upgrade pip setuptools wheel
RUN pip install python-dotenv
# Generate requirements using API keys
RUN python generate_requirements.py
RUN pip install -r requirements.txt
# Compile SCSS to CSS
RUN sass /app/financemgmt/static/scss/build.scss:/financemgmt/static/css/build.css
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "main.wsgi"]
And the start of the generate_requirements.py file looks like this:
import os
import subprocess
from dotenv import load_dotenv
load_dotenv()
for key, value in os.environ.items():
print(f"{key}={value}")
github_token = os.environ.get("GITHUB_TOKEN")
if not github_token:
raise ValueError("GITHUB_TOKEN not found in .env file")1 Replies
10 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open brody • 10 months ago
9 months ago
Hi there
that sounds to me like the same problem i had deploying a rails app that needed an ENV in order to access a private repo during build time, here is what worked for me
i add the following to my Dockerfile
ARG BUNDLE_GITHUB__COM
ENV BUNDLE_GITHUB__COM=$BUNDLE_GITHUB__COM
# this is command we use to install dependencies in rails. i just added here for demonstration
RUN BUNDLE_GITHUB__COM=$BUNDLE_GITHUB__COM bundle install then from the railway dashboard i went to the service that hosts the code for my app under Variables tab and i added my a new variable for BUNDLE_GITHUB__COM and the value an then hit deploy in the popup.
with any luck you variable is going to be set the next time you push a change to your repo.
let me know how it goes and if you found my answer helpful i would really appreciate it if you could mark it as accepted!
thanks
alex