2

I have a very simple django project with a few unit tests on it and I would like to set up Gitlab to run those tests evry time I make a new commit. I use sqlite3 for my database (I plan to change later, but for now I want to keep it simple) and I stored some of my settings variable in a .env file.

Here is my .gitlab-ci.yml file (I used the default file proposed by gitlab and removed what I thought I didn't need) :

image: python:latest

variables:
    SECRET_KEY: "this-is-my-secret-key"
    DEBUG: "True"
    ALLOWED_HOSTS: "['*']"
    DB_ENGINE: "django.db.backends.sqlite3"
    DB_NAME: "test_database.sqlite3"
    STATIC_URL: "/static/"

cache:
  paths:
    - ~/.cache/pip/

before_script:
  - python -V 
  - pip install -r requirements.txt

test:
  script:
    - cd backend
    - python3 manage.py makemigrations
    - python3 manage.py migrate
    - python3 manage.py test

but when I commit, I get this error

$ python3 manage.py makemigrations
/usr/local/lib/python3.9/site-packages/environ/environ.py:628: UserWarning: /builds/romainros/ynoverflow/backend/ynoverflow/.env doesn't exist - if you're not configuring your environment separately, create one.
  warnings.warn(
Traceback (most recent call last):
  File "/builds/romainros/ynoverflow/backend/manage.py", line 22, in <module>
    main()
  File "/builds/romainros/ynoverflow/backend/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
No changes detected
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/base.py", line 336, in run_from_argv
    connections.close_all()
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 224, in close_all
    connection.close()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 248, in close
    if not self.is_in_memory_db():
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 367, in is_in_memory_db
    return self.creation.is_in_memory_db(self.settings_dict['NAME'])
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/creation.py", line 12, in is_in_memory_db
    return database_name == ':memory:' or 'mode=memory' in database_name
TypeError: argument of type 'PosixPath' is not iterable

I've tried to add a app.settings.ci file and add --settings app.settings.ci in my test command, but I got this error :

django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

Any idea ?

EDIT

I use django-environ for my environment variables.

Here are the most important parts of my settings

# settings.py

from datetime import timedelta

import environ
from pathlib import Path

env = environ.Env()
# reading .env file
environ.Env.read_env()



# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG")

ALLOWED_HOSTS = env("ALLOWED_HOSTS")


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework_simplejwt.token_blacklist',
    'api'
]

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': env("DB_ENGINE"),
        'NAME': BASE_DIR / env("DB_NAME"),
    }
}

STATIC_URL = env("STATIC_URL")
1
  • how do you set your variables in settings.py? please atach your settings.py file to your question. Commented Nov 29, 2020 at 7:50

1 Answer 1

4

I found it ! I was using an old requirements.txt file I used on an other project, and the Django version in this file was set to 2.2, and for some reason, the BASE_DIR changed in the 3.1 django version (which I was using on this new project. I created the requirements file after I started the project)

credits : https://forum.djangoproject.com/t/django-tutorial-python-manage-py-startapp-polls-fails/2718/3

solution :

# requirements.txt

Django>=3.1
djangorestframework>=3.9.2
djangorestframework_simplejwt>=4.3.0
django-environ>=0.4.5

and

# .gitlab-ci.yml

image: python:latest

variables:
    SECRET_KEY: "this-is-my-secret-key"
    DEBUG: "True"
    ALLOWED_HOSTS: "['*']"
    DB_ENGINE: "django.db.backends.sqlite3"
    DB_NAME: "test_database.sqlite3"
    STATIC_URL: "/static/"

# This folder is cached between builds
# http://docs.gitlab.com/ee/ci/yaml/README.html#cache
cache:
  paths:
- ~/.cache/pip/

before_script:
  - pip install -r requirements.txt

test:
  script:
    - cd backend
    - python3 manage.py makemigrations
    - python3 manage.py migrate
    - python3 manage.py test
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.