몇 가지 단위 테스트가있는 매우 간단한 django 프로젝트가 있으며 새 커밋을 수행 할 때마다 해당 테스트를 실행하도록 Gitlab을 설정하고 싶습니다. 데이터베이스에 sqlite3를 사용하고 (나중에 변경할 계획이지만 지금은 간단하게 유지하고 싶습니다) 일부 설정 변수를 .env 파일에 저장했습니다.
다음은 .gitlab-ci.yml 파일입니다 (gitlab에서 제안한 기본 파일을 사용하고 필요하지 않다고 생각하는 파일을 제거했습니다).
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
하지만 커밋하면이 오류가 발생합니다.
$ 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
app.settings.ci
파일을 추가 --settings app.settings.ci
하고 테스트 명령 을 추가하려고했지만 이 오류가 발생했습니다.
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
어떤 생각?
편집하다
내 환경 변수에 django-environ을 사용합니다.
내 설정에서 가장 중요한 부분은 다음과 같습니다.
# 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")
찾았어요 ! 다른 프로젝트에서 사용한 이전 requirements.txt 파일을 사용하고 있었고이 파일의 Django 버전은 2.2로 설정되었으며 어떤 이유로 BASE_DIR이 3.1 django 버전에서 변경되었습니다 (이 새 프로젝트에서 사용하고 있음). . 프로젝트를 시작한 후 요구 사항 파일을 만들었습니다.)
크레딧 : https://forum.djangoproject.com/t/django-tutorial-python-manage-py-startapp-polls-fails/2718/3
해결책 :
# requirements.txt
Django>=3.1
djangorestframework>=3.9.2
djangorestframework_simplejwt>=4.3.0
django-environ>=0.4.5
과
# .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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다