📘

Rails+MySQLのCI(RSpec,Rubocop)

2022/04/12に公開

Rails + MySQLでRSpecとRubocopのCIを行うGithubActionsです。

name: Run rspec, rubocop

on:
  push:
  pull_request:

jobs:
  rspec:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    services:
      mysql:
        image: mysql:8.0
        ports:
          - 3306:3306
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
        options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10
    defaults:
      run:
        working-directory: backend

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
          ruby-version: 3.0.2

      - name: Cache node modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-node-
      
      - name: .env
        run: cp .env.sample .env

      - name: Bundler and gem install
        run: |
          bundle install

      - name: Database create and migrate
        run: |
          cp config/database.yml.ci config/database.yml
          bundle exec rails db:create RAILS_ENV=test
          bundle exec rails db:migrate RAILS_ENV=test

      - name: Run rubocop-airbnb
        run: bundle exec rubocop --require rubocop-airbnb

      - name: Run rspec
        run: bundle exec rspec spec --format documentation

Discussion