Sub topic 10
Merging reports, coverage and status from shards
This chapter assumes you can read a CI workflow in YAML and run a test suite from the command line. It also assumes you have already run one shard per job with a matrix, as in the previous chapters. Each shard now produces its own output, so you need one report, one coverage number and one pass/fail signal.
What each shard leaves behind
A shard runs on its own machine. When the job ends, that machine is discarded, and so is everything it wrote. Anything you want to combine must be uploaded as an artifact, which is a file the CI system stores after a job finishes.
The pattern has three steps:
- Each shard writes its results to a file with a shard-specific name.
- Each shard uploads that file as an artifact.
- A final job downloads all the artifacts and merges them.
Uploading with unique names
Two artifacts cannot share a name in the same run, so include the shard number in the name. Use if: ${{ !cancelled() }} so the upload still runs when tests fail. Failed shards are the ones whose reports you need most.
name: e2e
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
shard: [1, 2, 3, 4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install --with-deps
- run: npx playwright test --shard=${{ matrix.shard }}/4 --reporter=blob
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: blob-report-${{ matrix.shard }}
path: blob-report
retention-days: 3
merge-reports:
needs: test
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- uses: actions/download-artifact@v4
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@v4
with:
name: html-report
path: playwright-reportPlaywright's blob reporter writes a format designed for merging. The merge-reports command turns the blobs into a single HTML report that shows every test from every shard. Other runners have similar tools. For JUnit XML, which is a common report format, many CI systems accept several files and combine them for you.
Merging coverage
Coverage is the share of code that tests execute. Each shard sees only the code its own tests touch, so one shard's number is low and misleading. Averaging the four percentages is also wrong. You must merge the raw data first, then compute the percentage.
With Istanbul-based tools, each shard writes JSON coverage files. The merge job collects them into one folder and runs npx nyc merge to combine them. It then runs npx nyc report on the result. Python's coverage.py works the same way: run coverage combine on the data files, then coverage report. Name the data files per shard so they do not overwrite each other.
One status for the whole run
A matrix produces four separate statuses. Branch protection rules, which block merging until checks pass, are awkward to configure against names like test (1), test (2) and so on. They also break when you change the shard count.
Add a final job that depends on everything and reports one result:
all-tests-passed:
needs: [test, merge-reports]
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Fail if any dependency failed or was cancelled
run: |
if [ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "true" ]; then
exit 1
fiRequire only all-tests-passed in branch protection. The always() condition matters. Without it, a failed shard would cause the gate job to be skipped, and a skipped required check can be read as passing.
Common errors
- Artifact name collision. Uploads fail when two shards use the same name. Add the matrix value to the name.
- Missing report from a failed shard. The upload step was skipped because a previous step failed. Add
if: ${{ !cancelled() }}. - Wrong coverage number. The percentages were averaged, or the shards' files overwrote each other.
Exercise
A four-shard run shows 62%, 58%, 61% and 60% coverage on the individual shards. Why can you not report the average, 60.25%, as the project's coverage, and what should you do instead?
Show answer
Each shard runs only a quarter of the tests, and the lines they cover overlap in ways a percentage does not show. Averaging ignores which lines were covered where. Upload each shard's raw coverage data as an artifact, merge the files in a final job (for example with nyc merge or coverage combine), and then generate the percentage from the merged data.
Exercise
Why does the gate job in this chapter use if: ${{ always() }}?
Show answer
If a shard fails, jobs that depend on it are skipped by default. A skipped required check may be treated as passing. With always() the gate job runs anyway, sees the failure in needs, and exits with an error.