Github ActionsでRubocopなどを実行するようにしていて、RSpecもGithub ActionsにできるとCircle CIにお金払わなくてよくなるので、やってみたという内容です。
Circle CIの設定ファイルは以下です。
.circleci/config.yml
version: 2
jobs:
build:
working_directory: ~/someproj
docker:
- image: circleci/ruby:2.6.5-node-browsers
environment:
RAILS_ENV: test
PGHOST: 127.0.0.1
PGUSER: root
- image: circleci/postgres:9.6.2-alpine
environment:
POSTGRES_USER: root
POSTGRES_DB: someproj_test
steps:
- checkout
- restore_cache:
name: Restore bundle cache
keys:
- someproj-bundle-{{ checksum "Gemfile.lock" }}
- run:
name: Install Required Libraries
command: sudo apt-get update && sudo apt-get install -y optipng jpegoptim
- run:
name: Bundle Install
command: |
set -ex
gem uninstall bundler
gem install bundler -v 2.1.4
bin/bundle check --path vendor/bundle || \
bin/bundle install --clean --path vendor/bundle --jobs 4 --retry 3
- save_cache:
name: Store bundle cache
key: someproj-bundle-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
- restore_cache:
keys:
- someproj-yarn-{{ checksum "yarn.lock" }}
- someproj-yarn-
- run:
name: Yarn Install
command: yarn install --cache-folder ~/.cache/yarn
- save_cache:
key: someproj-yarn-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
- run:
name: Wait for DB
command: dockerize -wait tcp://localhost:5432 -timeout 1m
- run:
name: Database setup
command: bin/rails db:schema:load --trace
- run:
name: Run rspec
command: bundle exec rspec --profile 10
Github Actionsの設定ファイルは以下です。
.github/workflows/rspec.yml
name: RSpec
on:
pull_request:
branches:
- master
jobs:
execute:
runs-on: ubuntu-latest
env:
DOCKER_DB_HOST: db
services:
db:
image: circleci/postgres:9.6.2-alpine
env:
POSTGRES_USER: root
POSTGRES_DB: someproj_test
container:
image: circleci/ruby:2.6.5-node-browsers
options: --user root
steps:
- uses: actions/checkout@v2
- name: Cache node modules
uses: actions/cache@v1
id: cache_rbenv
with:
path: /home/runner/.cache/yarn
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
{{ runner.os }}-yarn-
- name: Yarn install
run: |
yarn install --cache-folder ~/.cache/yarn
- name: Build and setup
run: |
gem install bundler --no-document -v $(grep "BUNDLED WITH" -1 Gemfile.lock | tail -n 1)
bundle config build.nokogiri --use-system-libraries
bundle install --jobs 4 --retry 3
- name: Database setup
run: |
bundle exec rails db:create
bundle exec rails db:schema:load --trace
- name: Exec rspec
run: bundle exec rspec
config/database.ymlが以下のようになっているので、DOCKER_DB_HOST: dbを設定しています。
config/database.yml
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
host: <%= ENV.fetch("DOCKER_DB_HOST") { "localhost" } %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
test:
<<: *default
database: someproj_test
ポイントはCircle CIのイメージを使っていることとrootユーザーじゃないとパーミッションエラーになるので、options: --user rootをしている部分だと思います。
他はそんなに難しいことはしてないと思います。
参考にしていたければありがたいです。