【问题标题】:CI/CD pipeline for Django failureDjango 失败的 CI/CD 管道
【发布时间】:2021-10-19 20:38:53
【问题描述】:

我的 gitlab ci 管道不断失败。似乎被困在这里。实际上我对 CI 的东西还是新手,所以我不知道做错了什么。任何帮助将不胜感激以下是 .gitlab-ci.yml 文件

image: python:latest

services:
  - postgres:latest

variables:
  POSTGRES_DB: thorprojectdb
  POSTGRES_PASSWORD: $''
  POSTGRES_USER: $majesty
  POSTGRES_HOST_AUTH_METHOD: trust

# This folder is cached between builds
# http://docs.gitlab.com/ee/ci/yaml/README.html#cache
cache:
  paths:
    - ~/.cache/pip/

before_script:
  - python -V

connect:
  image: postgres
  script:
  # official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
  - export PGPASSWORD=$POSTGRES_PASSWORD
  - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"

build:
  stage: build
  script:
  - pip install -r requirements.txt
  - python manage.py migrate
  only:
    - EC-30

在我的 settings.py 文件中,我有以下设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'projectdb',
        'HOST': 'postgres',
        'PASSWORD': ''
    }
}

但是当我推送到 gitlab 时,构建过程一直失败。 - pip install -r requirements.txt 运行完美,但一旦到达 - python manage.py migrate,它就会失败。以下是我得到的错误

django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1

【问题讨论】:

    标签: django gitlab


    【解决方案1】:

    分析.gitlab-ci.yml文件的描述,很明显你在文件(POSTGRES_DB)中声明了数据库,但是你缺少与凭据相关的信息,DB_USER, DB_PASS,如下面的链接所述:

    gitlab-config-postgres

    请记住,最好在存储库的 CI/CD 部分使用声明变量。欲了解更多信息:

    ci-cd-gitlab-variables

    带有配置示例的存储库:

    repository-example-gitlab-postgres

    services:
      - postgres
    
    variables:
      # Configure postgres service (https://hub.docker.com/_/postgres/)
      POSTGRES_DB: custom_db
      POSTGRES_USER: custom_user
      POSTGRES_PASSWORD: custom_pass
    
    connect:
      image: postgres
      script:
      # official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
      - export PGPASSWORD=$POSTGRES_PASSWORD
      - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
    

    【讨论】:

    • 我刚刚编辑了问题以包含一些行。可以帮助我检查我做错了什么,因为我收到以下错误django.db.utils.OperationalError: FATAL: role "root" does not exist。请@marcosnasp
    • @Sammy 请检查链接:stackoverflow.com/questions/30273347/…。如果您没有在 DATABASE 字典块中指定用户,将使用 root 用户。
    • 指定用户后,我开始出现以下错误。 django.db.utils.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? Cleaning up project directory and file based variables ERROR: Job failed: exit code 1
    • @Sammy 尝试运行命令,启动并启用 postgres,你应该把它放在部分脚本上,检查这个链接:forum.gitlab.com/t/…stackoverflow.com/questions/35455109/…docs.gitlab.com/ee/ci/services/…
    猜你喜欢
    • 2022-10-25
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    相关资源
    最近更新 更多