CodeBlanks Logo Icon CodeBlanks

matrix

Learn multi-version testing with the matrix strategy.

What is the matrix strategy?

In GitHub Actions, strategy.matrix allows you to run the same job across multiple variable combinations in parallel. For example, instead of defining separate jobs to test on Node.js 18 and 20, you define a single matrix. GitHub Actions automatically spins up an independent virtual runner for each version.

strategy:
  matrix:
    node-version: [18, 20, 22]

This lets you test multi-platform and multi-version support with minimal code duplication.

Multi-dimensional matrix and fail-fast

When multiple variables are defined in a matrix, all combinations are executed via cartesian product. For example, specifying 2 operating systems and 3 language versions creates 6 parallel jobs. Setting fail-fast: false ensures other matrix jobs continue running even if one fails.

strategy:
  fail-fast: false
  matrix:
    os: [ubuntu-latest, windows-latest]
    node: [18, 20]

This allows you to see test results across all platforms even if one fails.

Practice Create matrix configuration

Add the matrix: definition with node-version: [18, 20] under the strategy block in the workflow.

Starter code

terminal
cat << 'EOF' > .github/workflows/ci.yml
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      # matris ayarini buraya ekle
EOF

cat .github/workflows/ci.yml

Sample solution

terminal
cat << 'EOF' > .github/workflows/ci.yml
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20]
EOF

cat .github/workflows/ci.yml

Expected output

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the primary purpose of using strategy.matrix in GitHub Actions workflows?

  • Running the same job in parallel across different versions or operating systems Correct answer
  • Summarizing the entire commit history of the repo in one line
  • Encrypting secret API keys to store them in the repository
  • Automatically resolving branching merge conflicts

Explanation: matrix executes the job in parallel and independently for each item in the defined variable lists.

2 Define matrix strategy

Question
    strategy:
      ____:
        node: [18, 20]
Answer
    strategy:
      matrix:
        node: [18, 20]

Explanation: Under the strategy block, the matrix key is used to specify multi-version combinations.

3 Matrix concepts

Term Meaning
strategy.matrix Multi-version and platform matrix
fail-fast: false Keeps other jobs running on failure
Kartezyen çarpım Pairs all matrix variables together
matrix.<degisken> Reads matrix value inside a step

All lessons in Advanced