在Gitlab-CI上启动postgres服务

时间:2018-06-22 02:59:38

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

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

我尝试根据configuring the .gitlab-ci.yml fileusing 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

database.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

1 个答案:

答案 0 :(得分:1)

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

服务是另一个无法在本地访问的容器。因此,您必须通过主机名来访问它-默认情况下是服务的名称。您可以根据需要设置服务的可选别名documentation