QA Interview Hub · Knowledge base · Quizzes · Українська

CI/CD › GitHub Actions for a test framework

Cache, matrix, concurrency and sharding

The npm cache shortens install, a matrix multiplies a job by parameters, sharding splits e2e across those jobs, and concurrency cancels outdated PR runs. The cache is tied to the lock file, shards run with --shard and a separate job merges their blob reports, with unique artifact names. Cancellation is not enabled on main.

CI/CD · GitHub Actions

Four shards gave x2.5, not x4, and every push runs a stale run. Why?

A team split e2e into four shards and hoped for a four-fold speedup. A run dropped from 20 minutes to eight, not five: one shard got the heavy files and install in every job added its own two minutes. On top of that the author pushes three commits in a row, and three full runs compete for runners. This lesson is about the cache, matrix, concurrency and sharding that fix it.

How it works and why. Dependency cache. actions/setup-node with cache: npm caches the global npm cache (~/.npm) under a key derived from package-lock.json. npm ci still runs, but takes packages from disk. Caching node_modules itself is usually not advised: npm ci deletes and recreates it. For custom caches there is actions/cache: the key must change with the lock file, otherwise the cache goes stale.

Playwright browsers live in ~/.cache/ms-playwright (on Linux). The Playwright docs advise against caching them: restoring the cache takes about as long as downloading, and system libraries sit outside that directory and are not restored by the cache. If a cache is still wanted, the key must change with the Playwright version (in the example through the hash of package-lock.json, which records it), and after a cache hit the libraries are installed separately.

- uses: actions/cache@v6
  id: pw-cache
  with:
    path: ~/.cache/ms-playwright
    key: pw-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
- if: steps.pw-cache.outputs.cache-hit != 'true'
  run: npx playwright install --with-deps chromium
- if: steps.pw-cache.outputs.cache-hit == 'true'
  run: npx playwright install-deps chromium

Matrix. One job multiplied by parameters: browsers, Node versions, shard numbers. Each combination is a separate job on a separate runner. By default fail-fast: true: when one combination fails, the rest, running or queued, are cancelled. For a full picture set fail-fast: false. max-parallel caps parallel jobs, and the limit of concurrent jobs depends on the account plan.

Shards and report merge

test 1/4 ... test 4/4 (matrix)in parallel, each gives a blob reportupload-artifact: blob-report-Na unique name per shardmerge-reports: one HTML reportneeds: [test], if: !cancelled()
Figure 1. Without the merge you have four separate reports instead of one.

Example: sharding with a report merge. Each shard calls --shard=i/N and leaves a blob report. For this, set the blob reporter in the Playwright config on CI. Artifact names within a run must be unique, so the shard number is part of the name.

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        shardIndex: [1, 2, 3, 4]
        shardTotal: [4]
    steps:
      # checkout, setup-node, npm ci, playwright install ...
      - run: npx playwright test --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
      - uses: actions/upload-artifact@v7
        if: ${{ !cancelled() }}
        with:
          name: blob-report-${{ matrix.shardIndex }}
          path: blob-report
          retention-days: 1

  merge-reports:
    needs: [test]
    if: ${{ !cancelled() }}
    runs-on: ubuntu-latest
    steps:
      # checkout, setup-node, npm ci ...
      - uses: actions/download-artifact@v8
        with:
          path: all-blob-reports
          pattern: blob-report-*
          merge-multiple: true
      - run: npx playwright merge-reports --reporter html ./all-blob-reports
      - uses: actions/upload-artifact@v7
        with:
          name: html-report
          path: playwright-report
          retention-days: 14

Evenness depends on how Playwright distributes tests: by default sharding goes by file, and with fullyParallel: true by individual test, which evens the load. More in the lesson “Parallelism, workers and sharding”.

Concurrency. So that a new push to a PR cancels a stale run, set a group:

concurrency:
  group: e2e-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

On main cancelling is usually turned off: the result for merged commits should not be lost.

Limits and alternatives. A cache does not speed up a run if restore and save take longer than install: on small projects the gain is minimal. Caches in a repository have a size limit and the oldest are evicted. Shards raise minute costs: four shards of eight minutes each is 32 billable minutes. Large suites have dynamic balancing in third-party services, but to start fullyParallel is enough.

Typical mistakes.

  • The same artifact name in all shards: the run fails on a conflict.
  • Caching node_modules instead of the npm cache.
  • A cache key without a lock file hash.
  • Concurrency on main with cancel-in-progress: results get lost.
  • Blob reports not merged, so the full picture is missing.

Check yourself

Why is install-deps still needed after a cache hit for Playwright browsers?

Answer

The cache restores only the directory with browsers. System libraries (fonts, shared libs) live in the OS and are not kept between runs, so they are installed separately.

Why must a shard's artifact name contain its number?

Answer

Artifact names within a run must be unique: the same name causes a conflict. With a number in the name every shard leaves its own blob report, and they are merged later.

In an interview. Junior: The npm cache shortens install, a matrix runs one job with different parameters in parallel, and concurrency cancels stale PR runs. Middle/Senior: I shard e2e through a matrix with --shard, turn on fullyParallel for evenness and merge blob reports in a separate job. I tie the cache key to the lock file, make artifact names unique, turn fail-fast off where a full picture is needed, and do not enable cancelling on main. I also remember minute cost: shards are not free.

More articles in this chapter