Warm tip: This article is reproduced from serverfault.com, please click

python-使用简单的Django项目设置gitlab CI

(python - Setting up gitlab CI with simple django project)

发布于 2020-11-28 14:02:23

我有一个非常简单的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")
Questioner
Aralek
Viewed
11
Aralek 2020-12-02 01:51:28

我找到了 !我正在使用在其他项目上使用的旧的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