Since the dotnet scanner does not (yet) support config files, I have come up with a work around: run a task that reads the config file if it exists and transforms it into extraproperties.
Advantage is that you can actually mix pipeline settings with config file settings, depending on how you order them.
Here the powershell script for reading the config file:
# parses repository's sonar-project.properties and exports "SonarExtraProperties"
- pwsh: |
$file = "$(Build.SourcesDirectory)/sonar-project.properties"
if (-not (Test-Path $file)) {
Write-Host "No sonar-project.properties found at $file. Skipping."
return
}
# Keys to ignore because they should come from the service connection or pipeline inputs
$deny = @(
'sonar.host.url',
'sonar.login', 'sonar.password', 'sonar.token',
'sonar.projectKey', 'sonar.projectName', 'sonar.projectVersion'
)
$lines = Get-Content -Path $file -Encoding UTF8
$kept = New-Object System.Collections.Generic.List[string]
foreach ($raw in $lines) {
$line = $raw.Trim()
if ([string]::IsNullOrWhiteSpace($line)) { continue }
if ($line.StartsWith('#')) { continue }
$eq = $line.IndexOf('=')
if ($eq -lt 1) { continue } # skip malformed lines
$key = $line.Substring(0, $eq).Trim()
$val = $line.Substring($eq + 1).Trim()
if ($deny -contains $key) { continue }
# Keep exactly "key=value" lines for extraProperties
$kept.Add("$key=$val")
}
$propsBlock = ($kept -join "`n")
Write-Host "Resolved $(($kept | Measure-Object).Count) Sonar properties from file."
# Export multiline variable for later step
Write-Host "##vso[task.setvariable variable=SonarExtraProperties;issecret=false]$propsBlock"
displayName: "Build Sonar extraProperties from sonar-project.properties"
And here how to add it to your pipeline:
# Prepare SonarQube analysis
- task: SonarQubePrepare@8
inputs:
SonarQube: 'NameOfYourServiceConnection' # your SonarQube service connection
scannerMode: 'MSBuild'
projectKey: 'your.project.key'
projectName: 'Your Project Name'
# You can set projectVersion here or derive from Build.BuildNumber
# projectVersion: '$(Build.BuildNumber)'
extraProperties: |
$(SonarExtraProperties)
You can add your pipeline parameters before or after the $(SonarExtraProperties) depending on which one should overwrite which, if defined in both.