概要
- GitHub Actionsについて - GitHub ヘルプ
- 本稿は2019年9月10日時点の情報を元にしており、この時点でGitHub ActionsはBeta版のため、今後変更される可能性が大です
環境
- 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
やったこと
-
Features • GitHub ActionsからSign up(有効になるとrepositoryのルートに
Actions
タブが出現します) - GitHub Actions用の
database.yml.ci
を書いた -
Actions
タブからAdd a new workflow
してYAMLを書いた -
Gemfile
にgem 'webdrivers'
を追記した -
rails_helper.rb
に設定を追記した -
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">
おまけ
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さん、どこいった