7

I have a GitHub workflow for Django and when it gets to migrating the database it gives the error

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

the secret key is stored in a .env file and loaded with

from dotenv import load_dotenv
load_dotenv()
from pathlib import Path
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
SECRET_KEY = os.getenv("secret_key")

Here is the file tree

C:.
|   db.sqlite3
|   manage.py
|
\---djangosite
    |   .env
    |   asgi.py
    |   settings.py
    |   urls.py
    |   wsgi.py
    |   __init__.py
    |
    \---__pycache__
        ...

This is the manage.py, it is the regular django one with the load .env code from settings.py

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
from dotenv import load_dotenv
load_dotenv()
from pathlib import Path
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djangosite.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

when I run manage.py on my PC it loads the key and runs the server, but GitHub gives the error above. How do I stop this error from happening?

mmoomocow
  • 1,173
  • 7
  • 27

3 Answers3

11

If you have stored the SECRET_KEY in your system's environment variable, then for GitHub workflow, you can add a dummy environment variable in the YAML file.

The settings.py should look like this

import os
...
SECRET_KEY = os.environ.get('SECRET_KEY') # Or the name by which you stored environment variable
...

The steps are given below:

Step 1: Generate a dummy SECRET_KEY. You can create it yourself by

import secrets
print(secrets.token_hex(25))

Or generate from a site like this.

Step 2: In your .github/workflows YAML file (e.g., django.yml), add this

steps:
...
- name: Run Tests
  env: 
    SECRET_KEY: your-genereated-secret_key
  run: |
    python manage.py test

Then everything will work fine with the same version of code in your local environment, production environment, and GitHub workflow.

2

Adding to @PhysicistSouravDas's answer. You can alternatively do:

- name: Run Tests
    env:
    SECRET_KEY: ${{ secrets.SECRET_KEY }}
    run: |
        python manage.py test

Now, go to the settings of your GitHub repository. Under the secrets menu, click Actions and then click New Repository Secret.

Add a new secret with the name SECRET_KEY and value as the dummy SECRET_KEY generated by the method suggested by @PhysicistSouravDas.

GitHub Actions would pick up the SECRET_KEY from there.

0

When you run python manage.py runserver 8000 you are using manage.py which is settings your DJANGO_SETTINGS_MODULE to settings. It doesn't appear that you have a settings.py in your root directory, so this line in manage.py:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

should become:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Your-App-Name.settings")

as you have in your wsgi.py file.

You can also check here for some other possible solutions.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • I've just added the file tree, the settings.py exists and i can run the server on my PC without errors – mmoomocow May 08 '20 at 01:53
  • Have you tried the solution in the answer and have a look at the solutions here https://stackoverflow.com/a/20646241/2532070 . this can be happening for myriad of reasons so you will have to do some trial and error to fix the issue – AzyCrw4282 May 08 '20 at 04:16
  • In the worse case, you can simply set a value to your secret key in `settings.py` on Github and that should solve it (of-course). – AzyCrw4282 May 08 '20 at 04:17
  • 1
    I have done what you said and set a value and in settings.py (KEY) then I changed the readme (its a public project) to say to replace the key in settings.py – mmoomocow May 08 '20 at 09:41