Skip to content

Can I pass environment variable value from Git Action's deployment.yaml to docker file ?

0

Hi team, need help- I am trying to pass environemnt variable value defined in github action's (in deployment.yaml ) to DOCKERFILE, so I can take that value and pass it in my python script in the container. I have tried some google feed but that is not working. Can you please help on this ?

asked 2 years ago130 views

1 Answer
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}")

AWS

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.