Run rails tests with github actions matrix strategy

You can easily speedup tests runs by using matrix strategy with github actions.

Create a script called for example ci_tests which generates for example the following JSON output:

{
  "include": [
    {
      "name": "System tests",
      "files": "test/controllers/home_controller_test.rb test/controllers/onboarding_controller_test.rb"
    },
    {
      "name": "Model tests",
      "files": "test/models/user_test.rb test/models/post_test.rb"
    }
  ]
}


Create a github action workflow:

name: CI

on: [push]

jobs:
 configure:
  runs-on: ubuntu-latest
  outputs:
   matrix: ${{ steps.set-matrix.outputs.matrix }}
  steps:
   - name: Checkout to repository
    uses: actions/checkout@v4

   - name: Set matrix data
    id: set-matrix
    run: echo "matrix=$(jq -c . <<< $(./ci_tests))" >> $GITHUB_OUTPUT

  tests:
    runs-on: ubuntu-latest
    needs: configure
    strategy:
      matrix: ${{ fromJson(needs.configure.outputs.matrix) }}
    name: ${{ matrix.name }}

    steps:
      - uses: actions/checkout@v4
      ...
      - name: Run tests
        env:
          RAILS_ENV: test
        run: |
          ./bin/rails test ${{matrix.files}}

Pierre FILSTROFF

Senior Software Engineer - IC - Ruby on Rails/Hotwire - Android/iOS - DevOPS