Circl CI の導入 ~ rubocop と rspec を連携 ~

## 概要
- ポートフォリオ作成のため、CircleCI を導入。
- 初心者なので間違っているところあると思いますがご了承ください。

## 下記サイトを参考に導入

[【Rails】GithubとCircleCIを連携してcommit時にrspecとrubocopを動かす - Qiita](https://qiita.com/junara/items/a40bb231c405be7983f7)

## 設定内容

```yaml
version: 2
jobs:
  rubocop:
    docker:
      - image: circleci/ruby:2.7-browsers-legacy
        environment:
          RAILS_ENV: test
          POSTGRES_HOST: 127.0.0.1
      - image: circleci/postgres:9.4
        environment:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: root
          POSTGRES_DB: photo_submission_test
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-
      - run: bundle install --jobs=4 --retry=3 --path vendor/bundle
      - run: yarn install
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      # Rubocop
      - run:
          name: Rubocop
          command: bundle exec rubocop

  rspec:
    docker:
      - image: circleci/ruby:2.7-browsers-legacy
        environment:
          RAILS_ENV: test
          POSTGRES_HOST: 127.0.0.1
      - image: circleci/postgres:9.4
        environment:
          POSTGRES_USER: root
          POSTGRES_DB: photo_submission_test
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-
      - run: bundle install --jobs=4 --retry=3 --path vendor/bundle
      - run: yarn install
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      - run: bundle exec rake db:create
      - run: bundle exec rake db:schema:load
      # Rspec
      - run:
          name: Rspec
          command: bundle exec rspec

workflows:
  version: 2
  rubocop_rspec:
    jobs:
      - rubocop
      - rspec:
          requires:
            - rubocop
```

## プルリク出したら以下エラー

```bash
#!/bin/bash -eo pipefail
bundle install --jobs=4 --retry=3 --path vendor/bundle
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set path 'vendor/bundle'`, and stop using this flag
Your Ruby version is 2.7.2, but your Gemfile specified 2.7.1

Exited with code exit status 18
CircleCI received exit code 18
```

image 部分を変更

```yaml
- image: circleci/ruby:2.7.1
```

また、steps 以下の下記を変更

```yaml
run: bundle install --jobs=4 --retry=3 --path vendor/bundle

↓

run: bundle config set path 'vendor/bundle' 
```

再びプルリクするも yarn コマンドがないよとエラー出たため、コメントアウトして実行。下記エラー

```bash
#!/bin/bash -eo pipefail
bundle exec rubocop
bundler: command not found: rubocop
Install missing gem executables with `bundle install`

Exited with code exit status 127
CircleCI received exit code 127
```

## 色々エラー修正して下記で通った

```yaml
version: 2
jobs:
  rubocop:
    docker:
      - image: circleci/ruby:2.7.1
        environment:
          RAILS_ENV: test
          POSTGRES_HOST: 127.0.0.1
      - image: circleci/postgres:12.4
        environment:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: root
          POSTGRES_DB: photo_submission_test
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-
      # - run: bundle config set path 'vendor/bundle'
      - run: bundle install
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      # Rubocop
      - run:
          name: Rubocop
          command: bundle exec rubocop

  rspec:
    docker:
      - image: circleci/ruby:2.7.1
        environment:
          RAILS_ENV: test
          POSTGRES_HOST: 127.0.0.1
      - image: circleci/postgres:12.4
        environment:
          POSTGRES_USER: root
          POSTGRES_PASSWORD: root
          POSTGRES_DB: photo_submission_test
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-
      # - run: bundle config set path 'vendor/bundle'
      - run: bundle install
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}
      - run: bundle exec rake db:create
      - run: bundle exec rake db:schema:load
      # Rspec
      - run:
          name: Rspec
          command: bundle exec rspec

workflows:
  version: 2
  rubocop_rspec:
    jobs:
      - rubocop
      - rspec:
          requires:
            - rubocop
```

database.yml.ci

```yaml
test:
  adapter: postgresql
  encoding: unicode
  pool: 5
  database: daily-report-api_test
  username: postgres
  password: postgres
  host: localhost
```