GitLab CI with Postgres 错误:连接 ECONNREJECT 127.0.0.1:5432



我有一个测试、构建和部署代码的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 *nt StrictHostKeyChecking no nn" > ~/.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'

现在它向我显示此错误:

错误:连接 ECONN,拒绝 127.0.0.1:5432

另外,我的另一个问题是...

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

我想确保我的迁移生效,同时我的数据库应该在每次部署时设定种子,并且我不希望数据库有冗余数据。

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

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

请尝试在节点进程中使用主机名postgres而不是localhost数据库连接,访问应该可以正常工作。根据您定义的其他变量,这可能已经可以通过再添加一variablePOSTGRES_HOST: postgres(或类似 - 我不熟悉续集配置)来工作

有关 Gitlab CI 中服务的详细信息,请查看文档,这些文档甚至提供了访问postgres服务的特定用例的示例,并对此特定问题进行了一些说明。

最新更新