- script: |
git config --global http.https://dev.azure.com/<company>/.extraheader "AUTHORIZATION: bearer $(System.AccessToken)"
# Now fetch the PR source branch using the token
git fetch origin $(System.PullRequest.SourceBranch):$(System.PullRequest.SourceBranch)
git checkout $(System.PullRequest.SourceBranch)
displayName: 'Fetch and Checkout PR Source Branch'
env:
# This exposes the system access token to the script
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
- script: |
git checkout $(System.PullRequest.SourceBranch)
displayName: 'Checkout PR Source Branch'
- task: DotNetCoreCLI@2
displayName: New Manifest for tool
inputs:
command: custom
custom: 'new '
arguments: tool-manifest
- task: PowerShell@2
displayName: Set the prereleaseVersionNumber variable value
inputs:
targetType: 'inline'
script: |
[string] $dateTime = (Get-Date -Format 'yyyyMMddTHHmmss')
[string] $prereleaseVersionNumber = "$(stableVersionNumber)-$(Build.SourceVersion)"
Write-Host "Setting the prerelease version number variable to '$prereleaseVersionNumber'."
Write-Host "##vso[task.setvariable variable=prereleaseVersionNumber]$prereleaseVersionNumber"
- task: PowerShell@2
displayName: Set the versionNumber to the stable or prerelease version number based on if the 'main' branch is being built or not
inputs:
targetType: 'inline'
script: |
[bool] $isMainBranch = $$(isMainBranch)
[string] $versionNumber = "$(prereleaseVersionNumber)"
if ($isMainBranch)
{
$versionNumber = "$(stableVersionNumber)"
}
Write-Host "Setting the version number to use to '$versionNumber'."
Write-Host "##vso[task.setvariable variable=versionNumber]$versionNumber"
- task: PowerShell@2
displayName: Set the name of the build (i.e. the Build.BuildNumber)
inputs:
targetType: 'inline'
script: |
[string] $buildName = "$(versionNumber)_$(Build.SourceBranchName)"
Write-Host "Setting the name of the build to '$buildName'."
Write-Host "##vso[build.updatebuildnumber]$buildName"
- task: NuGetToolInstaller@1
displayName: 'Install NuGet 5.8'
inputs:
versionSpec: '5.8.x'
- task: NuGetCommand@2
displayName: 'Restore packages using NuGet'
inputs:
restoreSolution: '**/*.csproj'
- task: UseDotNet@2
displayName: Use .NET 6.0
inputs:
packageType: 'sdk'
version: '6.0.x'
- task: DotNetCoreCLI@2
displayName: 'Add Coverlet.msbuild package'
inputs:
command: 'custom'
custom: 'add'
projects: '$(Build.SourcesDirectory)/Virtue/Virtue.API/Virtue.API.csproj'
arguments: 'package coverlet.msbuild'
- task: DotNetCoreCLI@2
displayName: 'dotnet restore Virtue.API'
inputs:
command: 'restore'
projects: '$(Build.SourcesDirectory)/Virtue/Virtue.API/Virtue.API.csproj'
includeNuGetOrg: true
noCache: true
- task: SonarCloudPrepare@1
inputs:
SonarCloud: 'SonarCloud Service Connection'
organization: '<company_name>'
scannerMode: 'MSBuild'
projectKey: 'dotnet'
projectName: 'Dotnet'
extraProperties: |
sonar.projectBaseDir=$(Build.SourcesDirectory)
sonar.exclusions=**/README.md,**/obj/**,**/*.dll
sonar.inclusions=**/*.csproj
sonar.cs.vscoveragexml.reportsPaths=$(Build.SourcesDirectory)/**/coverage.xml
sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
sonar.pullrequest.key=$(System.PullRequest.PullRequestId)
sonar.pullrequest.branch=$(System.PullRequest.SourceBranch)
sonar.pullrequest.base=$(System.PullRequest.TargetBranch)
- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI@2
displayName: 'Test ...'
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
testRunTitle: 'dotnet test'
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Build.SourcesDirectory)/**/coverage.opencover.xml'
reportDirectory: '$(Build.SourcesDirectory)/**/coverage/'
- script: |
dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.opencover.xml -targetdir:$(Build.SourcesDirectory)/CoverageReport -reporttypes:"HtmlInline;Cobertura"
displayName: 'Generate Coverage Report'
- task: PublishTestResults@2
displayName: "Publish Test Results"
inputs:
testResultsFormat: VSTest
testResultsFiles: "$(Build.SourcesDirectory)/**.*trx"
mergeTestResults: true
condition: succeededOrFailed()
- task: SonarCloudAnalyze@1
inputs:
sonar.analysis.level: 'DOTNET'
- task: SonarCloudPublish@1
inputs:
pollingTimeoutSec: '300'
Why on pull request, it is not taking new lines from my feature branch. I could see zero coverage and no new lines. Please help me. It is on high priority.
I tried many ways but no luck. I would like to analyse new lines of code using pull request that triggers my azure pipeline
2
Answers
I could fix the issue. Thank you. Needed to modify pipeline script.
There are some points for collecting code coverage that you need to know:
By default, the output result files of test and code coverage are generated under the directory "
$(Agent.TempDirectory)
" rather than "$(Build.SourcesDirectory)
".If you want to specify the output directory to a different one, On the
dotnet test
command, you can configure the arguments like as below.If your projects are .NET 5 or later, it is recommended using the NuGet package "
coverlet.collector
" instead of "coverlet.msbuild
" in the test projects.In the pipeline, you can configure like as below.
The dotnet test task will automatically publish test result by default (
publishTestResults: true
), if you want to use a separatePublishTestResults
task to publish test result, you can set "publishTestResults: false
". Then add aPublishTestResults
task after the dotnet test task.