Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions .github/workflows/sonar-library.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Sonar library readiness

on:
workflow_call:
inputs:
tool-revision:
description: Full workflows commit SHA, identical to this reusable workflow pin and the consumer policy
required: true
type: string

permissions:
contents: read
actions: read

jobs:
readiness:
if: >-
vars.SONAR_MAIN_ENABLED == 'true' &&
github.event_name == 'workflow_run' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_branch == 'main' &&
github.event.workflow_run.head_repository.full_name == github.repository
runs-on: ubuntu-24.04
environment: sonar-main
timeout-minutes: 90
concurrency:
group: sonar-library-${{ github.repository }}
cancel-in-progress: false
steps:
- name: Require successful CI for the current main revision
env:
CANDIDATE_SHA: ${{ github.event.workflow_run.head_sha }}
CI_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
TOOL_REVISION: ${{ inputs.tool-revision }}
run: |
set -euo pipefail
test "$GITHUB_REF" = refs/heads/main
test "$GITHUB_SHA" = "$CANDIDATE_SHA"
test "$GITHUB_WORKFLOW_SHA" = "$CANDIDATE_SHA"
test "$CI_CONCLUSION" = success
[[ "$TOOL_REVISION" =~ ^[0-9a-f]{40}$ ]]

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.sha }}
fetch-depth: 0
persist-credentials: false

- name: Check out the immutable shared runner
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: andregoepel/workflows
ref: ${{ inputs.tool-revision }}
path: artifacts/sonar/tool-source
persist-credentials: false

- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
global-json-file: artifacts/sonar/tool-source/sonar/global.json

- name: Build the pinned shared runner before exposing analysis credentials
working-directory: artifacts/sonar/tool-source/sonar
run: |
set -euo pipefail
dotnet restore src/AndreGoepel.Workflows.Sonar/AndreGoepel.Workflows.Sonar.csproj --locked-mode
dotnet publish src/AndreGoepel.Workflows.Sonar/AndreGoepel.Workflows.Sonar.csproj -c Release --no-restore -o "$RUNNER_TEMP/sonar-tool"

- name: Verify main evidence and perform full central analysis
env:
WORKFLOWS_GITHUB_TOKEN: ${{ github.token }}
WORKFLOWS_SONAR_TOKEN: ${{ secrets.SONAR_SCAN_TOKEN }}
WORKFLOWS_SONAR_VERIFY_TOKEN: ${{ secrets.SONAR_VERIFY_TOKEN }}
run: |
set -euo pipefail
WORKFLOWS_SONAR_TOOL_SHA=$(git -C artifacts/sonar/tool-source rev-parse HEAD)
export WORKFLOWS_SONAR_TOOL_SHA
dotnet "$RUNNER_TEMP/sonar-tool/AndreGoepel.Workflows.Sonar.dll" --root "$GITHUB_WORKSPACE" --trusted-main

- name: Preserve sanitized analysis diagnostics
if: ${{ always() }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: sonar-analysis-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
path: artifacts/sonar/main-${{ github.run_id }}-${{ github.run_attempt }}/publish/summary.json
if-no-files-found: ignore
retention-days: 7

- name: Publish analysis readiness evidence
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: sonar-library-readiness-${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
path: artifacts/sonar/main-${{ github.run_id }}-${{ github.run_attempt }}/publish/readiness.json
if-no-files-found: error
retention-days: 1
50 changes: 50 additions & 0 deletions .github/workflows/validate-sonar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Validate Sonar runner

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

jobs:
contract-tests:
strategy:
fail-fast: false
matrix:
os: [ubuntu-24.04, windows-latest]
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: sonar
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0
with:
global-json-file: sonar/global.json
- name: Restore
run: dotnet restore --locked-mode
- name: Restore tools
run: dotnet tool restore
- name: Format check
run: dotnet csharpier check .
- name: Build
run: dotnet build -c Release --no-restore
- name: Test offline contracts
run: dotnet test -c Release --no-build --no-restore
- name: Audit dependencies
shell: pwsh
run: |
$audit = dotnet list package --vulnerable --include-transitive --format json --no-restore
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$report = $audit | ConvertFrom-Json
if ($report.projects.Count -ne 2 -or $report.sources.Count -eq 0) { throw 'Incomplete dependency audit.' }
foreach ($project in $report.projects) {
foreach ($framework in $project.frameworks) {
if ($framework.topLevelPackages -or $framework.transitivePackages) { throw 'Vulnerable dependency found.' }
}
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
artifacts/
sonar/**/bin/
sonar/**/obj/
sonar/**/TestResults/
**/__pycache__/
8 changes: 6 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ includes. The `ci-library` → `ci-hostapp` → `e2e` / `docker-image` shape
referenced by that root CLAUDE.md's "Commands" section lives here, not
duplicated per-repo.

This repo has no application code and no `Directory.Build.props` — the
The repository root has no application code or `Directory.Build.props` — the
`.NET repos` command/warning rules from the shared conventions don't apply
here. What applies instead:
to legacy image producers. The isolated `sonar/` C# tool follows the shared
.NET conventions and has its own pinned SDK, formatting, build, tests and
additive validation workflow. See `docs/sonar-library.md`.

What applies to existing reusable workflows:

## What lives here

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,9 @@ per-repo:
- finance-app#65
- customer-portal#89
- andregoepel-dev#56

## Opt-in Sonar readiness

See [the library analysis guide](docs/sonar-library.md) for the additive shared
runner, local verification, consumer policy and controlled activation.
Existing CI and publishing workflows retain their behavior.
140 changes: 140 additions & 0 deletions docs/sonar-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Opt-in Sonar library analysis

This addition extracts the analysis/readiness subset accepted in
[app-foundation PR #289](https://github.com/andregoepel/app-foundation/pull/289)
at commit `9e93efe1058637ea9bd781a33f9ff32e03104b09`. Its offline tests retain
the established failure cases. The shared runner uses the official SonarScanner
for .NET and Microsoft `dotnet-coverage`; Octokit.NET handles GitHub requests.
The small Sonar HTTP boundary covers server APIs without an unsupported .NET SDK.

Tracking: [workflows #27](https://github.com/andregoepel/workflows/issues/27),
[core #24](https://github.com/andregoepel/core/issues/24),
[rollout epic #238](https://github.com/andregoepel/app-foundation/issues/238).

## Scope and boundaries

The first consumer is `core`. Existing reusable workflows, their consumers,
and app-foundation's implementation remain unchanged. This runner supports the
current ecosystem's .NET 10 library layout: a `.slnx` solution, `src/` projects,
`tests/<project>/<project>.csproj`, xUnit v3/Microsoft.Testing.Platform test names
under `AndreGoepel.*`, and no external services or browser setup. Host-app/E2E
onboarding needs a separately reviewed extension. E2E is not applicable to core.

`sonar/` is an isolated non-packable .NET tool, not a library for consumer
applications. Its `AndreGoepel.Core` dependency supplies the existing Result
abstraction. It adds no dependency to the Core library. The legacy Python image
producers are unrelated and unchanged. App-foundation can migrate to this shared
implementation in a later reviewed change; its publishing receipts are not changed.

The output is **analysis readiness only**: `kind: library-analysis-readiness`,
`analysisReady: true`, `releaseEligible: false`. It does not authorize package
uploads or deployments, and cannot substitute for app-foundation's release receipt.
No OIDC access, package credentials, NuGet login or push operation is provided.

## Consumer contract

Each consumer commits these reviewed files:

- `scripts/sonar/contract.json`: SDK/server versions, central server identity,
gate name, complete solution/test inventory with minimum discovered counts,
executed source anchors and generated-source exclusions. Authored coverage
exclusions are prohibited. Core's example is in its onboarding PR.
- `scripts/sonar/main-policy.json`: repository and workflow paths, HTTPS Sonar
target, project/server identity, immutable tool revision, gate conditions,
frozen quality profiles with rule counts/timestamps, approved
`SPECIFIC_ANALYSIS` baseline, evidence age limits, and mandatory CI jobs/steps.
- An immutable new `sonar-main.yml` caller. Its reusable-workflow SHA,
`tool-revision` input and policy `toolRevision` must match. Never use a branch
or tag. The runner verifies the actual referenced workflow and tool checkout.
- Pinned local tools: SonarScanner 12.0.0, dotnet-coverage 18.12.0, CSharpier 1.3.0.
The runner SDK is exactly 10.0.402. NuGet lock files are mandatory.
- Ignore `artifacts/`, `.sonarqube/` and `.sonar-analysis.lock`.

Standalone CI uses empty `sharedPath`/`sharedSha`; unexpected reusable references
are rejected. CI using one reusable workflow must record its exact path and SHA.
Multiple/nested reusable CI chains need an explicit policy-model extension.
Only listed required jobs/steps count; an intentionally skipped tag publisher
on a main push is not treated as a failed mandatory job.

## Central workflow and initial activation

The new `.github/workflows/sonar-library.yml` is callable only. Its job requires
the consumer variable `SONAR_MAIN_ENABLED=true` and a completed main-push CI event.
It runs in the **consumer's** `sonar-main` environment with `contents: read` and
`actions: read`. Configure branch restrictions to main and the agreed reviewer
before adding `SONAR_SCAN_TOKEN` and `SONAR_VERIFY_TOKEN` environment secrets.
Use a project-analysis token and a separate browse-only verification identity.
No token belongs in a file, CLI argument supplied by an operator, PR or artifact.

Review/merge shared support first, then the consumer. Provision the consumer's
separate private Sonar project, copied/frozen profiles, dedicated gate and scoped
credentials. Capture actual profile rule revisions into its reviewed policy;
do not copy app-foundation's project, baseline, tokens or profile timestamps.
Keep the opt-in variable unset until these prerequisites are reviewed.

A null baseline is allowed only to retain the initial complete analysis. It
deliberately fails readiness and emits no `readiness.json`. Select that actual
main analysis as the reviewed baseline, commit its ID through review, and only
then collect successful readiness evidence. Empty profile inventory fails before
analysis. There is no bootstrap switch that silently grants readiness.

GitHub's caller context is retained inside a reusable workflow; the runner binds
that context to the current main SHA and triggering CI attempt, then rechecks
the GitHub API before, during and after analysis. It also verifies successful
required jobs/steps, freshness, source hashes, unchanged policy, the submitted CE
task, latest analysis, revision/build string, gate and exact baseline. A newer
main commit, rerun, missing evidence, stale evidence or changed policy fails closed.
See [GitHub's reusable workflow context](https://docs.github.com/en/actions/reference/workflows-and-actions/reusing-workflow-configurations).

Analysis is serialized per consumer. During wave 1 only core is activated; do not
enable a further consumer concurrently until staging capacity has been measured.
Successful final jobs upload one short-lived readiness record; failures may upload
only the sanitized summary. Private logs, coverage XML and scanner files are never
uploaded. Scanner scratch state is removed; an existing lock/state is preserved
and requires inspection instead of automatic deletion.

## Local Windows and Linux verification

Use PowerShell 7 and .NET SDK 10.0.402 on either platform. Start a separate
loopback Sonar server on port 9000 at the contract version, provision a
`<repository>-local` project and matching dedicated gate/profiles, and use
short-lived local scan/verification credentials. The main project and central
server cannot be used for local branch scans. Local results never create readiness.
The core wrapper fetches an exact shared commit, builds outside the analyzed
solution, strips credentials during bootstrap and invokes the same runner.

The full pipeline checks CSharpier, locked restore, vulnerabilities, ordinary
build, scanner build, every solution project, complete test discovery/execution,
fresh xUnit/TRX reports, coverage source checksums/anchors, merged coverage and
Sonar's actual coverage import. It uses the documented
[dotnet-coverage integration](https://docs.sonarsource.com/sonarqube-community-build/analyzing-source-code/dotnet-environments/dotnet-test-coverage).

To develop the shared tool, from `sonar/` run:

```text
dotnet restore --locked-mode
dotnet tool restore
dotnet csharpier format .
dotnet build -c Release --no-restore
dotnet test -c Release --no-build --no-restore
dotnet list package --vulnerable --include-transitive --format json --no-restore
```

The additive `validate-sonar.yml` runs these checks on Windows and Linux without
Sonar credentials or publishing permissions. Existing validation remains in place.
Until a separately reviewed Dependabot configuration change is approved, review
the new NuGet/tool pins manually; existing Action update configuration is preserved.

## Rollout acceptance and rollback

PR checks/local scans are preparation evidence. After reviewed activation, retain
two fresh exact-main CI/Sonar sets, controlled local gate failure/recovery,
missing/stale/wrong-revision rejection tests, and staged resource/task-processing
measurements. No main-branch failure drill is implied; obtain explicit approval
for one if needed. Keep the wave issue open until this evidence is accepted.

Rollback: unset `SONAR_MAIN_ENABLED`, wait for the in-flight analysis to finish,
revoke only this consumer's scoped credentials, and revert the additive caller
through review if needed. Retain analysis evidence. Do not reset another project's
baseline or change existing CI/publishing. A stopped readiness producer makes no
claim that an existing publisher is protected.
13 changes: 13 additions & 0 deletions sonar/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"csharpier": {
"version": "1.3.0",
"commands": [
"csharpier"
],
"rollForward": false
}
}
}
1 change: 1 addition & 0 deletions sonar/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
10 changes: 10 additions & 0 deletions sonar/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<IsPackable>false</IsPackable>
</PropertyGroup>
</Project>
12 changes: 12 additions & 0 deletions sonar/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AndreGoepel.Core" Version="1.0.4" />
<PackageVersion Include="Octokit" Version="14.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageVersion Include="xunit.v3" Version="4.0.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="4.0.1" />
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions sonar/Workflows.Sonar.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Solution>
<Project Path="src/AndreGoepel.Workflows.Sonar/AndreGoepel.Workflows.Sonar.csproj" />
<Project Path="tests/AndreGoepel.Workflows.Sonar.Tests/AndreGoepel.Workflows.Sonar.Tests.csproj" />
</Solution>
4 changes: 4 additions & 0 deletions sonar/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sdk": { "version": "10.0.402", "rollForward": "disable" },
"test": { "runner": "Microsoft.Testing.Platform" }
}
Loading
Loading