発生した事象
CircleCI上でRSpecを実行するための前準備として bundle exec rails db:create
を行う場面で PG::ConnectionBad
のエラーが発生してしまっていました。
結論
database.ymlとconfig.ymlの設定内容を確認しましょう。
今回の場合で言うとdatabase.ymlのtest環境のdatabaseのhost指定が抜けていたのが上記のエラーの原因でした。
解決した結果のファイル
config/database.yml
default: &default adapter: postgresql encoding: unicode pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: postgres password: shoukaigame_password host: db development: <<: *default database: shoukaiGame_development test: <<: *default host: localhost # ここの指定がないのでエラーになっていた database: shoukaiGame_test production: <<: *default database: shoukaiGame_production username: shoukaiGame password: <%= ENV['SHOUKAIGAME_DATABASE_PASSWORD'] %>
.circleci/config.yml
# Ruby CircleCI 2.0 configuration file # # Check https://circleci.com/docs/2.0/language-ruby/ for more details # version: 2 defaults: &defaults working_directory: ~/shoukaigame_app docker: # specify the version you desire here - image: circleci/ruby:2.6.3-node-browsers-legacy environment: RAILS_ENV: test TZ: /usr/share/zoneinfo/Asia/Tokyo BUNDLE_PATH: vendor/bundle BUNDLER_VERSION: 1.17.2 - image: postgres:11.1-alpine environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: shoukaigame_password - PGDATA: /dev/shm/pgdata/data jobs: build: <<: *defaults steps: - checkout - run: gem install bundler -v 1.17.2 - run: bundle install - persist_to_workspace: root: ~/shoukaigame_app paths: - ./* # RuboCop check!! code_analyze: <<: *defaults steps: - attach_workspace: at: ~/shoukaigame_app - run: name: install dependencies command: | bundle install --jobs=4 --retry=3 - run: name: Run Rubocop command: bundle exec rubocop --parallel rspec: <<: *defaults steps: - attach_workspace: at: ~/shoukaigame_app - run: name: install dependencies command: | bundle install --jobs=4 --retry=3 # Database setup - run: name: Database setup command: | bundle exec rake db:create RAILS_ENV=test bundle exec rake db:schema:load RAILS_ENV=test - run: name: Run RSpec command: | mkdir /tmp/test-results TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)" bundle exec rspec workflows: version: 2 build_and_test: jobs: - build - code_analyze: requires: - build - rspec: requires: - build
その他の気づきメモ
RubyのイメージをCI上で指定する場合にnodeを含まないものを指定してしまうと別途nodeをインストールしないと nodeが無い事によるエラーが発生するので基本はnodeを含むイメージを指定する方が良さそうです。