create-matrix-action

This action creates a non-square matrix from parsing a matrix configuration declared in the workflow file.

I wrote this action for my python/django testing needs, but it is very flexible and scalable, so it can be used in many other contexts.

Matrix configuration

Example:

python-version {2.7}, django-version {1.7,1.8,1.9,1.10,1.11}, database {sqlite,mysql,postgres}
python-version {3.6}, django-version {1.8,1.9,1.10,1.11,2.0,2.1,2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
python-version {3.7}, django-version {2.0,2.1,2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
python-version {3.8}, django-version {2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
python-version {3.9}, django-version {2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
python-version {3.10}, django-version {3.2,4.0}, database {sqlite,mysql,postgres}

Note: In the above example, python-version, django-version and database are just the matrix variable names that we can access in the following step, so it is possible to name these variables as needed.

Workflow

Example:

# ...

jobs:

  create_matrix:
    
    runs-on: ubuntu-latest  
    
    steps:
      
      - name: Checkout code
        uses: actions/checkout@v2
      
      - name: Set matrix
        id: set_matrix
        uses: fabiocaccamo/create-matrix-action@v2
        with:
          matrix: |
            python-version {2.7}, django-version {1.7,1.8,1.9,1.10,1.11}, database {sqlite,mysql,postgres}
            python-version {3.6}, django-version {1.8,1.9,1.10,1.11,2.0,2.1,2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
            python-version {3.7}, django-version {2.0,2.1,2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
            python-version {3.8}, django-version {2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
            python-version {3.9}, django-version {2.2,3.0,3.1,3.2}, database {sqlite,mysql,postgres}
            python-version {3.10}, django-version {3.2,4.0}, database {sqlite,mysql,postgres}
          
    outputs:
      matrix: ${{ steps.set_matrix.outputs.matrix }}
        
  test:
    
    needs: create_matrix
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix: 
        include: ${{fromJson(needs.create_matrix.outputs.matrix)}}

    # ...

    steps:
      
      # ...
      
      - name: Debug matrix
        run: |
          echo "Python ${{ matrix.python-version }} + Django ${{ matrix.django-version }} + Database ${{ matrix.database }}"

Check the full test-action.yaml workflow file.


License

Released under MIT License.

GitHub

View Github