</>

Technology

GitHub Actions

Difficulty

Intermediate

Interview Question

What is the matrix strategy in GitHub Actions? How do you run cross-browser or cross-version tests?

Answer

Matrix Strategy in GitHub Actions

The matrix strategy runs one job multiple times with different variable combinations — all in parallel.

Basic Matrix — Multiple Values

YAML
jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        browser: [chrome, firefox, edge]

    steps:
      - uses: actions/checkout@v4
      - name: Run tests on ${{ matrix.browser }}
        run: mvn test -Dbrowser=${{ matrix.browser }}

# Creates 3 parallel jobs:
# test (chrome), test (firefox), test (edge)

Multi-Dimensional Matrix — Cross-Browser × OS

YAML
jobs:
  cross-platform-tests:
    strategy:
      matrix:
        os:      [ubuntu-latest, windows-latest]
        browser: [chrome, firefox]
        java:    ['17', '21']
      # 2 OS × 2 browsers × 2 Java = 8 parallel jobs!

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v4
        with:
          java-version: ${{ matrix.java }}
          distribution: 'temurin'

      - name: Run tests
        run: mvn test -Dbrowser=${{ matrix.browser }}

      - name: Upload results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: results-${{ matrix.os }}-${{ matrix.browser }}-java${{ matrix.java }}
          path: target/surefire-reports/

include — Add Extra Variables to Specific Combinations

YAML
strategy:
  matrix:
    browser: [chrome, firefox]
    include:
      - browser: chrome
        headless: true
        driver-version: '120'
      - browser: firefox
        headless: true
        driver-version: '121'
      - browser: safari    # extra combination not in base list
        os: macos-latest
        headless: false

exclude — Remove Specific Combinations

YAML
strategy:
  matrix:
    os:      [ubuntu-latest, windows-latest, macos-latest]
    browser: [chrome, firefox, edge]
    exclude:
      - os: ubuntu-latest
        browser: edge     # Edge not supported on Linux
      - os: macos-latest
        browser: edge     # Edge has issues on macOS

fail-fast and max-parallel

YAML
strategy:
  fail-fast: false      # don't cancel other matrix jobs if one fails
  max-parallel: 3       # run max 3 jobs at a time (limit concurrency)
  matrix:
    browser: [chrome, firefox, edge, safari]

Matrix with Environment Targets

YAML
jobs:
  regression:
    strategy:
      matrix:
        environment: [staging, qa]
        suite: [smoke, regression]

    environment: ${{ matrix.environment }}

    steps:
      - run: mvn test -Denv=${{ matrix.environment }} -Dsuite=${{ matrix.suite }}

Real-World: Java Multi-Version Testing

YAML
jobs:
  test-java-compatibility:
    strategy:
      matrix:
        java: ['11', '17', '21']

    runs-on: ubuntu-latest
    name: Test on Java ${{ matrix.java }}

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-java@v4
        with:
          java-version: ${{ matrix.java }}
          distribution: 'temurin'

      - run: mvn test
        name: Run tests on Java ${{ matrix.java }}

Follow AutomateQA

Related Topics