# Matrix builds and branch protection in GitHub Actions

> A matrix build runs one job definition across many combinations such as Node versions and operating systems, and branch protection makes chosen checks mandatory before a merge.

URL: https://levelrail.com/learn/ci-cd/matrix-builds-and-branch-protection/
Updated: 2026-11-27

A matrix runs the same job against several combinations, so you learn that your code works on Node 20 and 22, on Linux and Windows, without copying the job. Branch protection then turns those results into a rule: no merge to main until the checks pass and someone reviews the change. This lesson covers both, plus the name-matching trap that blocks merges. Start from the workflow in [Run tests on every push and pull request](https://levelrail.com/learn/ci-cd/run-tests-on-every-push-and-pull-request/)).

## How it works

`strategy.matrix` lists variables and values. GitHub generates one job for each combination and runs them in parallel on separate runners. Inside the job you read the current values with the `matrix` context. Each generated job reports its own status check, named after the job and its values, such as `test (20, ubuntu-latest)`. Branch protection and rulesets select checks by those names. If the names and the rule drift apart, the check never reports and the merge stays blocked.

## Step 1: Add a matrix to the job

```yaml
name: test

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        node: [20, 22]
        os: [ubuntu-latest, windows-latest]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm test
```

This creates four jobs: two Node versions on two operating systems. Choose versions you actually support. Every extra axis multiplies the job count, and each job uses runner time.

## Step 2: Tune with fail-fast, include and exclude

`fail-fast` defaults to true, which cancels the other combinations when one fails. That saves time, but you only see the first failure. Set it to `false` when you want the full picture, which is usually right for a test matrix.

`exclude` removes a combination:

```yaml
strategy:
  matrix:
    node: [20, 22]
    os: [ubuntu-latest, windows-latest]
    exclude:
      - node: 20
        os: windows-latest
```

`include` adds a combination or extra variables to one:

```yaml
strategy:
  matrix:
    node: [20, 22]
    include:
      - node: 22
        experimental: true
```

Use `continue-on-error: ${{ matrix.experimental == true }}` on the job if an experimental combination may fail without blocking. Keep the matrix small enough that you read its results.

## Step 3: Create a branch protection rule or ruleset

In the repository, open Settings, then Branches (branch protection rules) or Rules (rulesets). Rulesets are the newer, more flexible option and can cover several branches. For `main`, enable:

- **Require a pull request before merging**, with at least one approving review.
- **Require status checks to pass before merging**, then pick the checks.
- **Require branches to be up to date before merging**, if you want checks against the latest main.
- **Block force pushes.** Force pushes rewrite shared history.
- **Restrict deletions**, so the branch cannot be removed.

Be careful with the setting that includes administrators. If you enforce rules for yourself too, you cannot bypass them in an emergency, so decide on purpose. For a solo repository, requiring checks still protects you from your own bad pushes.

## Step 4: Match the required check names

Required checks are matched by name, not by workflow file. Open a recent pull request and look at the Checks list. For the matrix above the names look like `test (20, ubuntu-latest)`. Select each one you want required. A check only appears in the picker after it has run at least once recently, so run the workflow first.

Two common traps:

- If you rename the job or change matrix values, the names change and the old required names become pending forever. Update the rule.
- If you want one stable name for the whole matrix, add a final job that depends on the matrix and require that instead:

```yaml
  all-tests:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - run: echo "all test jobs passed"
```

Require `all-tests`. If any matrix job fails, `needs` makes this job skip, and a skipped required check blocks the merge in most configurations. Test that behaviour on a throwaway branch before relying on it.

## Check it worked

Open a pull request. You should see one check per matrix combination, all green, and the merge button enabled only after the reviews and checks pass. Then break a test on a scratch branch, open a PR, and confirm the merge button stays disabled. Finally, try to force push to main from your machine, on a test repository if possible, and confirm the push is rejected with a message about protected branch rules. Never test force pushes on a branch others rely on.

## Edge cases and gotchas

- **Renamed jobs block merges.** A required check that never runs stays pending. Fix the rule, not the code.
- **Matrix size.** Four values on three axes is already dozens of jobs. Keep what you can explain.
- **Windows shells.** `run` steps default to PowerShell on Windows runners. Use `shell: bash` for portable scripts.
- **Path separators and line endings.** Tests that pass on Linux can fail on Windows because of paths or CRLF. That is the kind of bug the matrix finds.
- **Admin bypass.** If admins can bypass rules, a rushed fix skips the checks. Decide whether you want that.
- **Stale approvals.** Enable dismissing stale approvals so new commits need a fresh review.

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Required check is `Expected - Waiting for status to be reported` | No job with that name ran | Run the workflow, then fix the required name |
| `Required status check "test" is expected` after a rename | Old name still in the rule | Update the required checks list |
| Merge button disabled with all checks green | Missing review or branch not up to date | Open the merge box for the exact reason |
| `remote: error: GH006: Protected branch update failed` | Push straight to a protected branch | Push to a branch and open a pull request |
| Matrix job named `test` only shows one run | Matrix keys not used in the job | Use `${{ matrix.node }}` in the steps |
| Windows job fails on a shell script | Default shell is PowerShell | Add `shell: bash` |
| One failure cancels everything | `fail-fast` is true | Set `fail-fast: false` |

## Quick reference

```yaml
strategy:
  fail-fast: false
  matrix:
    node: [20, 22]
    os: [ubuntu-latest, windows-latest]
    exclude:
      - node: 20
        os: windows-latest
runs-on: ${{ matrix.os }}
```

```text
main rules: require PR + review, require named checks,
            block force pushes, restrict deletions
```

## Related lessons

- [Run tests on every push and pull request](https://levelrail.com/learn/ci-cd/run-tests-on-every-push-and-pull-request/)): the single-job version.
- [Git branches and merging](https://levelrail.com/learn/git/git-branches-and-merging/)): the branches these rules protect.
- [Git workflow that triggers a deploy](https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/)): how protected main fits a release flow.
- [Undo mistakes in Git](https://levelrail.com/learn/git/undo-mistakes-in-git/)): what to do instead of force pushing to main.
- [GitHub Actions workflow anatomy](https://levelrail.com/learn/ci-cd/github-actions-workflow-anatomy/)): the job and strategy keys.

## Next

Continue with [Version and tag releases](https://levelrail.com/learn/ci-cd/version-and-tag-releases/)).

## Frequently asked questions

### What is a matrix build in GitHub Actions?

It is a strategy that creates several copies of one job, one for each combination of values you list, such as Node 18, 20 and 22 on Linux and Windows. Each combination runs as its own job.

### What does fail-fast do in a matrix?

With fail-fast true, the first failing combination cancels the others still running. Setting it to false lets every combination finish so you see all failures at once.

### How do I require status checks before merging?

In Settings, add a branch protection rule or a ruleset for your main branch, enable require status checks to pass, and select the checks by name. Matrix jobs have one check per combination.

### Why does my required check stay pending forever?

The name in the rule does not match any job that runs. After changing a matrix, the generated job names change, so update the required list.


## Sources

- [GitHub Docs, running variations of jobs in a workflow](https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs)
- [GitHub Docs, about protected branches](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches)
- [GitHub Docs, about rulesets](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/about-rulesets)
