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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With