gpt4 book ai didi

ruby-on-rails - 在 Gitlab-CI 上启动 postgres 服务

转载 作者:行者123 更新时间:2023-11-29 12:52:20 24 4
gpt4 key购买 nike

我有一个使用 Postgres 的 Rails5 应用程序。当我推送新提交时,我正在尝试使用 Gitlab CI 运行我的规范测试。

我尝试根据 configuring the .gitlab-ci.yml file 的文档设置我的文件和 using the postgres service

我没有改变我的运行者,所以它在 Gitlab 使用的任何默认情况下运行。

.gitlab-ci.yml

services:
- postgres:9.6

variables:
POSTGRES_DB: galapagos_test
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""

before_script:
# System
- apt-get update -qq

# Ruby
- ruby -v
- which ruby

# Gems
- 'echo ''gem: --no-ri --no-rdoc'' > ~/.gemrc'
- gem install bundler
- bundle install --without production --jobs $(nproc) "${FLAGS[@]}"

# App
- RAILS_ENV=test bundle exec rake db:create
- RAILS_ENV=test bundle exec rake db:migrate

rspec:
script:
- bundle exec rspec

数据库.yml

我的 Rails 应用 database.yml 配置为使用上面指定的相同 postgres 数据库、用户名和密码

development: &default
adapter: postgresql
database: galapagos_development
encoding: utf8
host: localhost
min_messages: warning
pool: 2
timeout: 5000

test:
<<: *default
database: galapagos_test
user: runner
password: ""

错误

当构建运行时,rake 任务总是失败

$ RAILS_ENV=test bundle exec rake db:create
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?
Couldn't create database for {"adapter"=>"postgresql", "database"=>"galapagos_test", "encoding"=>"utf8", "host"=>"localhost", "min_messages"=>"warning", "pool"=>2, "timeout"=>5000, "user"=>"runner", "password"=>""}
rake aborted!
PG::ConnectionBad: 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?
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `connect'

显然 postgres 服务已安装(构建日志表明 9.6 已安装)但 psql 服务器未启动。

如何启动 postgres 服务?

我尝试了我看到的 2 个“常见”解决方案,但它们也都会产生错误。

$ /etc/init.d/postgresql restart
/bin/bash: line 69: /etc/init.d/postgresql: No such file or directory
ERROR: Job failed: exit code 1


$ service postgresql start
postgresql: unrecognized service
ERROR: Job failed: exit code 1

最佳答案

database.yml 中将 host: localhost 更改为 host: postgres

服务是另一个无法在本地访问的容器。所以你必须通过它的主机名访问它——默认情况下是服务的名称。如有必要,您可以为服务设置可选别名documentation

关于ruby-on-rails - 在 Gitlab-CI 上启动 postgres 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com