Skip to content

Instantly share code, notes, and snippets.

@nhattan
Last active January 9, 2025 05:35
Show Gist options
  • Save nhattan/beb59cb78f9b6f208e73ec17884092d0 to your computer and use it in GitHub Desktop.
Save nhattan/beb59cb78f9b6f208e73ec17884092d0 to your computer and use it in GitHub Desktop.
Rspec, parallel_tests on Github Action
# Run RSpec in parallel on GitHub Actions with even test group runtimes
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
group: [1, 2]
name: test group ${{ matrix.group }}/2
services:
postgres:
image: postgres:17
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true
- name: "Runtime cache: restore"
uses: actions/cache/restore@v4
id: cache-restore
with:
key: runtime-cache-all
path: tmp/parallel_runtime_rspec.log
- name: Run tests
env:
RAILS_ENV: test
DATABASE_URL: postgres://postgres:postgres@localhost:5432
run: |
bundle exec rake parallel:setup
bundle exec parallel_test -t rspec -n 2 --group-by runtime --only-group ${{ matrix.group }} --verbose
# change to `--group-by filesize` when this runtime is missing or bad
- name: "Runtime cache: clear previous chunk cache"
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
gh extension install actions/gh-actions-cache
gh actions-cache delete runtime-cache-${{ matrix.group }} --confirm
continue-on-error: true # do not fail in case the key was deleted by a parallel run
- name: "Runtime cache: prepare chunk"
shell: bash
run: |
mkdir -p tmp/parallel_runtime_rspec
mv tmp/parallel_runtime_rspec.log tmp/parallel_runtime_rspec/${{ matrix.group }}.log
- name: "Runtime cache: store chunk"
uses: actions/cache/save@v4
with:
key: runtime-cache-${{ matrix.group }}
path: tmp/parallel_runtime_rspec
store-runtime:
runs-on: ubuntu-latest
needs: test
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: "Runtime cache: load chunk 1"
uses: actions/cache/restore@v4
with:
key: runtime-cache-1
path: tmp/parallel_runtime_rspec
- name: "Runtime cache: load chunk 2"
uses: actions/cache/restore@v4
with:
key: runtime-cache-2
path: tmp/parallel_runtime_rspec
- name: "Runtime cache: combine chunks"
run: |
cat tmp/parallel_runtime_rspec/* > tmp/parallel_runtime_rspec.log
cat tmp/parallel_runtime_rspec.log
- name: "Runtime cache: clear"
env:
GH_TOKEN: ${{ github.token }}
run: |
gh extension install actions/gh-actions-cache
gh actions-cache delete runtime-cache-all --confirm
continue-on-error: true # do not fail in case the key was deleted by a parallel run
- name: "Runtime cache: store"
uses: actions/cache/save@v4
with:
key: runtime-cache-all
path: tmp/parallel_runtime_rspec.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment