name: record coverage
on:
push:
branches: ["main"]
workflow_dispatch:
jobs:
record-coverage:
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update stable
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Run coverage and output JSON
id: run_cov
run: |
set -euxo pipefail
cargo llvm-cov --json --summary-only > coverage-summary.json
cat coverage-summary.json
- name: Determine branch name
id: get_branch
run: |
set -euxo pipefail
if [ -n "${GITHUB_HEAD_REF}" ]; then
branch=${GITHUB_HEAD_REF}
else
branch=${GITHUB_REF#refs/heads/}
fi
echo "Branch is: $branch"
echo "branch=${branch}" >> $GITHUB_OUTPUT
- name: Checkout coverage branch
uses: actions/checkout@v3
with:
ref: coverage
token: ${{ secrets.GITHUB_TOKEN }}
path: coverage_branch
- name: Generate badge from template
run: |
COVERAGE=$(jq -r '.data[0].totals.lines.percent' coverage-summary.json)
COVERAGE_ROUNDED=$(printf "%.0f" "$COVERAGE")
COVERAGE_DISPLAY="${COVERAGE_ROUNDED}%"
echo "Generating badge.svg with coverage: ${COVERAGE_DISPLAY}"
sed "s/%%/${COVERAGE_DISPLAY}/g" coverage_branch/template.svg > coverage_branch/badge.svg
- name: Save coverage JSON and badge for branch
run: |
set -euxo pipefail
branch=${{ steps.get_branch.outputs.branch }}
commit=${GITHUB_SHA}
mkdir -p coverage_branch/coverage-data
rm -f coverage_branch/coverage-data/${branch}-*.json || true
cp coverage-summary.json "coverage_branch/coverage-data/${branch}-${commit}.json"
cd coverage_branch
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
if ! git diff --cached --quiet; then
git commit -m "Update coverage for branch ${branch} at commit ${commit} on $(date -u)"
git push origin coverage
else
echo "No changes to commit."
fi