ci: analyze with SonarCloud in GitHub Actions

Automatic Analysis is off in SonarCloud; the new SonarCloud workflow runs
SonarScanner for .NET 12.0 around a real Release build, with Cobertura
coverage from the unit tests and from the smoke test run under
dotnet-coverage, and waits for the quality gate. Fork and Dependabot pull
requests get no SONAR_TOKEN, so their analysis is skipped instead of
failing.

Both tools join .config/dotnet-tools.json. Benchmarks are analysed as test
code, eng/ scripts stay out of coverage, and gate failures on main are
reported like other CI failures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5.5 committed 2026-10-25 07:25:27 +07:00
1 parent 4da9667975
commit 217d3b63d4
9 files changed
+148 -5

No files matched your search

+15 -1
View File
@@ -22,6 +22,20 @@
"dotnet-CycloneDX"
],
"rollForward": false
},
"dotnet-sonarscanner": {
"version": "12.0.0",
"commands": [
"dotnet-sonarscanner"
],
"rollForward": false
},
"dotnet-coverage": {
"version": "18.12.0",
"commands": [
"dotnet-coverage"
],
"rollForward": false
}
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ name: Notify CI Failures
on:
workflow_run:
workflows: [CI, Security, Scorecard]
workflows: [CI, Security, Scorecard, SonarCloud]
types: [completed]
permissions: {}
+114
View File
@@ -0,0 +1,114 @@
name: SonarCloud
# CI-based analysis (Automatic Analysis is turned off in SonarCloud): the C# is analysed during a real build, with
# coverage from the unit tests and the smoke test; scripts and workflows are analysed too. SonarCloud reports the
# quality gate on pull requests itself.
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
permissions: {}
concurrency:
group: sonarcloud-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
env:
DOTNET_NOLOGO: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
AVALONIA_TELEMETRY_OPTOUT: 1
jobs:
# Pull requests from forks and from Dependabot get no repository secrets: skip the analysis instead of failing it.
token:
name: token
runs-on: ubuntu-latest
timeout-minutes: 5
permissions: {}
outputs:
available: ${{ steps.check.outputs.available }}
steps:
- name: Check for SONAR_TOKEN
id: check
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
if [ -n "$SONAR_TOKEN" ]; then
echo "available=true" >> "$GITHUB_OUTPUT"
else
echo "available=false" >> "$GITHUB_OUTPUT"
echo "::notice::SONAR_TOKEN is not available to this run (fork or Dependabot pull request); analysis skipped."
fi
sonarcloud:
name: sonarcloud
needs: token
# Manual runs analyse main only: other branches would create separate SonarCloud branch analyses.
if: needs.token.outputs.available == 'true' && (github.event_name != 'workflow_dispatch' || github.ref == 'refs/heads/main')
runs-on: ubuntu-latest
timeout-minutes: 30
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# Full history: SonarCloud uses blame to tell new code from old.
fetch-depth: 0
persist-credentials: false
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
dotnet-version: 10.0.4xx
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.nuget/packages
key: nuget-${{ runner.os }}-${{ hashFiles('**/*.csproj', 'Directory.Packages.props', 'Directory.Build.props', 'global.json') }}
restore-keys: nuget-${{ runner.os }}-
- name: Install tools
run: dotnet tool restore
# Coverage: the Cobertura reports of the unit tests and of the smoke test. eng/ holds the CI and release scripts
# (Python), for which no coverage is collected, so they stay out of the coverage figure.
# Third-party Roslyn analyzers (Meziantou, ReferenceTrimmer) already fail the CI build when they warn; their
# suggestions are not imported as SonarCloud issues.
- name: Begin analysis
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: |
version=$(sed -n 's:.*<Version>\(.*\)</Version>.*:\1:p' Directory.Build.props)
dotnet sonarscanner begin /k:poli0981_HashTool /o:poli0981 /v:"$version" \
/d:sonar.host.url=https://sonarcloud.io /d:sonar.token="$SONAR_TOKEN" \
/d:sonar.cs.cobertura.reportsPaths="TestResults/**/*.cobertura.xml" \
/d:sonar.coverage.exclusions="eng/**" \
/d:sonar.cs.roslyn.ignoreIssues=true \
/d:sonar.qualitygate.wait=true
# No -warnaserror: CI enforces it, and here it would turn SonarCloud findings into build errors.
- name: Build
run: dotnet build HashTool.slnx -c Release
- name: Test
run: >
dotnet test --solution HashTool.slnx -c Release --no-build
--results-directory TestResults
--coverage --coverage-output-format cobertura
# The real app, headless: every page and language, hashing and verifying a file (see ci.yml).
- name: Smoke test with coverage
env:
HASHTOOL_DATA_DIR: ${{ runner.temp }}/smoke-data
run: >
dotnet coverage collect -f cobertura -o TestResults/smoke.cobertura.xml
"dotnet src/HashTool.App/bin/Release/net10.0/CheckHash.dll --smoke-test"
# Fails when the quality gate fails.
- name: End analysis
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: dotnet sonarscanner end /d:sonar.token="$SONAR_TOKEN"
+5
View File
@@ -43,6 +43,8 @@ Run the app or smoke test with `HASHTOOL_DATA_DIR` pointing at a scratch folder
workflow; `ci-ok` is the single required check.
- Don't embed C# or regex text containing backslashes in Python/bash heredocs — the escapes get mangled. Edit such
files with the editor tools.
- .NET tools (vpk, nuget-license, CycloneDX, dotnet-sonarscanner, dotnet-coverage) live in `.config/dotnet-tools.json`;
Dependabot doesn't read a root `dotnet-tools.json`. vpk and the Velopack package move together.
## Workflows
@@ -50,3 +52,6 @@ Run the app or smoke test with `HASHTOOL_DATA_DIR` pointing at a scratch folder
- `security.yml` — vulnerable packages, licences (eng/allowed-licenses.json), gitleaks, actionlint, zizmor.
- `release.yml` — manual (`workflow_dispatch`, version + publish) with an upgrade gate from 1.1.3; dry run on PRs that
touch release files. Discord notifications use the maintainer's `poli0981/.github` reusable workflows, pinned by SHA.
- `sonarcloud.yml` — SonarCloud CI analysis (Automatic Analysis is off; project `poli0981_HashTool`): scanner build,
Cobertura coverage from the tests and the smoke test (`dotnet-coverage`), "Sonar way" gate on new code. Skipped when
`SONAR_TOKEN` isn't available (forks, Dependabot). Issues and hotspots can't be resolved from here; fix them in code.
+1 -1
View File
@@ -34,7 +34,7 @@ How the code is organised: [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).
2. Keep the change focused; follow the existing style (the analyzers and `.editorconfig` help).
3. Add or update tests — hashing and checksum logic belongs in `HashTool.Core` with tests in `tests/HashTool.Core.Tests`.
4. Add a line under `## [Unreleased]` in [CHANGELOG.md](CHANGELOG.md) for anything users will notice.
5. Open the pull request and link the issue. CI builds and tests on Windows, macOS and Linux, and scans dependencies, licences, secrets and workflows; everything must be green before merging.
5. Open the pull request and link the issue. CI builds and tests on Windows, macOS and Linux, and scans dependencies, licences, secrets and workflows; everything must be green before merging. SonarCloud also analyses the pull request: new code should pass its quality gate (A ratings, at least 80% coverage, little duplication).
Commit messages follow [Conventional Commits](https://www.conventionalcommits.org/) (`fix(core): …`, `feat(app): …`, `docs: …`).
+2
View File
@@ -2,6 +2,8 @@
[![Release](https://img.shields.io/github/v/release/poli0981/HashTool?sort=semver)](https://github.com/poli0981/HashTool/releases/latest)
[![CI](https://github.com/poli0981/HashTool/actions/workflows/ci.yml/badge.svg)](https://github.com/poli0981/HashTool/actions/workflows/ci.yml)
[![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=poli0981_HashTool&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=poli0981_HashTool)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=poli0981_HashTool&metric=coverage)](https://sonarcloud.io/component_measures?id=poli0981_HashTool&metric=coverage)
[![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/poli0981/HashTool/badge)](https://scorecard.dev/viewer/?uri=github.com/poli0981/HashTool)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
[![Downloads](https://img.shields.io/github/downloads/poli0981/HashTool/total.svg)](https://github.com/poli0981/HashTool/releases)
+1 -1
View File
@@ -14,7 +14,7 @@ Security fixes are released for the latest minor version only. Installed and por
### How releases are protected
- Releases are built, tested and published by GitHub Actions from the `main` branch; every release includes `SHA256SUMS`, CycloneDX SBOMs and [build provenance attestations](https://docs.github.com/actions/security-for-github-actions/using-artifact-attestations) (`gh attestation verify <file> --repo poli0981/HashTool`).
- Every pull request is checked for vulnerable NuGet packages, licences, leaked secrets and unsafe workflow patterns, in addition to CodeQL and OpenSSF Scorecard.
- Every pull request is checked for vulnerable NuGet packages, licences, leaked secrets and unsafe workflow patterns, in addition to CodeQL, SonarCloud and OpenSSF Scorecard.
- The apps are not code-signed yet, so Windows SmartScreen and macOS Gatekeeper may warn when they are first opened.
### Reporting a Vulnerability
@@ -4,6 +4,8 @@
<IsPackable>false</IsPackable>
<!-- Run manually, see docs/ARCHITECTURE.md (Benchmarks). -->
<Optimize>true</Optimize>
<!-- SonarCloud analyses benchmarks as test code: they are not product code and have no coverage. -->
<SonarQubeTestProject>true</SonarQubeTestProject>
</PropertyGroup>
<ItemGroup>
+7 -1
View File
@@ -14,7 +14,8 @@ build/ packaging inputs (macOS Info.plist, Linux icon)
Shared build settings: `Directory.Build.props` (version, nullable, analyzers, NuGet audit), `Directory.Packages.props`
(central package versions with transitive pinning), `global.json` (SDK and the Microsoft.Testing.Platform runner),
`dotnet-tools.json` (vpk, nuget-license, CycloneDX).
`.config/dotnet-tools.json` (vpk, nuget-license, CycloneDX, SonarScanner for .NET, dotnet-coverage; Dependabot keeps
them up to date).
## HashTool.Core
@@ -60,6 +61,11 @@ mismatched file, 2 usage error or unreadable checksum file. Published per platfo
- `tests/Compat/LegacyClient` — not in the solution: the release workflow uses it to update a real 1.1.3 install with
its own Velopack 0.0.1298 library and updater (see [RELEASING.md](RELEASING.md)).
[SonarCloud](https://sonarcloud.io/project/overview?id=poli0981_HashTool) analyses every pull request and `main` from
the SonarCloud workflow: the C# during a real build, with coverage from the tests above and from the smoke test
(`dotnet-coverage`), plus the scripts and workflows. Its quality gate ("Sonar way") applies to new code; `bench/` is
analysed as test code and `eng/` is left out of coverage.
## Benchmarks
```text