LoginSignup
6
6

More than 3 years have passed since last update.

Rails(SystemSpec+webdrivers+RDB)をGitHubActionsでCIする

Last updated at Posted at 2019-09-10

概要

環境

  • ruby 2.6.3
  • Rails 5.2.3
  • RSpec 3.8
  • rspec-rails (3.8.2)
  • webdrivers (4.1.2)
  • postgres (PostgreSQL) 11
  • mysql Ver 14.14 Distrib 5.7.27

やったこと

  1. Features • GitHub ActionsからSign up(有効になるとrepositoryのルートに Actions タブが出現します)
  2. GitHub Actions用の database.yml.ci を書いた
  3. Actions タブから Add a new workflow してYAMLを書いた
  4. Gemfilegem 'webdrivers' を追記した
  5. rails_helper.rb に設定を追記した
  6. README.md にステータスバッジを表示させた

サンプル

config/database.yml.ci
test:
  adapter: postgresql
  encoding: unicode
  database: blog_test # 適当に
  pool: 20
  username: <%= ENV["POSTGRES_USER"] %>
  password: <%= ENV["POSTGRES_PASSWORD"] %>
  host: localhost
.github/workflows/main.yml
name: Build

on: [push, pull_request]

jobs:
  test:
    name: Test
    strategy:
      matrix:
        # os: [ ubuntu-latest, windows-latest, macOS-latest ]
        os: [ ubuntu-latest ]
        ruby: [ '2.6.3' ]
    runs-on: ${{ matrix.os }}

    services:
      postgres:
        image: postgres:11
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - uses: actions/checkout@v1
    - name: Setup Ruby version
      uses: actions/setup-ruby@master
      with:
        version: ${{ matrix.ruby }}

    - name: Install dependent libralies
      run: sudo apt-get install libpq-dev

    - name: Setup bundler
      run: gem install bundler

    - name: bundle install
      run: bundle install --jobs 4 --retry 3

    - name: Setup Database
      run: |
        cp config/database.yml.ci config/database.yml
        bundle exec rake db:create
        bundle exec rake db:schema:load
      env:
        RAILS_ENV: test
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres

    - name: Run RSpec
      run: bundle exec rspec
      env:
        RAILS_ENV: test
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
# Gemfile
group :test do
  gem 'capybara'
  gem 'webdrivers'
end
spec/rails_helper.rb
RSpec.configure do |config|
  config.before(:each) do |example|
    # `describe 〜, type: :system do` のspecを対象にする
    if example.metadata[:type] == :system
      driven_by :selenium_chrome_headless, screen_size: [1600, 1600]
    end
  end
end
README.md
<img alt="{workflow_name}" src="https://github.com/{github_id}/{repository}/workflows/{workflow_name}/badge.svg">
  • {workflow_name} は前述の .github/workflows/main.yml の1行目に対応しています
  • このように表示されます Build

おまけ

MySQL 5.7対応

config/database.yml.ci
test:
  adapter: mysql2
  charset: utf8mb4
  collation: utf8mb4_bin
  encoding: utf8mb4
  database: blog_test # 適当に
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: root
.github/workflows/main.yml
name: Build

on: [push, pull_request]

jobs:
  setup_and_test_execution:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Setup Ruby version
      uses: actions/setup-ruby@master
      with:
        version: 2.6.3

    - name: Install dependent libralies
      run: sudo apt-get install libmysqlclient-dev

    - name: Setup bundler
      run: gem install bundler

    - name: bundle install
      run: bundle install --jobs 4 --retry 3

    - name: Setup Database
      run: |
        cp config/database.yml.ci config/database.yml
        bundle exec rake db:create
        bundle exec rake db:schema:load
      env:
        RAILS_ENV: test

    - name: Run RSpec
      run: bundle exec rspec
      env:
        RAILS_ENV: test

ハマったこと

その1

spec/rails_helper.rb
-     caps = Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: { args: %w(--headless --no-sandbox --disable-gpu) })
-     driven_by :selenium, using: :chrome, screen_size: [1600, 1600], options: { desired_capabilities: caps }
+     driven_by :selenium_chrome_headless, screen_size: [1600, 1600]

上記の修正前、ローカルでは非headlessモードでテストが走り、GitHub上では unknown error: DevToolsActivePort file doesn't exist が発生して、ふがふがしました(最近はCodeceptJSに浮気してたもんで)。

その2

workflowをMySQL対応に書き換える際、PostgreSQLと同様にDockerイメージを取得していたのですが、
Mysql2::Error::ConnectionError: Access denied for user 'root'@'localhost' (using password: YES)
が出てふがふが。なんとMySQLは以下が最初から入っていたので不要っした☠️

  • MySQL (mysql Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using EditLine wrapper)
  • MySQL Server (user:root password:root)

ココに書いてありました)

あとがき

  • GitHub Actionsさん、正式リリースではキャッシュが効きますように🙏
  • Visual Editorさん、どこいった

参考文献(ちょー助かりました🙇‍♂️)

6
6
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
6
6