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 Likes

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

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
6 Likes

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

1 Like

I’m looking for something similar as well: out of my 4 target configurations Ubuntu, MacOS, Android and iOS I would like to specify that only Ubuntu configuration should be ran on regular daily check-ins into say master (to save me precious minutes), while all 4 configurations should only be ran when I create a version tag (since I also need binaries released for each of the versions). The condition for inclusion of a specific configuration might become more complicated later, but this should be a useful enough use case scenario for you to consider.

Here is one thing I tried. In your post from October 1, 2019 “GitHub Actions - New Workflow Syntax Features” you announce support for if: at the job level. So one natural thing I tried was:

name: CI

on: [push]

jobs:
  build:
    strategy:
      matrix:
        os: [macos-latest,ubuntu-latest]
    runs-on: ${{ matrix.os }}
    if: matrix.os == 'ubuntu-latest' # This line fails with error
    steps:

but the conditional line gave me error: 

Invalid workflow file

The workflow is not valid. .github/workflows/ci.yml (Line: 61, Col: 9):
Unrecognized named-value: 'matrix'. Located at position 1 within expression: matrix.os == 'ubuntu-latest'

 

Why would you disallow usage of matrix variable there?