Skip to content

Automatic test coverage report on PR #113

Automatic test coverage report on PR

Automatic test coverage report on PR #113

name: Test .NET w/ Postgres
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
build-and-test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret123
POSTGRES_DB: tdsdb
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U admin -d tdsdb"
--health-interval=10s
--health-timeout=5s
--health-retries=10
env:
CONFIGURATION: Debug
EF_PROJECT: exercise.wwwapi
STARTUP_PROJECT: exercise.wwwapi
ASPNETCORE_ENVIRONMENT: Debug
ConnectionStrings__LocalDatabase: Host=localhost;Port=5432;Database=tdsdb;Username=admin;Password=secret123
steps:
- uses: actions/checkout@v3
- name: Setup .NET (net9.0)
uses: actions/setup-dotnet@v3
with:
dotnet-version: "9.0.x"
- name: Ensure dotnet-ef is available
run: |
dotnet tool update --global dotnet-ef || dotnet tool install --global dotnet-ef
echo "$HOME/.dotnet/tools" >> $GITHUB_PATH
- name: Restore
run: dotnet restore $STARTUP_PROJECT
- name: Wait for Postgres to be healthy
run: |
for i in {1..30}; do
if docker run --rm --network host postgres:16 pg_isready -h localhost -p 5432 -U admin -d tdsdb; then
echo "Postgres is ready"
exit 0
fi
echo "Waiting for Postgres..."
sleep 2
done
echo "Postgres did not become ready in time" >&2
exit 1
- name: Generate EF Core migration (unique per run)
run: |
MIG_NAME="ci_${{ github.run_id }}_$(date +%Y%m%d%H%M%S)"
echo "Creating migration: $MIG_NAME"
dotnet ef migrations add "$MIG_NAME" \
--project "$EF_PROJECT" \
--startup-project "$STARTUP_PROJECT" \
--output-dir Migrations \
--configuration $CONFIGURATION
- name: Apply EF Core migrations
run: |
dotnet ef database update \
--project "$EF_PROJECT" \
--startup-project "$STARTUP_PROJECT" \
--configuration $CONFIGURATION \
--connection "$ConnectionStrings__LocalDatabase"
- name: Run tests
run: dotnet test -c $CONFIGURATION --settings coverlet.runsettings
- name: Code Coverage Report
uses: irongut/[email protected]
with:
filename: '**/TestResults/**/coverage.cobertura.xml'
badge: true
fail_below_min: false
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: '60 80'
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
recreate: true
path: code-coverage-results.md