【发布时间】:2021-09-15 06:38:08
【问题描述】:
错误
让我的 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 and Postgres
使用dj_database_url 包:
Could not connect to server: Connection refused (0x0000274D/10061) - PostgreSQL on remote server
Django Gitlab CI/CD 的默认设置 https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Django.gitlab-ci.yml
如您所见,我使用了一个非常简单的设置进行测试,但仍然无法正常工作。
【问题讨论】:
标签: django postgresql gitlab continuous-integration cicd