Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django SECRET_KEY setting must not be empty with github workflow

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?

like image 645
mmoomocow Avatar asked May 08 '20 00:05

mmoomocow


2 Answers

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.

like image 52
PhysicistSouravDas Avatar answered Oct 12 '22 11:10

PhysicistSouravDas


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.

like image 40
Himanshu Srivastava Avatar answered Oct 12 '22 09:10

Himanshu Srivastava