LoginSignup
4
2

More than 5 years have passed since last update.

circleCI 2.0でRails5.1, webpack (yarn) で導入してみた。

Last updated at Posted at 2018-01-29

所感

circleCI2.0を新規プロジェクトに導入してみた。
1.0系の頃に使っていた頃の知識が全く通用できなく言語のテンプレートで使ってみるとRspecが標準になっていたため、Minitest用に書き直し。
1.0系に比べて設定項目が多くなっていたこと、
Docker化していて、1.0からアップグレードする人たちを考えると闇が深く感じたが、勘所が少なくてよかった。

勘所だけ説明して記事を終わろうかと思う。

前提条件

  • rails 5.1.4
  • ruby 2.4.3
  • mysql 5.7.21
  • weboack
  • yarn
  • minitest
# 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
       - image: circleci/ruby:2.4.1-node-browsers
         environment:
           RAILS_ENV: test
           DATABASE_HOST: 127.0.0.1

      # 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/mysql:5.7
#         environment:
#           MYSQL_DATABASE: sampleapp_test

    working_directory: ~/sampleapp

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "Gemfile.lock" }}-{{ checksum "yarn.lock" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run:
          name: install dependencies
          command: |
            bundle install --jobs=4 --retry=3 --path vendor/bundle
      - run:
          name: install dependencies
          command: yarn

      - save_cache:
          paths:
            - ./vendor/bundle
            - ./node_modules
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}-{{ checksum "yarn.lock" }}


      # Database setup
      - run: bundle exec rake db:create
      - run: bundle exec rake db:schema:load
      # seed
      # - run: bundle exec rake db:fixtures:load

      # weboack
      - run: bundle exec bin/webpack

      # run tests!
      - run: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
      - run: mkdir ~/reports
      - run:
          command: bundle exec rake test TESTOPTS="--ci-dir=~/reports"
          when: always
      - store_test_results:
          path: ~/reports

Docker imageについて

jobs:
  build:
    docker:
      # specify the version you desire here
       - image: circleci/ruby:2.4.1-node-browsers
         environment:
           RAILS_ENV: test
           DATABASE_HOST: 127.0.0.1

      # 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/mysql:5.7
#         environment:
#           MYSQL_DATABASE: sampleapp_test

imageについて

circleCIはDocker採用されているため、imageを用意する必要がある。
ただcircleCIがimageを用意してくれていて、標準のimageを用意するよりは設定が全く必要なかったので、こちらを使うと良いだろう。
DBについても、環境変数を設定してあげると詰まることがないので、デフォルト値と自分のアプリに合わせてみると良いだろう。

テストフレームワークについて

     # run tests!
      - run: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3
      - run: mkdir ~/reports
      - run:
          command: bundle exec rake test TESTOPTS="--ci-dir=~/reports"
          when: always
      - store_test_results:
          path: ~/reports

公式に記載があるのでコピペで良いと思う。
minitest で設定だが、テスト結果のサマリーとして artifacts がうまく表示できていなかった。
このあたりについては、ご存じの方コメントでもらえると幸いです。💧

設定のバリデーションについて

毎回アップロードして設定が通ってるかどうかチェックするのはとても時間がかかるため、ローカル環境でもチェックできる方法が公式で用意されていたので最初の導入時には入れておくと良い。

Using the CircleCI Command Line Interface (CLI)

参考URL

P.S.

circleCIで消耗しないことを願って!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2