[RUBY] Incorporate circleCI into CircleCI Rails app

Thing you want to do

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

What you need

  1. Register with circleCI
  2. Set the Repository you want to put under circleCI monitoring
  3. When managing environment variables using credentials.yml Create environment variables for master.key on circleCI
  4. Register the private key in circleCI to realize automatic deploy flow by capistrano

Registration of 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

It's strange because I wrote it while googled without any knowledge of CI / CD. It was from a sudden motivation to give it a try, so ...

Reference link

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

Recommended Posts

Incorporate circleCI into CircleCI Rails app
I tried to introduce CircleCI 2.0 to Rails app
Incorporate Docker into your Ruby on Rails portfolio!
Introducing CircleCI to Rails
Heroku app moving (rails)
Delve into Rails form_with
[Core ML] Incorporate an automatically generated ML model into the app
[Rails] New app creation --Notes--
Rails book review app RSpec introduction
Create a new app in Rails
Track Rails app errors with Sentry
Deploy your Rails app on Heroku