1 Answer
- Newest
- Most votes
- Most comments
0
Here's how to pass environment variables from GitHub Actions to a Dockerfile and then to your Python script:
** GitHub Actions Workflow (deployment.yaml):**
name: Deploy
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
env:
MY_VARIABLE: "some-value" # Define at job level
steps:
- uses: actions/checkout@v2
- name: Build Docker image
run: |
docker build \
--build-arg MY_VAR="${{ env.MY_VARIABLE }}" \
-t my-image .
** Dockerfile:**
FROM python:3.9
# Accept build argument
ARG MY_VAR
# Set it as environment variable
ENV MY_ENV_VAR=$MY_VAR
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "script.py"]
** Python script (script.py):**
import os
# Access the environment variable
my_var = os.environ.get('MY_ENV_VAR')
print(f"Value from GitHub Actions: {my_var}")
answered 2 years ago
