Home > Back-end >  Why does my environment variable are equal to None on github actions?
Why does my environment variable are equal to None on github actions?

Time:02-23

I am trying to build a CI with GitHub actions for my django app. I defined my environment variables on github in settings -> secrets -> actions.

This is my ci.yaml file :


# name of our workflow
name: Django CI/CD Workflow

# triggers for our workflow
on: push


jobs:
  health-check-job: # health check job for testing and code formatting check
    runs-on: ubuntu-latest # os for running the job
    services:
      postgres: # we need a postgres docker image to be booted a side car service to run the tests that needs a db
        image: postgres
        env: # the environment variable must match with app/settings.py if block of DATBASES variable otherwise test will fail due to connectivity issue.
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: github-actions
        ports:
          - 5432:5432 # exposing 5432 port for application to use
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - name: Checkout code # checking our the code at current commit that triggers the workflow
        uses: actions/checkout@v2
      - name: Cache dependency # caching dependency will make our build faster.
        uses: actions/cache@v2 # for more info checkout pip section documentation at https://github.com/actions/cache
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
      - name: Setup python environment # setting python environment to 3.x
        uses: actions/setup-python@v2
        with:
          python-version: '3.x' # if you want multiple python version run just use matrix strategy in job config. See the documentation of GitHub Actions
      - name: Check Python version # checking the python version to see if 3.x is installed.
        run: python --version
      - name: Install requirements # install application requirements
        run: pip install -r pur_beurre/requirements.txt
      - name: Run Migrations # run migrations to create table in side car db container
        run: python pur_beurre/manage.py migrate
      - name: Run Test # running tests
        run: python pur_beurre/manage.py test

When it come ro Run Migration action, I have this error:

 File "/home/runner/work/P10/P10/pur_beurre/pur_beurre/settings.py", line 29, in <module>
    ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
AttributeError: 'NoneType' object has no attribute 'split'

But I did defined DJANGO_ALLOWED_HOSTS. It is equal to localhost. I also tried to define it as "localhost", but it doesnt work either.

CodePudding user response:

You are close - all you need is to pass secrets to env variables to your build step, like this:

- name: Run Migrations # run migrations to create table in side car db container
  run: python pur_beurre/manage.py migrate
  env:
     DJANGO_ALLOWED_HOSTS: ${{ secrets.DJANGO_ALLOWED_HOSTS }}

Secrets are not by default part of environment variables, so for each of them, you have to specify a separate line, if you have more of those.

CodePudding user response:

You need to set the environment variable at the script step level (it can also be done at the job or workflow level).

For this DJANGO_ALLOWED_HOSTS env variable specifically, it could look like this:

      - name: Run Migrations # run migrations to create table in side car db container
        run: python pur_beurre/manage.py migrate
        env: 
          DJANGO_ALLOWED_HOSTS: your_host_value # you can also use a secret variable
  • Related