Почему моя переменная окружения равна None в действиях на github?

Я пытаюсь создать CI с действиями GitHub для моего приложения django. Я определил переменные окружения на github в settings -> secrets -> actions.

Вот мой файл ci.yaml :


# 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

Когда дело доходит до действия Run Migration, у меня возникает эта ошибка:

 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'

Но я определил DJANGO_ALLOWED_HOSTS. Оно равно localhost. Я также пытался определить его как "localhost", но это тоже не работает.

Вы уже близко - все, что вам нужно, это передать секреты в переменные env на шаге сборки, например, так:

- 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 }}

Секреты по умолчанию не являются частью переменных окружения, поэтому для каждой из них нужно указать отдельную строку, если у вас их больше.

Вам необходимо установить переменную окружения на уровне сценария step (это также можно сделать на уровне job или workflow).

Конкретно для этой переменной env DJANGO_ALLOWED_HOSTS она может выглядеть следующим образом:

      - 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
Вернуться на верх