0

I'm trying to run the tests of my Django app in Gitlab CI/CD. The process fails every time with an error: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty..

I assume this is because I have not my .env file available in the repository and that file contains for example the SECRET_KEY -variable which is then used in the settings.py like this: SECRET_KEY = os.getenv('SECRET_KEY').

So what would be the proper way to set these variables so my tests would pass?

lr_optim
  • 276
  • 7
  • 19

1 Answers1

1

One good way is to add the secret in CI variable. Then export it in target repo.

http://your/gilab/url/project/-/settings/ci_cd > Expand variables

Add a variable named SECRET_KEY, with the value, select type variable. Then in .gitlab-ci.yml, add following in concerned job.

 before_script:
    # to export variable to target environment
    - export SECRET_KEY=$SECRET_KEY  
    # to check if OK
    - env 

When set up the variable, you might select on which environment (dev, staging, prod) if you have many env.

  • What if I have a .env file that contains more than 50 variables. Is there a way to export them at once without having to write them one by one like you do above? – Mai Elshiashi Mar 09 '22 at 11:12
  • create a ci with FILE type (ex : CI_DEPLOY_CONFIG_FILE) then use this : before_script: - export $(grep -v '^#' $CI_DEPLOY_CONFIG_FILE | xargs) – Romain TAILLANDIER Mar 09 '22 at 13:42
  • like in this post : https://stackoverflow.com/questions/66278742/gitlab-ci-cd-is-it-possible-to-store-multiple-ci-cd-variables-in-one-file – Romain TAILLANDIER Mar 09 '22 at 13:44