How to Generate a Coverage Badge in GitHub Actions
A coverage badge keeps the number visible, but you do not need an external service to produce one.
Run tests with coverage, generate an SVG badge from the summary, and commit it back or upload it as an artifact.
Steps
- Run the test suite with coverage enabled to produce a coverage summary.
- Generate a badge SVG from the percentage with a badge action.
- Commit the badge to a branch or upload it as an artifact for the README to reference.
Workflow
.github/workflows/coverage-badge.yml
name: Coverage Badge
on:
push:
branches: [main]
permissions:
contents: write
jobs:
badge:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: npm
- run: npm ci
- run: npm test -- --coverage
- uses: we-cli/coverage-badge-action@mainNotes
- Commit the badge to an orphan branch so it does not churn your main history.
- On Latchkey managed runners coverage jobs run cheaper and self-heal if a runner dies.
Related guides
How to Upload Coverage to Codecov in GitHub ActionsUpload test coverage to Codecov from GitHub Actions with codecov/codecov-action, using a token so coverage re…
How to Merge Coverage from Sharded Jobs in GitHub ActionsCollect per-shard coverage artifacts in GitHub Actions and merge them into one report in a dependent job so a…