CircleCIでPostgreSQL 10を使うための設定
久々にCircleCIを利用したのですが、バージョンも2.0になっていたり、変わってる部分もあったので、結構設定ファイルを記述するのに時間が掛かってしまいました。
構成
- FW : Rails 5
- DB : PostgreSQL 10.1
設定ファイル
- .circleci/config.yml
version: 2
general:
branches:
only:
- master
- develop
jobs:
build:
working_directory: ~/api
docker:
- image: circleci/ruby:2.4.1-node
environment:
PGHOST: 127.0.0.1
PGUSER: username
RAILS_ENV: test
- image: circleci/postgres:10.1-alpine
environment:
POSTGRES_USER: username
POSTGRES_DB: api_test
POSTGRES_PASSWORD: password
steps:
- checkout
- run:
name: Install System Dependencies
command: |
sudo apt-get update
sudo apt-get install -y lsb-release
- run:
name: Install postgresql 10
command: |
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
cat /etc/apt/sources.list.d/pgdg.list
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
- restore_cache:
name: Restore bundle cache
keys:
- api-bundle-{{ checksum "Gemfile.lock" }}
- api-bundle-
- run:
name: Bundle Install
command: bin/bundle check --path vendor/bundle || bin/bundle install --path vendor/bundle --jobs 4 --retry 3
- save_cache:
name: Store bundle cache
key: api-bundle-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
- run:
name: Rubocop
command: bundle exec rubocop --rails -f html -o doc/rubocop.html --fail-level F
- store_artifacts:
path: doc
- run:
name: Set up DB
command: |
bundle exec rake db:structure:load --trace
environment:
DATABASE_URL: "postgres://usename@localhost:5432/api_test"
- run:
name: RSpec
command: bundle exec rspec --format progress spec
- store_artifacts:
path: coverage
つまづいたポイント
Rubocopを実行後、Exit codeが1で返って来る
1つでもWarningがあった場合でも、1になって返って仕様だったので、--fail-level で SEVERITY を指定して、Exit codeが0になるレベルに変更した
psqlコマンドが実行されない
Rails側のコンテナの方にpostgresqlをインストールする必要があった。
CircleCIが提供しているコンテナは、debianの為、以下の方法を参考に、PostgreSQL 10のインストールを設定ファイルに記述。
一番時間を喰ってしまったのは、設定を直す度に、コミット、Pushを繰り返して、トライ&エラーを行っていたので、コミットメッセージもめちゃくちゃで(後でrebaseはしたけど)、非効率だなと感じました。。。 もしかしたら最初からDockerコンテナを自作して、それを利用させる設定にしたほうが良かったのかな...