[Ruby] [CircleCI] Incorporate circleCI into Rails app

1 minute read

Thing you want to do

  1. When you push, run test and notify slack of success or fail.
  2. When merged to master, after doing 1, run deploy job and notify slack of success or fail

Required

  1. Register with circleCI
  2. Set the Repository you want to put under circleCI monitoring
  3. If environment variables are managed using credentials.yml, create an environment variable for master.key in circleCI
  4. In order to realize the automatic deploy flow by capistrano, register the private key in circleCI

Register master.key see -> https://qiita.com/murata0705/items/9c99fc715d8b987a5b6e SSH key registration see -> https://circleci.com/docs/ja/2.0/add-ssh-key/#%E6%89%8B%E9%A0%86

Reference

yaml:.circleci/config.yml


version: 2.1
orbs:
  ruby: circleci/[email protected]
  slack: circleci/[email protected]
jobs:
  build:
    docker:
      -image: circleci/ruby:2.5.1-node-browsers
        environment:
          RAILS_ENV: test
          BUNDLER_VERSION: 2.1.4
      -image: circleci/mysql:5.6.47-ram
    working_directory: ~/app
    steps:
      -checkout
      -run:
          name: Install bundler
          command: gem install bundler -v 2.1.4
      -run:
          name: Which bundler?
          command: bundle -v
      -restore_cache:
          keys:
            -v1-dependencies-{{ checksum "Gemfile.lock" }}
        # fallback to using the latest cache if no exact match is found
            -v1-dependencies-
      -run:
          name: Bundle Install
          command: bundle install --jobs=4 --retry=3 --path vendor/bundle
      -save_cache:
          paths:
            -./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      -run:
          name: Wait for DB
          command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s

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

  # 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)"

            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
  # run rubocop
      -run:
          name: run rubocop
          command: bundle exec rubocop
      -slack/status:
          success_message:':successcircleci:$CIRCLE_BRANCH Build Success\n:github: User: $CIRCLE_USERNAME'
          failure_message:':failcircleci2:$CIRCLE_BRANCH Build Fail\n:github: User: $CIRCLE_USERNAME'
          webhook:'${SLACK_WEBHOOK}'
  deploy:
    docker:
      -image: circleci/ruby:2.5.1-node-browsers
        environment:
          BUNDLER_VERSION: 2.1.4
    working_directory: ~/app
    steps:
      -checkout
      -run:
          name: Install bundler
          command: gem update bundler
      -run:
          name: Which bundler?
          command: bundle -v
      -restore_cache:
          keys:
            -v1-dependencies-{{ checksum "Gemfile.lock" }}
        # fallback to using the latest cache if no exact match is found
            -v1-dependencies-
      -run:
          name: Bundle Install
          command: bundle install --jobs=4 --retry=3 --path vendor/bundle
      -save_cache:
          paths:
            -./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      -add_ssh_keys:
          fingerprints:
            -0f:2a:6c:6a:85:f7:b4:5a:18:95:9f:71:f3:14:d0:a8
      -run:
          name: caistrano deploy
          command: bundle exec cap production deploy
      -slack/status:
          success_message:':successcircleci:$CIRCLE_BRANCH Deploy Success\n:github: User: $CIRCLE_USERNAME'
          failure_message:':failcircleci2:$CIRCLE_BRANCH Deploy Fail\n:github: User: $CIRCLE_USERNAME'
          webhook:'${SLACK_WEBHOOK}'
workflows:
  version: 2.1
  build-deploy:
    jobs:
      -build
      -deploy:
          requires:
            -build
          filters:
            branches:
              only: master

Supplement

I wrote it while googled without any knowledge of CI/CD. It was because I had a sudden motivation to try it out, so…

https://qiita.com/AK4747471/items/882b70f663f4a80ae770