我有一个用于测试、构建和部署代码的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/