错误
我在让我的 Django CI/CD 工作时遇到了很多麻烦。我正在使用 Django + postgres,我的测试用例都通过了,但我想实现持续集成。请注意以下输出来自 Gitlab Runner。
我不断收到此错误:(这...
是我遗漏了一些细节的地方,但它只是回溯或命名有关我的项目的特定内容等)
$ cd src
$ python manage.py makemigrations
/usr/local/lib/python3.9/site-packages/django/core/management/commands/makemigrations.py:105: RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
warnings.warn(
Migrations for 'app':
app/migrations/0001_initial.py
- Create model ...
...
...
$ python manage.py migrate
Traceback (most recent call last):
...
...
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
配置文件
.gitlab-cl.yml
image: python:latest
services:
- postgres:latest
variables:
POSTGRES_DB: postgres
POSTGRES_HOST: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
cache:
paths:
- ~/.cache/pip/
before_script:
- python -V # Print out python version for debugging
- pip install -r requirements.txt
test:
script:
- python manage.py makemigrations
- python manage.py migrate
- python manage.py test --settings mysite.cicd_settings
settings.py
如您所见,我指定了一个不同的设置文件cicd_settings
来运行我的测试:
from mysite.settings import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'postgres',
'PORT': '5432',
},
}
它继承了我的普通settings.py
文件的所有内容,但覆盖了数据库设置。
尝试过的解决方案
我尝试了以下解决方案:
Postgres Gitlab 教程: https ://docs.gitlab.com/ee/ci/services/postgres.html
类似的 Stackoverflow 问题: GitLab CI Django 和 Postgres
使用dj_database_url
包:
无法连接到服务器:连接被拒绝 (0x0000274D/10061) - 远程服务器上的 PostgreSQL
Django Gitlab CI/CD 的默认设置 https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Django.gitlab-ci.yml
正如你所看到的,我使用了一个非常简单的测试设置,但我仍然无法让它工作。