GitHub Actions with encrypted SECRET_KEY as environmental variable do not work

Viewed 253

I don't have much experience in coding. I am currently writing a small Django application. Following all the rules, I do not want to place the SECRET_KEY in the repository, so I created the SECRET_KEY environment variable in the .bashrс file. And in the settings.py file I wrote: SECRET_KEY = os.environ.get ('SECRET_KEY') When I execute pylint --load-plugins pylint_django my_application in the terminal on my computer everything works. I get a response

Your code has been rated at 9.88 / 10

Also I have workflow pylint.yaml to accomplish pylint-django tests on push trigger, when I send my files to my GitHub repo:

name: Pylint

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Analysing the code with pylint 
      run: |
        pylint --load-plugins pylint_django rm

When I am pushing my files to GitHub, I reseive this:

Run pylint --load-plugins pylint_django rm
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint_django/checkers/foreign_key_strings.py", line 90, in open
    django.setup()
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.12/x64/bin/pylint", line 8, in <module>
    sys.exit(run_pylint())
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint/__init__.py", line 24, in run_pylint
    PylintRun(sys.argv[1:])
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint/lint/run.py", line 374, in __init__
    linter.check(args)
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 970, in check
    self._check_files(
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1005, in _check_files
    with self._astroid_module_checker() as check_astroid_module:
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1127, in _astroid_module_checker
    checker.open()
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/pylint_django/checkers/foreign_key_strings.py", line 114, in open
    django.setup()
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/contrib/auth/admin.py", line 6, in <module>
    from django.contrib.auth.forms import (
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/contrib/auth/forms.py", line 11, in <module>
    from django.contrib.auth.tokens import default_token_generator
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/contrib/auth/tokens.py", line 117, in <module>
    default_token_generator = PasswordResetTokenGenerator()
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/contrib/auth/tokens.py", line 18, in __init__
    self.secret = self.secret or settings.SECRET_KEY
  File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/django/conf/__init__.py", line 90, in __getattr__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
Error: Process completed with exit code 1.

It`s look like GitHub-hosted runner dont see the value of my SECRET_KEY, encripted in .bashrc file
on my local machine. I tried to change my file Like this:

name: Pylint

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Analysing the code with pylint 
      run: |
        export SECRET_KEY=${{ secrets.SECRET_KEY }}
        export DB_USER=${{ secrets.DB_USER }}
        export DB_PASSWORD=${{ secrets.DB_PASSWORD }}
        export DB_NAME=${{ secrets.DB_NAME }}
        pylint --load-plugins pylint_django rm

Then i got this error:

Run export  SECRET_KEY=***
/home/runner/work/_temp/87974674-0535-4fca-ab32-30cf1a19f030.sh: line 1: syntax error near unexpected token `4h%fzr+bqqsmg8ftp-a$4%b'
Error: Process completed with exit code 2.

I changed my pylint.yaml file to this one:

name: Pylint

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.8
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Analysing the code with pylint # SECRET_KEY is generated with DJANGO SECRET KEY GENERATOR
      run: |
        export SECRET_KEY=0-42!#@*6jqo)lv^+@-+&(-@kjke@xzk%)1!w)vz-1uwk71p)=
        export DB_USER=${{ secrets.DB_USER }}
        export DB_PASSWORD=${{ secrets.DB_PASSWORD }}
        export DB_NAME=${{ secrets.DB_NAME }}
        pylint --load-plugins pylint_django rm

and got this error:

Run export SECRET_KEY=0-42!#@*6jqo)lv^+@-+&(-@kjke@xzk%)1!w)vz-1uwk71p)=
/home/runner/work/_temp/fdbfd3cd-9022-4cd6-833c-0aef801ddddb.sh: line 1: syntax error near unexpected token `)'
Error: Process completed with exit code 2.

How can I tell my pylint workflow what SECRET_KEY to use in pylint.yaml file to pass tests?

Please, help.I've already spent a lot of time thinking and searching on the Internet and GitHub documentation, but I haven't found anything for myself.

1 Answers

I was very close to the solution. Due to the fact that the SECRET_KEY contains parentheses, you need to enclose the SECRET_KEY in double quotes.

 - name: Analysing the code with pylint # SECRET_KEY is generated with DJANGO SECRET KEY GENERATOR
      run: |
        export SECRET_KEY="0-42!#@*6jqo)lv^+@-+&(-@kjke@xzk%)1!w)vz-1uwk71p)="
        export DB_USER=${{ secrets.DB_USER }}
        export DB_PASSWORD=${{ secrets.DB_PASSWORD }}
        export DB_NAME=${{ secrets.DB_NAME }}
        pylint --load-plugins pylint_django rm
Related