Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Pilot Lvl 1
Message 1 of 4

How to conditionally include/exclude items in matrix (eg. based on branch)?

I have a matrix like this:

 

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
        node-version: [10]
        dart-version: [stable, dev]
        code-version: [stable, insiders]

I would like to only run the dev-dart and insiders-vscode builds for the master branch, and have only the stable/stable versions run for other branches. I can't find a way to make these values conditional (either here, or by using exclude).

3 Replies
Highlighted
GitHub Partner
Message 2 of 4

Re: How to conditionally include/exclude items in matrix (eg. based on branch)?

Hi @DanTup ,

 

Currently, GitHub Actions does not support to filter breaches for matrix jobs.

However, as a workaround, you can try to copy two workflows, one only for master branch and one for other branches, then set different branch filters on event and configure different values for the matrix parameters based on the branches.

  • In the workflow for master branch, you can configure the branch filters and matrix like the below example:

forMaster.PNG

  • In the workflow for other branches, you can configure the branch filters and matrix like the below example:

forOther.PNG

Highlighted
Pilot Lvl 1
Message 3 of 4

Re: How to conditionally include/exclude items in matrix (eg. based on branch)?

Thanks - that would probably work, but my workflow is quite large and maintaining two of them wouldn't be ideal. Is it possible this may change in future? To do something similar on Travis, you can have a top-level `if` block:

 

if: tag IS blank AND ((branch = master and type = push) OR (env(ONLY_RUN_DART_VERSION) = env(ONLY_RUN_CODE_VERSION)))

And on AppVeyor you can use a `for` block to specify some combinations that only run for a certain branch:

 

for:
  -
    branches:
      only:
        - master
    environment:
      matrix:
        - ONLY_RUN_CODE_VERSION: DEV
          ONLY_RUN_DART_VERSION: STABLE
        - ONLY_RUN_CODE_VERSION: STABLE
          ONLY_RUN_DART_VERSION: DEV
Highlighted
Copilot Lvl 2
Message 4 of 4

Re: How to conditionally include/exclude items in matrix (eg. based on branch)?

I'm looking for something similar - I'm actually trying to only run the job if the matrix option matches a particular value - unfortunately it seems that's not an option at the job level, so I'd have to do it at the step level, which defeats the purpose