Matrix cannot be used in jobs level if

Hi

Something like does doesn’t seem to work:

name: "Deploy"
on: deployment

jobs:
  deployment:
    name: "Deploy ${{ matrix.environment }} - ${{ github.ref }}"
    if: matrix.environment == github.event.deployment.environment

    runs-on: "ubuntu-latest"

    strategy:
      matrix:
        environment:
          - stage
          - test
          - production

    steps:
...

This errors:

Your workflow file was invalid: The pipeline is not valid. .github/workflows/deploy.yml (Line: 8, Col: 9): Unrecognized named-value: 'matrix'. Located at position 1 within expression: matrix.environment == github.event.deployment.environment

This seems like it could be supported, since the substitution of matrix.environment in the job name works just fine, and also:

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idif

says:

> You can use any supported context and expression to create a conditional.

Which is clearly not the case.

Thanks

13 Likes

This is not currently supported because the if: is evaluted before the matrix is expanded.  This is useful for cases where you want to skip the whole process but does have the downside of not being able to skip an individual leg.  We are looking at options for how we can enable both scnearios.

7 Likes

Today the condition is evaluated before the matrix is computed. It’s a scenario we’re feature we’re interested to add, but currently not supported.

4 Likes

Hello @ericsciple 

Any news or open issue on this feature ?

2 Likes

Hi @chrispat. Any chance of a status report on this proposal? I have a use-case where I test many versions on three different OSes, but some older versions aren’t supported on Windows. Here’s what I’d like to do:

jobs:
  build:
    strategy:
      matrix:
        os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
        perl: [ '5.30', '5.28', '5.26', '5.24', '5.22', '5.20', '5.18', '5.16', '5.14', '5.12', '5.10', '5.8' ]
    name: Perl ${{ matrix.perl }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    if: "!startsWith(matrix.os, 'windows') || matrix.perl >= 5.14"

Until then, I have to create multiple jobs, like so, which is highly redundant.

If you have a specific set of combinations you want to remove from the matrix the exclude feature might work for you https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-excluding-configurations-from-a-matrix.

1 Like

Oh, I was looking for that, thanks!