Robert
Robert

Reputation: 6437

Stop github action matrix case

I want to use a github action matrix for different build types, but there's one case of the matrix that I'm not interested in supporting. How do I stop this case from running but still get the build to marked successfully.

In this particular case I want to build Windows and Ubuntu, 32bit and 64bit but I'm not interested in supporting 32bit on Ubuntu. So my matrix would be:

    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, ubuntu-latest]
        platform: ['x64', 'x86']

My current solution is to stop each action running by adding an if expression:

    - name: Build Native
      if: ${{ ! (matrix.os == 'ubuntu-18.04' && matrix.platform == 'x86') }}

While this works okay, I feel there ought to be a more elegant way of solving this. Can anyone help make my yaml script more beautiful?

Upvotes: 6

Views: 1749

Answers (2)

Lloeki
Lloeki

Reputation: 6713

There are situations where one wants to include or exclude specific matrix coordinates so as not to run some of them, yet (stretching the question a bit) also still want the job to run for a couple of these coordinates, so as to track the evolution of it across commits, while not blocking the whole process.

In that situation, continue-on-error at the job level, combined with matrix include and exclude is very useful:

Prevents a workflow run from failing when a job fails. Set to true to allow a workflow run to pass when this job fails.

This is similar to GitLab CI's allow_failure, although at time of writing GitHub Actions UI only has two states (red failed and green passed) whereas GitLab introduces a third one (orange warning) in that case.

Here is a real-life workflow example:

jobs:
    linux:
        continue-on-error: ${{ matrix.experimental }}
        strategy:
            fail-fast: false
            matrix:
                os:
                    - ubuntu-20.04
                container:
                    - 'ruby:2.0'
                    - 'ruby:2.1'
                    - 'ruby:2.2'
                    - 'ruby:2.3'
                    - 'ruby:2.4'
                    - 'ruby:2.5'
                    - 'ruby:2.6'
                    - 'ruby:2.7'
                    - 'ruby:3.0'
                    - 'ruby:2.1-alpine'
                    - 'ruby:2.2-alpine'
                    - 'ruby:2.3-alpine'
                    - 'ruby:2.4-alpine'
                    - 'ruby:2.5-alpine'
                    - 'ruby:2.6-alpine'
                    - 'ruby:2.7-alpine'
                    - 'jruby:9.2-jdk'
                experimental:
                    - false
                include:
                    - os: ubuntu-20.04
                      container: 'ruby:3.0.0-preview2'
                      experimental: true
                    - os: ubuntu-20.04
                      container: 'ruby:3.0.0-preview2-alpine'
                      experimental: true

Upvotes: 1

DannyB
DannyB

Reputation: 14816

Perhaps the strategy.matrix.exclude directive is suitable?

From the documentation:

You can remove a specific configurations defined in the build matrix using the exclude option. Using exclude removes a job defined by the build matrix.

So in your case, probably something like this:

strategy:
  matrix:
    os: [windows-latest, ubuntu-latest]
    platform: ['x64', 'x86']
    exclude:
      - os: ubuntu-latest
        platform: x86

Upvotes: 8

Related Questions