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

阿拉雷克

我有一个非常简单的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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Gitlab上为django项目设置CI / CD

来自分类Dev

无法使用symfony项目设置Gitlab-ci

来自分类Dev

如何使用Gitlab CI运行程序设置非常简单的部署?

来自分类Dev

使用MATLAB的Gitlab CI

来自分类Dev

如何使用gitlab ci测试django

来自分类Dev

使用GitLab CI连续交付

来自分类Dev

尝试使用gitlab和远程服务器设置CI

来自分类Dev

在Travis CI中为Django项目设置Postgresql

来自分类Dev

为Android项目配置gitlab ci

来自分类Dev

Gitlab中的Wordpress CI

来自分类Dev

Gitlab CI运行器

来自分类Dev

Gitlab CI管道作业

来自分类Dev

Gitlab CI Ssl 错误

来自分类Dev

GitLab CI 脚本变量

来自分类Dev

如何设置gitlab CI运行程序?

来自分类Dev

Angularjs + Grunt + Bower + Gitlab CI。测试设置

来自分类Dev

在Gitlab CI / CD中配置REDIS以在Django中使用

来自分类Dev

使用Travis CI设置模块

来自分类Dev

使用Gitlab CI生成Android构建

来自分类Dev

GitLab CI无法安装/使用PhantomJS

来自分类Dev

使用keycloak作为gitlab-ci服务

来自分类Dev

使用GitLab CI / CD等GitHub操作

来自分类Dev

使用GitLab CI构建奇异容器

来自分类Dev

在gitlab CI / CD中使用硒

来自分类Dev

如何使用ros | 与gitlab-ci cmake

来自分类Dev

使用gitlab-ci构建Erlang

来自分类Dev

使用 Gitlab-CI 构建 Webpack 失败

来自分类Dev

在已安装GitLab的服务器上设置GitLab CI

来自分类Dev

在Gitlab CI中包含Ghostscript的最简单方法