node.js - GitLab CI 与 Postgres 错误 : connect ECONNREFUSED 127. 0.0.1:5432

标签 node.js postgresql gitlab sequelize.js gitlab-ci

我有一个用于测试、构建和部署代码的Gitlab CI pipeline。 该代码基本上是一个nodejs api,并且使用Sequelize ORM 管理数据库。 最近,我尝试了使用 Postgres 数据库 的管道。

这是 gitlab-ci.yml 文件:

image: trion/ng-cli-karma

variables:
  POSTGRES_DB: test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: password
  NODE_ENV: test

cache:
  paths:
  - wiki-quotes-client/node_modules/
  - wiki-quotes-server/node_modules/

stages:
    - test
    - build
    - deploy

test_server:
  services:
   - postgres:latest
  script:
   - cd wiki-quotes-server
   - npm install
   - npm install -g sequelize-cli
   - sequelize db:migrate
   - sequelize db:seed:all
   - npm run test

test_client:
    script:
        -  cd wiki-quotes-client
        -  npm install
        -  ng test --watch=false

build_angular:
  only:
    - master
  stage: build
  script:
    - npm install
    - cd wiki-quotes-client
    - ng build --prod
  artifacts:
    paths:
      - wiki-quotes-client/dist/wiki-quotes-client/.

deploy:
  only:
    - master
  stage: deploy
  dependencies:
    - build_angular
  script:
    - ls -all
    - apt-get update -qq
    - apt-get install -qq git
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - ls ~/.ssh/
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\t StrictHostKeyChecking no \n\n" > ~/.ssh/config'
    - ssh-keyscan 159.65.156.240 >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6b1b9a3a2b7bbb4a5b3b3a4a0bf96e7e3eff8e0e3f8e7e3e0f8e4e2e6" rel="noreferrer noopener nofollow">[email protected]</a> 'cd ~/wikiquotesapp; git checkout master; git pull;  cd wiki-quotes-server; npm install; npm start:prod'

现在它向我显示了这个错误:

ERROR: connect ECONNREFUSED 127.0.0.1:5432

此外,我还有一个问题是......

当我使用 Sequelize 部署带有数据库的 api 时,理想的路径是什么?

我想确保我的迁移上线,同时我的数据库应该在每次部署时进行播种,并且我不希望数据库有冗余数据。

最佳答案

在 Gitlab CI 中使用服务的工作方式与运行不同的 docker 容器(docker-compose 中的命名服务)基本相同,这些容器可供运行特定作业的容器使用。

因此,根据您的错误消息,我假设您的代码尝试访问 localhost 上的 postgres 数据库当您在开发人员机器上运行两者时,它就可以工作。在 Gitlab CI 中,这是两个“都有自己的 localhost ”的容器,并且必须使用像 postgres 这样的 dns 名称。连接到另一个容器。因此,当您使用名为 postgres 的图像时,Gitlab 也这样命名服务。

请尝试使用主机名postgres而不是localhost对于 Node 进程中的数据库连接,访问应该可以工作。根据您定义的其他变量,只需再添加一个 variable 就可以了。的POSTGRES_HOST: postgres (或类似的 - 我不熟悉sequelize配置)

Gitlab CI 服务详情请查看 the docs甚至为您访问 postgres 的特定用例提供示例服务和some clarification on this specific question .

关于node.js - GitLab CI 与 Postgres 错误 : connect ECONNREFUSED 127. 0.0.1:5432,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809920/

相关文章:

continuous-integration - Gitlab CI - 无法注册运行者

git - 如何获取 GitLab 项目分支中的最后一次提交状态?

node.js - 我需要 MongoDB 在执行其他操作之前等待预删除 Hook

javascript - NextJS : How can I use a loading component instead of nprogress?

python - `heroku pgbackups:capture` 在服务器上运行什么命令来执行备份?

linux - 如何在 Linux 服务器机器中提供 psql 密码

node.js - 在扩展 EventEmitter 的 TypeScript 类中声明事件

javascript - 层次结构的平面目录路径

sql - 在 PostgreSQL 中使用军事时间值作为时间?

gitlab - 无法将 .dll 文件推送到 GitLab