0

I am currently implementing an azure DevOps pipeline for my .Net microservices. I researched and read the sonar cloud doc, but nothing helped yet. I did generate the code coverage report using cobertura and that is working correctly in azure devops but I found out that you cannot link that type of format to sonarcloud so I was trying somehow to generate also the opencover and use it for exporting to sonarcloud, this is what I have done so far:

trigger:
- dev
- main

pool:
  vmImage: ubuntu-latest

jobs:
- job: check_code_quality
  steps:
  - task: SonarCloudPrepare@1
    displayName: 'Setup Sonar Cloud'
    inputs:
      SonarCloud: 'SonarCloud'
      organization: '***'
      scannerMode: 'MSBuild'
      projectKey: '***'
      projectName: '****'
      extraProperties: |
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
        sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
        
  - task: DotNetCoreCLI@2  
    displayName: 'Build main code'  
    inputs:  
      command: build  
      projects: '**/*.csproj'
      arguments: '--configuration Release'
  
  - task: DotNetCoreCLI@2
    displayName: 'Run unit tests'
    continueOnError: true
    inputs:
      command: 'test'
      projects: '**/*[Tt]est*/*.csproj'
      testRunTitle: 'Backend Unit Testing'
      arguments: '--configuration Release --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'
      publishTestResults: true
  
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
  
  - task: SonarCloudAnalyze@1
    displayName: 'Run Sonar Analysis'
  
  - task: SonarCloudPublish@1
    displayName: 'Publish Sonar Results'
    inputs:
      pollingTimeoutSec: '300'

I checked the logs and I saw this:

Attachments:
  /home/vsts/work/_temp/111bef07-6e19-43fe-a689-7597bc24dda3/coverage.cobertura.xml
  /home/vsts/work/_temp/111bef07-6e19-43fe-a689-7597bc24dda3/coverage.opencover.xml

Also I have an issue that sonar cloud is only analyzing my test projects not the main code

Jbtf
  • 51
  • 1
  • 8

2 Answers2

0

I suppose that you could re-modify your dotnet test task with argument below.

arguments: '--logger trx --collect:"XPlat Code Coverage" --collect:"Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura --output $(build.artifactstagingdirectory)'

enter image description here

Full yaml for dotnet part as below.

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: 'test'
    projects: 'xxx.csproj'
    arguments: '--logger trx --collect:"XPlat Code Coverage" --collect:"Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura --output $(build.artifactstagingdirectory)'
    publishTestResults: false

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.Repository.LocalPath)\{reponame}\TestResults'
    ArtifactName: 'drop'
    publishLocation: 'Container'
 
- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'VSTest'
    testResultsFiles: '**/*.trx'
  
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage from $(Agent.TempDirectory)\**\coverage.cobertura.xml'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(Build.Repository.LocalPath)\{reponame}\TestResults\**\coverage.cobertura.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)\reports' ###please keep this setting as mine

For the sonarqube part, the sonarqube task would search for the .trx file in your agent folder.

My path is below, and you could check your local agent folder in the debug log and set the path in sonarqube task.

enter image description here

==================================================

Updated on 1/12

i suppose that you could find all the test result files with coverage..xml, .trx, and .coverage, you could check in pipeline artifacts drop-down list for the existence of the files and then go to your local folder again,.

enter image description here

Ceeno Qi-MSFT
  • 924
  • 1
  • 3
  • 5
  • Does sonar cloud only require .trx files, and why u removed the opencover? – Jbtf Jan 11 '23 at 07:35
  • @Jbtf I suppose that you could tested locally, if your sonarqube could also accept other type result report, you could customize the file path in your pipeline. – Ceeno Qi-MSFT Jan 11 '23 at 08:23
  • @Jbtf take notice the setting in dotnet test task `publishTestResults: true`, with this setting enabled, your result file would be outputted to `$(agent.tempdirectory)`, which will lead your result files could hardly be used in afterward task. And that is why i set the `dotnet test` task with the argument `--output $(build.artifactstagingdirectory)`, and publish the result files to pipeline artifacts for self-check comparison. – Ceeno Qi-MSFT Jan 11 '23 at 08:27
  • so i changed the output dir like u said and the files now are being generated like this: `/home/vsts/work/1/s/Services/Service1/TestResults/b0c455de-52ab-449f-af7b-e8785e5e121a/coverage.opencover.xml` . So the publish build artifacts it fails because cannot find those files: `##[error]Publishing build artifacts failed with an error: Not found PathtoPublish: /home/vsts/work/1/s/test/TestResults` – Jbtf Jan 11 '23 at 09:38
  • @Jbtf I suggest that you could check the debug logs of the `dotnet test` task to check the generated files path – Ceeno Qi-MSFT Jan 11 '23 at 09:59
  • That is the generated file i took from logs: `/home/vsts/work/1/s/Services/Service1/TestResults/b0c455de-52ab-449f-af7b-e8785e5e121a/coverage.opencover.xml` – Jbtf Jan 11 '23 at 11:28
  • @Jbtf i suppose that you could find all the test result files with `coverage..xml`, `.trx`, and `.coverage`, you could check in pipeline artifacts drop-down list for the existence of the files and then go to your local folder again,. – Ceeno Qi-MSFT Jan 12 '23 at 02:41
  • What could cause that the sonar cloud is only analyzing my test projects, I think that is the issue. – Jbtf Jan 12 '23 at 07:10
0

** Solution

I don't know why but changing the pool vmImage: windows-latest solved my issue. Final pipeline:

trigger:
- dev
- main

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

jobs:
- job: check_code_quality
  steps:
  - task: DotNetCoreCLI@2
    displayName: 'Restore solution'
    inputs:
      command: 'restore'
      projects: '**/*.sln'
      feedsToUse: 'select'

  - task: SonarCloudPrepare@1
    displayName: 'Prepare analysis configuration'
    inputs:
      SonarCloud: ''
      organization: ''
      scannerMode: 'MSBuild'
      projectKey: ''
      projectName: 'backend'
      extraProperties: |
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
        sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
        sonar.verbose=true

  - task: DotNetCoreCLI@2
    displayName: 'Build solution'
    inputs:
      command: 'build'
      projects: '**/*.sln'

  - task: DotNetCoreCLI@2
    displayName: 'Execute Unit tests'
    inputs:
      command: 'test'
      projects: '**/*[Tt]est*/*.csproj'
      arguments: '--configuration Release --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover'
      publishTestResults: true
  
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
    inputs:
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
      
  - task: SonarCloudAnalyze@1
    displayName: 'Run SonarCloud analysis'

  - task: SonarCloudPublish@1
    displayName: 'Publish results on build summary'
    inputs:
      pollingTimeoutSec: '300'
Jbtf
  • 51
  • 1
  • 8