Skip to content

Direct to main

Direct to main #140

Workflow file for this run

name: "3. Code Coverage"
permissions:
contents: write
pull-requests: write
on:
push:
branches:
- "main"
- "feature/*"
paths-ignore:
- "**/README.md"
- "coverage-badge.svg" # prevent loops when the bot updates the badge
pull_request:
branches:
- "main"
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
fetch-depth: 0
persist-credentials: false # we'll push with the workflow token
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Install ReportGenerator tool
run: dotnet tool install --global dotnet-reportgenerator-globaltool
- name: Restore dependencies
run: dotnet restore
- name: Build the solution
run: dotnet build --no-restore
- name: Run tests and collect code coverage
run: dotnet test --no-build --collect:"XPlat Code Coverage"
- name: Generate code coverage report
run: reportgenerator -reports:"**/coverage.cobertura.xml" -targetdir:"coverage" -reporttypes:"HtmlInline_AzurePipelines;Badges"
- name: Upload coverage report zip
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: coverage
- name: Upload coverage badge file
uses: actions/upload-artifact@v4
with:
name: code-coverage-badge
path: coverage/badge_linecoverage.svg
# === Create/update PR with the badge (only when running on main) ===
- name: Prepare badge (only on main)
if: github.ref == 'refs/heads/main'
run: |
cp coverage/badge_linecoverage.svg ./coverage-badge.svg
- name: Commit badge to a branch (only on main)
if: github.ref == 'refs/heads/main'
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Create/update a dedicated branch
BRANCH="ci/update-coverage-badge"
git checkout -B "$BRANCH"
git add coverage-badge.svg
if git diff --cached --quiet; then
echo "No changes to commit"
echo "NO_CHANGES=true" >> $GITHUB_ENV
else
git commit -m "Update coverage badge"
echo "NO_CHANGES=false" >> $GITHUB_ENV
fi
- name: Push branch (only on main and if changes)
if: github.ref == 'refs/heads/main' && env.NO_CHANGES == 'false'
env:
GH_TOKEN: ${{ github.token }}
run: |
BRANCH="ci/update-coverage-badge"
git push --set-upstream "https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}" "$BRANCH"
- name: Open / update PR (only on main and if changes)
if: github.ref == 'refs/heads/main' && env.NO_CHANGES == 'false'
uses: peter-evans/create-pull-request@v6
with:
branch: ci/update-coverage-badge
title: "Update coverage badge"
commit-message: "Update coverage badge"
body: "Automated update of coverage-badge.svg"
base: main
labels: ci, automated