带有Postgres的GitLab CI错误:连接ECONNREFUSED 127.0.0.1:5432

时间:2019-06-28 15:56:09

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

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

这是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 goutambseervi@159.65.156.240 'cd ~/wikiquotesapp; git checkout master; git pull;  cd wiki-quotes-server; npm install; npm start:prod'

现在它向我显示此错误:

  

错误:连接ECONNREFUSED 127.0.0.1:5432

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

当我使用Sequelize将api与数据库一起部署时,遵循的理想路径是什么?

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

1 个答案:

答案 0 :(得分:1)

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

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

请在节点进程中尝试使用主机名postgres代替localhost进行数据库连接,并且访问应该可以进行。根据您定义的其他变量,仅添加variable中的一个POSTGRES_HOST: postgres(或类似的方法-我对sequelize配置不熟悉)可能已经起作用

有关Gitlab CI中服务的详细信息,请检查the docs,甚至为您访问postgres服务和some clarification on this specific question的特定用例提供示例。

相关问题