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
  • 299
  • 1
  • 10
  • 30

2 Answers2

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.

Romain TAILLANDIER
  • 1,765
  • 10
  • 18
  • 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
  • 1
    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
1

Create ci-cd variables select file as type. Give your file name as key and put all your variables in values input. Like this

gitlab ci variables

Then in your job, you can do something like this :

before_script: - cat $env > path/where/save/envfile/inyourprojet

  • Works for me. With the `export SECRET_KEY=$SECRET_KEY` in other answer I wasn't successfull. – mirek Jun 30 '23 at 15:30