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

标签 ruby-on-rails postgresql gitlab gitlab-ci

我有一个使用 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/

相关文章:

ruby-on-rails - 滚动条的未定义方法拆分

sql - 计算直到某个日期的唯一组合 - 每月

SQL 使用嵌套的 INSERT INTO?

docker - Gitlab CI运行docker review应用

ruby-on-rails - 使用 Rspec + Capybara 在 Rails 中测试错误页面

ruby-on-rails - 如何在ActiveRecord查询中使用OR条件

ruby-on-rails - Rails 2.3 中的测试服务器端口是否发生变化?

postgresql - 无法使用 docker compose 通过 postgresql 启动 FastAPI 服务器

gitlab - 如何在 JFrog 中自动删除 Artifactory ,同时删除 Gitlab 上的特定分支?

ssh - GitLab ssh 到本地用户的主机名导致 "fatal: Could not read from remote repository"