When code coverage is bellow comment pr before build break #386
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build | |
# Controls when the workflow will run | |
on: | |
# Triggers the workflow on push or pull request events but only for the main branch | |
push: | |
branches: [ main ] | |
pull_request: | |
branches: [ main ] | |
# Allows to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
# Workflow jobs | |
jobs: | |
Build: | |
runs-on: ubuntu-latest | |
steps: | |
# Checkout | |
- name: Git checkout | |
uses: actions/checkout@v2 | |
# Needs .NET | |
- name: Setup .NET | |
uses: actions/setup-dotnet@v1 | |
with: | |
dotnet-version: 8.0.x | |
# Run dotnet restore | |
- name: Run dotnet restore | |
run: dotnet restore Cmf.CustomerPortal.Sdk.sln | |
# Run dotnet build | |
- name: Run dotnet build | |
run: dotnet build Cmf.CustomerPortal.Sdk.sln --no-restore --configuration Release | |
# Run tests with coverage | |
- name: Run Unit Tests with Coverage | |
run: | | |
dotnet test Cmf.CustomerPortal.Sdk.sln --no-build --no-restore --configuration Release --collect:"XPlat Code Coverage" --logger trx --results-directory TestResults | |
- name: Merge Coverage Reports | |
run: | | |
dotnet tool install --global dotnet-reportgenerator-globaltool | |
reportgenerator -reports:TestResults/**/coverage.cobertura.xml -targetdir:TestResults/MergedCoverage -reporttypes:Cobertura | |
- name: Code Coverage Report | |
uses: irongut/[email protected] | |
with: | |
filename: TestResults/MergedCoverage/Cobertura.xml | |
badge: true | |
fail_below_min: false | |
format: markdown | |
hide_branch_rate: false | |
hide_complexity: true | |
indicators: true | |
output: both | |
thresholds: '5 8' | |
- 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 | |
- name: Write to Job Summary | |
run: cat code-coverage-results.md >> $GITHUB_STEP_SUMMARY | |
- name: Fail Build if Coverage < 10% | |
run: | | |
COVERAGE=$(xmllint --xpath "string(//coverage/@line-rate)" TestResults/MergedCoverage/Cobertura.xml) | |
COVERAGE_PERCENT=$(echo "$COVERAGE * 100" | bc) | |
echo "Detected Coverage: $COVERAGE_PERCENT%" | |
if (( $(echo "$COVERAGE_PERCENT < 10" | bc -l) )); then | |
echo "❌ Code coverage is below 10%, failing the build." | |
exit 1 | |
fi | |
# Run build | |
- name: Build Windows | |
if: github.ref_name == 'main' | |
env: | |
DOTNET_CLI_TELEMETRY_OPTOUT: true | |
run: dotnet publish --configuration Release --self-contained --runtime win-x64 Cmf.CustomerPortal.Sdk.sln | |
- name: Build Linux | |
if: github.ref_name == 'main' | |
env: | |
DOTNET_CLI_TELEMETRY_OPTOUT: true | |
run: dotnet publish --configuration Release --self-contained --runtime linux-x64 Cmf.CustomerPortal.Sdk.sln | |
# Read version | |
- name: Read tool version | |
if: github.ref_name == 'main' | |
uses: QwerMike/xpath-action@v1 | |
id: getver | |
with: | |
filename: src/Version.props | |
expression: '/Project/PropertyGroup/Version/text()' | |
# Archive | |
- name: Archive Console Win64 | |
if: github.ref_name == 'main' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: Cmf.CustomerPortal.Sdk.Console-${{ steps.getver.outputs.result }}.win-x64 | |
path: src/Console/bin/Release/net8.0/win-x64/publish/ | |
- name: Archive Console Linux64 | |
if: github.ref_name == 'main' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: Cmf.CustomerPortal.Sdk.Console-${{ steps.getver.outputs.result }}.linux-x64 | |
path: src/Console/bin/Release/net8.0/linux-x64/publish/ | |
- name: Archive PowerShell | |
if: github.ref_name == 'main' | |
uses: actions/upload-artifact@v4 | |
with: | |
name: Cmf.CustomerPortal.Sdk.PowerShell-${{ steps.getver.outputs.result }} | |
path: src/Powershell/bin/Release/net8.0/linux-x64/publish/ |