Multiple matrix include configurations

I have a job matrix as so:

strategy:
  matrix:
    service: [a, b, c, d]
    include:
      - service: a
        custom-variables-json: '{"count": 1}'

I want to run ‘service’ “a” with two different ‘custom-variables-json’. e.g.

strategy:
  matrix:
    service: [a, b, c, d]
    include:
      - service: a
        custom-variables-json: '{"count": 1}'
      - service: a
        custom-variables-json: '{"count": 500}'

This does not work, and the job runs with just the last configuration (custom-variables-json: ‘{“count”: 500}’).

Is this possible in another way? I’d like to still use the matrix feature.

1 Like

This looks like some thing that’d be interpreted as add additional values into an existing combination, the service: a one, which would explain why only the last value is used.

I think including custom-variables-json should help to get the additional includes recognized as new combinations:

strategy:
  matrix:
    service: [a, b, c, d]
    custom-variables-json: '{}'
    include:
      - service: a
        custom-variables-json: '{"count": 1}'
      - service: a
        custom-variables-json: '{"count": 500}'
1 Like

If you don’t set any matrix variables, then all the configurations listed under include will run. Even if all the options are identical, a separate job is executed for each entry:

strategy:
  matrix:
    # no other keys than include
    include:
      - service: a
        custom-variables-json: '{"count": 1}'
      - service: a
        custom-variables-json: '{"count": 500}'

      # two jobs with same configuration will be executed
      - service: b
        non-unique: true
      - service: b
        non-unique: true

      # need to explicitly list all configs
      - service: c
      - service: d

I’ve also seen people do the following:

strategy:
  matrix:
    build: [1, 2]
    include:
      - build: 1
        non-unique: true
      - build: 2
        non-unique: true

Last but not least, you can compute a matrix dynamically. You could use a bash script or even actions/github-script to write inline JavaScript code to compute this matrix.

jobs:
  create-matrix:
    name: Create Matrix
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }} 
    steps:
      - name: Set Matrix
        id: set-matrix
        env:
          MATRIX_JSON: '
            { "include": [{
              "non-unique": true
            },{
              "non-unique": true
            }]}'
        run: echo "::set-output name=matrix::$MATRIX_JSON"

  build:
    name: Test Workflow
    needs: create-matrix
    runs-on: ${{ matrix.os }}
    strategy:
      matrix: ${{ fromJSON(needs.create-matrix.outputs.matrix) }}
    steps:
      - name: Matrix config
        env:
          MATRIX: ${{ toJSON(matrix) }}
        run: |
          echo "matrix = $MATRIX"