繁体   English   中英

使用简单的 django 项目设置 gitlab CI

[英]Setting up gitlab CI with simple django project

我有一个非常简单的 django 项目,上面有一些单元测试,我想设置 Gitlab 以在我每次提交新提交时运行这些测试。 我将 sqlite3 用于我的数据库(我计划稍后更改,但现在我想保持简单)并将我的一些设置变量存储在 a.env 文件中。

这是 my.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")

我找到了。 我正在使用我在另一个项目中使用的旧要求,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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM