Gitlab Django CI/CD postgres: 连接被拒绝
Gitlab Django CI/CD postgres: Connection refused
错误
我在使我的 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
类似的 Whosebug 问题:
使用 dj_database_url
包:
Django Gitlab 的默认设置 CI/CD
https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Django.gitlab-ci.yml
如您所见,我使用了非常简单的测试设置,但仍然无法正常工作。
脚本在 python manage.py makemigrations
行失败,因为您没有为它指定 --settings
。但实际上,您甚至不需要为测试手动 运行 迁移 - 它会由测试 运行 自动完成,到测试数据库。
所以从测试块中删除 python manage.py makemigrations
和 python manage.py migrate
,它应该 运行 成功。
此外,您永远不会希望 运行 makemigrations
自动。它是用于开发的工具,生成的迁移将包含在 VCS 中 (git)。
错误
我在使我的 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
类似的 Whosebug 问题:
使用 dj_database_url
包:
Django Gitlab 的默认设置 CI/CD https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Django.gitlab-ci.yml
如您所见,我使用了非常简单的测试设置,但仍然无法正常工作。
脚本在 python manage.py makemigrations
行失败,因为您没有为它指定 --settings
。但实际上,您甚至不需要为测试手动 运行 迁移 - 它会由测试 运行 自动完成,到测试数据库。
所以从测试块中删除 python manage.py makemigrations
和 python manage.py migrate
,它应该 运行 成功。
此外,您永远不会希望 运行 makemigrations
自动。它是用于开发的工具,生成的迁移将包含在 VCS 中 (git)。