-
-
Save hotsen/a35ffb803ee04f217968a5c7a7f36c8b to your computer and use it in GitHub Desktop.
GitHub Action + Rails test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .github/workflows/test.yml | |
name: test | |
on: [push] | |
env: | |
RUBY_VERSION: 2.7.2 | |
NODE_VERSION: 14.15.5 | |
RAILS_ENV: test | |
TZ: '/usr/share/zoneinfo/Asia/Tokyo' | |
jobs: | |
prepare: | |
name: prepare test | |
runs-on: ubuntu-latest | |
timeout-minutes: 30 | |
steps: | |
- uses: actions/checkout@v2 | |
- name: setup Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: ${{ env.RUBY_VERSION }} | |
bundler-cache: true | |
# 注釈) Nodeやyarn, キャッシュの設定は都度調整してください | |
- name: setup Node | |
uses: actions/setup-node@v2 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
- name: install yarn | |
run: npm i -g [email protected] | |
- name: Get yarn cache directory path | |
id: yarn-cache-dir-path | |
run: echo "::set-output name=dir::$(yarn cache dir)" | |
- name: Cache yarn cache directory | |
uses: actions/[email protected] | |
with: | |
path: ${{ steps.yarn-cache-dir-path.outputs.dir }} | |
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-yarn- | |
- name: Cache node_modules | |
uses: actions/[email protected] | |
with: | |
path: node_modules | |
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-node-modules- | |
- name: yarn install | |
run: yarn install --frozen-lockfile | |
- name: asset cache | |
uses: actions/[email protected] | |
with: | |
path: | | |
public/assets | |
tmp/cache/assets/sprockets | |
key: asset-cache-${{ runner.os }}-${{ github.ref }}-${{ github.sha }} | |
restore-keys: | | |
asset-cache-${{ runner.os }}-${{ github.ref }}-${{ github.sha }} | |
asset-cache-${{ runner.os }}-${{ github.ref }}- | |
asset-cache-${{ runner.os }}- | |
- name: assets precompile | |
run: bundle exec rake assets:precompile --trace | |
- name: cache bundle.js | |
uses: actions/[email protected] | |
with: | |
path: ./public/packs | |
key: bundle-js-${{ github.sha }} | |
# (補足) Projectによって適切に変更する. webpack buildプロセス。 | |
- name: yarn run release | |
run: yarn run release | |
backend-test: | |
name: ${{ matrix.target }} ${{ matrix.index }} | |
needs: prepare | |
runs-on: ubuntu-latest | |
timeout-minutes: 30 | |
env: | |
CI_NUMBER_OF_NODES: 8 # NOTE: 並列に実行する数. ここを増やしたらmatrix.indexも増やすこと. | |
PARALLEL_TESTS_CONCURRENCY: 2 # paralell_test gemを使用している場合 | |
# 省略... | |
strategy: | |
fail-fast: false | |
matrix: | |
target: [backend] | |
index: [0, 1, 2, 3, 4, 5, 6, 7] # NOTE: 要素数は CI_NUMBER_OF_NODES の個数分にすること | |
services: | |
mysql: | |
image: mysql:5.7 | |
env: | |
# (Projectによって適切に設定する) | |
# MYSQL_ROOT_PASSWORD: xxx | |
# MYSQL_DATABASE: xxx | |
ports: | |
- 3306:3306 | |
options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10 | |
steps: | |
# RDSのMySQL-5.7と同じオプションにする | |
# mysql --ssl-mode=DISABLE --protocol=tcp --host 127.0.0.1 --user=root --password=${DB_PASSWORD} -e "SELECT @@global.sql_mode" | |
# 2021-04-14の結果: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | |
# (補足) Projectによって適切に変更する。GitHub Actionsではserviceの起動時にdocker runの引数を上書きできない為... | |
- name: set MySQL sql_mode | |
run: | | |
mysql --ssl-mode=DISABLE --protocol=tcp --host 127.0.0.1 --user=root --password=${DB_PASSWORD} mysql <<SQL | |
SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION'; | |
SET GLOBAL character_set_server = 'utf8mb4'; | |
SET GLOBAL collation_server = 'utf8mb4_general_ci'; | |
SQL | |
- uses: actions/checkout@v2 | |
- name: setup Ruby | |
uses: ruby/setup-ruby@v1 | |
with: | |
ruby-version: ${{ env.RUBY_VERSION }} | |
bundler-cache: true | |
- name: Database Setup (includes db:seed) | |
env: | |
DB_PORT: ${{ job.services.mysql.ports[3306] }} | |
run: | | |
bundle exec rake parallel:setup[$PARALLEL_TESTS_CONCURRENCY] --trace | |
- name: asset cache | |
uses: actions/[email protected] | |
with: | |
path: | | |
public/assets | |
tmp/cache/assets/sprockets | |
key: asset-cache-${{ runner.os }}-${{ github.ref }}-${{ github.sha }} | |
- name: cache bundle.js | |
uses: actions/[email protected] | |
with: | |
path: ./public/packs | |
key: bundle-js-${{ github.sha }} | |
- name: Run RSpec | |
env: | |
DB_PORT: ${{ job.services.mysql.ports[3306] }} | |
CI_NODE_INDEX: ${{ matrix.index }} | |
# 他, クレデンシャル等のenvを ${{ secrets.HOGE_ENV }} 経由で渡す | |
# 注釈) parallel_rspecを使っていない場合は bundle exec rspec -- $TEST_FILES | |
run: | | |
TEST_FILES="$(ruby spec/spec_splitter.rb --glob='spec/**/*_spec.rb' --node-count=$CI_NUMBER_OF_NODES --node-index=$CI_NODE_INDEX)" | |
bundle exec parallel_rspec -n $PARALLEL_TESTS_CONCURRENCY -- $TEST_FILES |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment