# Matrix builds and test sharding

A slow test suite finishes sooner when it is split across several runners. On Kajiya every shard is its own VM, billed per started minute, so sharding mostly buys time, not extra cost. This guide shows how to shard, and how to size the fan-out to your limits.

## Shard a test suite

Run the same job N times with a matrix, and give each copy its shard number:

```yaml
jobs:
  test:
    runs-on: kajiya-4
    strategy:
      fail-fast: false
      max-parallel: 4
      matrix:
        shard: [1, 2, 3, 4]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npx playwright test --shard=${{ matrix.shard }}/4
```

Most test runners can split a suite themselves:

| Runner | Flag |
|---|---|
| Playwright | `--shard=${{ matrix.shard }}/4` |
| Jest | `--shard=${{ matrix.shard }}/4` |
| Vitest | `--shard=${{ matrix.shard }}/4` |
| Bun | `bun test --shard=${{ matrix.shard }}/4` |
| Go | `go test` a different package list per shard, for example from `go list ./...` split with `split -n r/${{ matrix.shard }}/4` |

## Size the fan-out to your limits

Jobs beyond your concurrency limit do not fail; they wait on GitHub (reason `limit`) and start as others finish. They still make the whole run take longer, so match the matrix to what can run at once:

- `concurrency.limit_vcpu` from `GET /api/v1/account` divided by the vCPUs per job is how many shards can run together: with 32 vCPUs, eight `kajiya-4` shards; with 4 (a new account), one.
- `strategy.max-parallel` caps how many matrix jobs GitHub starts at once, so one workflow cannot take the whole limit from the others.
- A [per-repository cap](/docs/queue-and-limits#per-repository-caps) does the same for a whole repository.

## What sharding costs

Each shard pays for its own boot (about 40 seconds) and its last started minute. Four shards of a 20-minute `kajiya-4` suite cost about as much as one 20-minute run, plus up to a few minutes of boot and rounding, and finish four times sooner. For a suite that already takes two minutes, sharding adds overhead without saving much time.

| Plan | Wall time | Billed vCPU-minutes |
|---|---|---|
| 1 × `kajiya-4`, 20 minutes | 20 min | 80 |
| 4 × `kajiya-4`, 5 min 10 s each | about 5 min | 4 × 6 × 4 = 96 |
| 2 × `kajiya-8`, 5 min 10 s each (if the suite uses 8 cores) | about 5 min | 2 × 6 × 8 = 96 |

## Jobs longer than 58 minutes

Kajiya stops a job after 58 minutes. Shard anything that comes close, so each shard stays well under the limit even on a slow day.

## Collect the results

Each shard runs on its own VM, so pass results between jobs through artifacts, not the file system:

```yaml
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: results-${{ matrix.shard }}
          path: test-results/

  report:
    needs: test
    if: always()
    runs-on: kajiya-2
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: results-*
          merge-multiple: true
          path: test-results/
      - run: ls test-results/
```
