Run rails tests with github actions matrix strategy

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}}
0
Did you enjoy this article? Give it a like.

Comments (0)

Leave a comment

Your comment will appear after review.

Related articles

My Automated Tech News Digest with Claude Cowork Schedules

My Automated Tech News Digest with Claude Cowork Schedules

There are tasks we keep pushing back. Not because they're hard, but because they're repetitive and time-consuming. Daily tech news is one of them: checking Hacker News, scrolling through TLDR, brow...