← All articles

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}}

Credits

  • Illustration générée par intelligence artificielle
Enjoyed this article? Leave a like.

Comments

Leave a comment

Your comment will appear after moderation.

Your name and email are only used to identify your comment. Your email is never shown publicly. Privacy policy.

Related articles