初めに
今回は備忘録を兼ねて、circleciの基礎について解説します!
私と同じようなビギナーの方に向けてできるだけ分かりやすく解説しますので良ければご覧ください。
なお、今回使用するCircleCIのconfig.ymlはリンク先の記事を使用させていただいております。
【circleCI】Railsアプリでgithubと連携してrubocopとrspecテストを走らせる
使用する設定ファイル
今回はcircleciの設定ファイルについての解説ですので、導入方法などについては割愛します。
.circleci/config.yml
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
# ruby2.5.1を使ったアプリのため
- image: circleci/ruby:2.5.1-node-browsers
environment:
- BUNDLER_VERSION: 2.0.2
# ②注意!! circleCIは仮想環境を構築するので、そのときに使うデータベースを指定する必要があります。
- RAILS_ENV: 'test'
# Mysqlのバージョン5.6で動かしていたため、5.6を指定
- image: circleci/mysql:5.6
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
- MYSQL_ROOT_HOST: '%'
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/postgres:9.4
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run:
name: install dependencies
command: |
# ①bundler2.0.1以降のものを使っているのであれば、環境変数と合わせて指定する必要があります。
gem install bundler -v 2.0.2
bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache:
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
# ②ちょっと無理やりですが、database.ymlとdatabase.yml.ciを入れ替える記述です。
- run: mv config/database.yml.ci config/database.yml
# Database setup
- run: bundle exec rake db:create
- run: bundle exec rake db:schema:load
# rubocopを走らせる記述です。
- run:
name: Rubocop
command: bundle exec rubocop
# rspecを走らせる記述です。
# run tests!
- run:
name: run tests
command: |
mkdir /tmp/test-results
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
circleci tests split --split-by=timings)"
# ③ここでRspecJunitFormatterというgemをインストールしていないとエラーになります。gemfileに記述しましょう。
bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
# collect reports
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results
解説
.circleci/config.yml
# circleciのバージョンを指定。現在1, 2, 2.1が指定できる。
version: 2
# jobsは環境設定を行う。一つ以上のjobsを指定しなければならない。
jobs:
# job名。jobが一つの場合はbuildとしなければいけない。
build:
# dockerを使用し、イメージを取得する。
docker:
- image: circleci/ruby:2.5.1-node-browsers
environment:
# 現在bundlerが2.0.1以上の場合、環境変数を指定しないとbundle installに失敗する。
- BUNDLER_VERSION: 2.0.2
- RAILS_ENV: 'test'
-
# Mysqlのバージョン5.6で動かしていたため、5.6を指定
- image: circleci/mysql:5.6
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
- MYSQL_ROOT_HOST: '%'
# ワーキングディレクトリを指定する。このコマンド以降/repo下でコマンドが実行される。
working_directory: ~/repo
steps:
# checkoutはcircleciのビルドインステップ。
# ワーキングディレクトリに、gitリポジトリをコピーする。
- checkout
# 下記の-save_cacheで保存されたキャッシュ情報を使用することで、処理時間を短縮する。
- restore_cache:
# kyes:は-save_cacheで指定されたkey情報を取得する。
keys:
# 保存された(ハッシュ値)されたGemfile.lockの情報を使用している。
- v1-dependencies-{{ checksum "Gemfile.lock" }}
-
# Gemfile.lock情報の使用に失敗した際に使用される。
- v1-dependencies-
- run:
-
#コマンド名
name: install dependencies
#コマンドが複数行の場合は | を最初に指定する。
command: |
gem install bundler -v 2.0.2
#jobsは並列処理を実行するコア数を記載。retruは失敗した際に再実行する回数。pathはインストールする場所。(これらはbundlerのオプションです)
bundle install --jobs=4 --retry=3 --path vendor/bundle
# 上記のrestore_cacheで使用するキャッシュを保存。
- save_cache:
-
# pathsで保存するファイルの場所を指定。key:で情報を呼び出すためのキーを作成。checksumは指定されたファイルのハッシュ値を取得。
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
# ②ちょっと無理やりですが、database.ymlとdatabase.yml.ciを入れ替える記述です。
- run: mv config/database.yml.ci config/database.yml
# Database setup
- run: bundle exec rake db:create
- run: bundle exec rake db:schema:load
# rubocopを走らせる記述です。
- run:
name: Rubocop
command: bundle exec rubocop
# rspecを走らせる記述です。
- run:
name: run tests
command: |
mkdir /tmp/test-results
# circleci tests globで一致するテストファイルをグロブし、TEST_FILES変数に挿入。
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
# テスト速度を向上するため、テストにかかる処理時間からテストを分割して並列処理する。
circleci tests split --split-by=timings)"
# ③ここでRspecJunitFormatterというgemをインストールしていないとエラーになります。gemfileに記述しましょう。
# テストを実行してその結果をrspec.xmlに出力する。
bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
# テスト情報をweb上で確認することができる。
- store_test_results:
path: /tmp/test-results
# CIでの処理実行中にできた成果物を artifacts に保存して、終了後にCircleCIのWEB上からダウンロードすることができる。
- store_artifacts:
path: /tmp/test-results
destination: test-results
最後に
今回はcircleciのconfigファイルについて解説しました。
一見複雑そうですが、理解するとそこまで難しいものではないですね!