How to use parallel_tests in github actions

1.9k views Asked by At

I'm trying to use parallel_tests in my github action to run my test suite but I was not able to find a proper solution. The official docs has one but it is for gitlab:

https://github.com/grosser/parallel_tests/wiki/Distributed-Parallel-Tests-on-CI-systems

Any help would be appreciated thanks!

3

There are 3 answers

2
anonymous On BEST ANSWER

This was what I used to get it solved. Note: There are some other omitted parts such as the setup for ruby, postgresql, sqlite, etc.

name: "Lint and Test"
jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    services:
      redis:
        image: redis
        ports: ["6379:6379"]
      postgres:
        ports: ["5432:5432"]
    steps:
        //omitted setups
      - name: Setup Parallel Database
        env:
          RAILS_ENV: test
          PGHOST: localhost
          PGUSER: postgres
        run: |
          cp config/database.yml.example config/database.yml
          bundle exec rake parallel:create
          bundle exec rake parallel:rake[db:schema:load] || true
      - name: Build and test with rspec
        env:
          RAILS_ENV: test
          PGHOST: localhost
          PGUSER: postgres
          APP_REDIS_URL: redis://localhost:6379
          MINIMUM_COVERAGE: 80
        run: |
          bundle exec parallel_rspec --verbose spec
0
Markos Fragkakis On

I wrote a blog post about this (link). The currently accepted answer runs all groups in a single runner, which doesn't really scale out. The answer by Benjamin Curtis does scale out, but only sends a single group to each runner, which for my use case didn't really utilize the runner's resources. In the approach I followed, I also use the matrix strategy, but rather send 3 groups to each runner.

This is the relevant snippet:

run_unit_tests_chunk:
   # this is the job for the unit tests (non-integration tests)
   strategy:
     matrix:
       groups: [ "[1,2,3]", "[4,5,6]", "[7,8,9]", "[10,11,12]"]
   uses: ./.github/workflows/ci_unit_tests_chunk.yml
   secrets: inherit
   with:
     groups: ${{ matrix.groups }}
     group_count: 12 # the total number of test groups, must match the groups listed in the matrix.groups
     parallel_processes_count: 3 # the number of parallel processes to run tests in worker, must match the size of the
                                 # inner arrays in the matrix.groups

You can read more on the blog post I wrote about this (link).

0
Benjamin Curtis On

Here's a sample workflow you can drop into .github/workflows/tests.yml:

name: Rails Tests

on: push

env:
  PGHOST: localhost
  PGUSER: postgres
  RAILS_ENV: test

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        # Set N number of parallel jobs you want to run
        # Remember to update ci_node_index below to 0..N-1
        ci_node_total: [6]
        # set N-1 indexes for parallel jobs
        # When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
        ci_node_index: [0, 1, 2, 3, 4, 5]

    services:
      postgres:
        image: postgres:11.5
        ports: ["5432:5432"]
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
      redis:
        image: redis:5
        ports: ["6379:6379"]

    steps:
    - uses: actions/checkout@v1

    - uses: ruby/setup-ruby@v1
      with:
        bundler-cache: true

    - name: Set node version (from .tool-versions)
      run: echo "NODE_VERSION=$(cat .tool-versions | grep nodejs | sed 's/^nodejs //')" >> $GITHUB_ENV

    - uses: actions/setup-node@v2
      with:
        node-version: ${{ env.NODE_VERSION }}

    - uses: bahmutov/npm-install@v1

    - name: Install PostgreSQL client
      run: |
        sudo apt-get -yqq install libpq-dev postgresql-client

    - name: Test Prep
      env:
        CI_NODE_INDEX: ${{ matrix.ci_node_index }}
      run: |
        bundle exec rake parallel:create["1"] parallel:load_schema["1"]

    - name: Run tests
      env:
        RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
        CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
        CI_NODE_INDEX: ${{ matrix.ci_node_index }}
      run : |
        bundle exec parallel_test spec/ -n $CI_NODE_TOTAL --only-group $CI_NODE_INDEX --type rspec