React on matrix build within Action

As a action creator, I would like to react on the fact whether it’s a matrix build or not.
For example: If it’s a matrix build, I would like to know the OS (matrix.os) and react on it.
Sadly I can’t find anything like this in https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables . Is there nothing like this or is this not documented?

Did this example can resolve your problems ?

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          os: macos-latest
            env:
              - IS_MATRIX: yes
              - MATRIX_OS: macos-latest

          os: ubuntu-latest
            env:
              - IS_MATRIX: yes
- MATRIX_OS: macos-latest

          os: windows-latest
            env:
              - IS_MATRIX: yes
- MATRIX_OS: windows-latest

You can set custom environment var with your matrix OS.

But you can’t see really if you use matrix.os because it don’t really change the way you use your Action.

If you really want to check if Matrix was used in your workflow, you can checkout the repository and parse the workflow file manually to find the informations you need.

@elgohr ,

You can use the matrix context to check whether the currently executing job is a matrix job:
◆ If the currently executing job is a matrix job, the matrix context will return the matrix parameters you configured for the current job.
◆ If the currently executing job isn’t a matrix job, the matrix context will return " null".

For example (also can see here):

jobs:
  job1:
    name: Matrix job
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]
    steps:
      - name: Check matrix
        shell: bash
        run: |
          if [[$MATRIX_CONTEXT != null]]; then
            echo "View the matrix context:"
            echo "$MATRIX_CONTEXT"
            echo "========================================"
            echo "matrix.os = ${{ matrix.os }}"
          else
            echo "This is not a matrix job."
          fi
        env:
          MATRIX_CONTEXT: ${{ toJson(matrix) }}

  job2:
    name: Non-matrix job
    runs-on: ubuntu-latest
    steps:
      - name: Check matrix
        shell: bash
        run: |
          if [[$MATRIX_CONTEXT != null]]; then
            echo "View the matrix context:"
            echo "$MATRIX_CONTEXT"
            echo "========================================"
            echo "matrix.os = ${{ matrix.os }}"
          else
            echo "This is not a matrix job."
          fi
        env:
          MATRIX_CONTEXT: ${{ toJson(matrix) }}
3 Likes

@elgohr ,

How are things going? Is my suggestion helpful to you?

If you have any question about this ticket, feel free to tell me.

1 Like