【问题标题】:Gitlab Django CI/CD postgres: Connection refusedGitlab Django CI/CD postgres:连接被拒绝
【发布时间】: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


    【解决方案1】:

    脚本在python manage.py makemigrations 行失败,因为您没有为它指定--settings。但实际上,您甚至不需要为测试手动运行迁移 - 它会由测试运行程序自动完成,迁移到测试数据库。

    所以从测试块中删除python manage.py makemigrationspython manage.py migrate,它应该会成功运行。

    此外,您永远不想自动运行makemigrations。它是一个用于开发的工具,由此产生的迁移将包含在 VCS (git) 中。

    【讨论】:

    • 删除 2 个迁移行确实会中断,但将 --setting 部分添加到它们确实可以解决问题,因此我建议您编辑答案以反映这一点。我已在此处stackoverflow.com/questions/68243461/… 提交了社区 Wiki 指南
    • 听起来不对。如果没有那条线,它到底是如何失败的?同样,您应该永远在您的 CI 代码中包含 makemigrations。您在本地计算机上手动运行它以生成迁移文件并将它们提交到 VCS。
    • 我已经为输出创建了一个 pastebin 链接以及确切的 yml 文件:pastebin.com/754htAB1
    • 在本地运行 makemigrations 后,您是否在存储库中包含 ALL 迁移文件?看起来他们不在那里,因此错误
    • 我不关注?我没有提交我的migrations/ 文件夹的内容(__init__.py 除外)。提交迁移不是不好的做法吗?尤其是在数据库经常为空的开发过程中,以及在初始化空数据库的测试期间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2018-07-03
    • 2021-04-12
    • 2021-03-23
    • 2021-10-12
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多