Skip to content
Latchkey

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@main

Notes

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →