GitHub action couldn't find environment variable for Django

Question:

I was trying to use the environment variable in my Django application where I use the django-environ package with the .env file in my local machine. But I can’t use the .env file in my GitHub action. I’ve configured action secret variables manually from my project settings.
Here is my local machine code:

import environ

env = environ.Env()
environ.Env.read_env()

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env('POSTGRES_DB_NAME'),
        'USER': env('POSTGRES_USER'),
        'PASSWORD': env('POSTGRES_PASSWORD'),
        'HOST': env('POSTGRES_HOST'),
        'PORT': env('POSTGRES_PORT'),
    }
}

This code is working in my local environment but failed to load in GitHub actions. I have configured the same variables in GitHub actions too, but still, the application couldn’t find the environment variable there in the GitHub action.

django.yml

name: Django CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.7, 3.8, 3.9]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run Tests
      run: |
        python manage.py test

The GitHub Action displays the following errors:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/environ/environ.py", line 403, in get_value
    value = self.ENVIRON[var_name]
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'POSTGRES_DB_NAME'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/core/management/base.py", line 346, in run_from_argv
    parser = self.create_parser(argv[0], argv[1])
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/core/management/base.py", line 320, in create_parser
    self.add_arguments(parser)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/core/management/commands/test.py", line 44, in add_arguments
    test_runner_class = get_runner(settings, self.test_runner)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/test/utils.py", line 317, in get_runner
    test_runner_class = test_runner_class or settings.TEST_RUNNER
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/runner/work/lha-backend/lha-backend/root/settings.py", line 87, in <module>
    'NAME': env('POSTGRES_DB_NAME'),
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/environ/environ.py", line 201, in __call__
    parse_default=parse_default
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/environ/environ.py", line 407, in get_value
    raise ImproperlyConfigured(error_msg) from exc
django.core.exceptions.ImproperlyConfigured: Set the POSTGRES_DB_NAME environment variable
Asked By: Maruf Khan

||

Answers:

You need to configure env for your run step, something like this:

    - name: Run Tests
      run: |
        python manage.py test
      env:
        POSTGRES_DB_NAME: ${{ secrets.POSTGRES_DB_NAME }}
        POSTGRES_USER: ${{ secrets.POSTGRES_USER }}
        POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
        POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }}
        POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }}

Relevant docs: https://docs.github.com/en/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow

Additional advice

  • Use the same name for both GitHub secret and Environment Variable
  • Create an organization on GitHub if you want to use the same secrets across multiple repositories
Answered By: Mandera