LoginSignup
8
5

More than 3 years have passed since last update.

RailsアプリをCircleCI(2.1)を使ってHerokuにデプロイするときの設定

Posted at

はじめに

Railsアプリの開発効率を上げるため、
GitHubにpushしたら静的コード解析(rubocop)と自動テスト(rspec)を実行し、
stagingブランチにマージしたらSTG環境に、masterブランチにマージをしたら本番環境に自動デプロイするように設定した。

前提

  • Railsのアプリケーションを作成済み
  • Rubocop, Rspec設定済み
  • Herokuの登録&設定済み
  • CircleCIのユーザおよびプロジェクト登録済み&GitHubと連携済み

環境

  • Ruby 2.5.0
  • Rails 5.2.2
  • CircleCI 2.1
  • SQLite(Rspec実行時)

手順

  • CircleCIに環境変数を設定する
  • .circleci/config.ymlを記述し、GitHubにpushする

CircleCIに環境変数を設定

Project Settings > Environment Variables にAdd Variableで以下を設定する。

  • HEROKU_API_KEY(HerokuのAccount Settings > Account > API Keyの内容を記載)
  • HEROKU_APP_NAME_PRD(本番環境のHerokuアプリ名)
  • HEROKU_APP_NAME_STG(STG環境のHerokuアプリ名)

設定ファイルの記述

以下のファイルを作成する。
masterブランチの時のみ本番環境のHerokuアプリにデプロイされ、
stagingブランチの時のみSTG環境のHerokuアプリにデプロイされる。

circleci/config.yml
version: 2.1
orbs:
  ruby-orbs: sue445/ruby-orbs@1.6.0
  heroku: circleci/heroku@1.0.1

workflows:
  build_test_and_deploy:
    jobs:
      - build
      - rubocop:
          requires: 
            - build
      - rspec:
          requires:
            - build
            - rubocop
      - deploy_prd:
          requires:
            - build
            - rspec
          filters:
            branches:
              only:
                - master
      - deploy_stg:
          requires:
            - build
            - rspec
          filters:
            branches:
              only:
                - staging

executors:
  default:
    working_directory: ~/repo
    docker:
      - image: ruby:2.5.0-stretch
        environment:
          BUNDLE_PATH: vendor/bundle
          RAILS_ENV: test

jobs:
  build:
    executor: default
    steps:
      - checkout
      - bundle-install
  rubocop:
    executor: default
    steps:
      - preparate
      - run:
          name: Rubocop
          command: bundle exec rubocop
  rspec:
    executor: default
    steps:
      - preparate
      - run: bundle exec rails db:create
      - run: bundle exec rails db:schema:load
      - run:
          name: Rspec
          command: bundle exec rspec
  deploy_prd:
    executor: heroku/default
    parameters:
      app-name: 
        default: $HEROKU_APP_NAME_PRD
        type: string
      post-deploy:
        default:
          - run:
              command: heroku run rails db:migrate --app $HEROKU_APP_NAME_PRD
        type: steps
    steps:
      - checkout
      - heroku/install
      - heroku/deploy-via-git:
          app-name: << parameters.app-name >>
      - steps: << parameters.post-deploy >>
  deploy_stg:
    executor: heroku/default
    parameters:
      app-name: 
        default: $HEROKU_APP_NAME_STG
        type: string
      post-deploy:
        default:
          - run:
              command: heroku run rails db:migrate --app $HEROKU_APP_NAME_STG
        type: steps
      force:
        default: true
        type: boolean
    steps:
      - checkout
      - heroku/install
      - heroku/deploy-via-git:
          app-name: << parameters.app-name >>
      - steps: << parameters.post-deploy >>

commands:
  bundle-install:
    steps:
      - ruby-orbs/bundle-install:
          bundle_clean: true
          bundle_extra_args: ''
          bundle_gemfile: Gemfile
          bundle_jobs: 4
          bundle_path: vendor/bundle
          bundle_retry: 3
          cache_key_prefix: v1-bundle-dependencies
          restore_bundled_with: true
  preparate:
    steps:
      - checkout
      - bundle-install

Rubocop, Rspecもorbを使ったほうがよかったかもしれないです。

注意

3rdパーティ製のorb(sue445/ruby-orbs@1.6.0)を利用する場合は、Orb Security Settingsを有効にしておく必要がある。

Organization Settings > Security > Orb Security Settings
Allow Uncertified OrbsをYesにする。

参考

8
5
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
8
5