Clarification - SonarCloudPrepare@3 and cache - unclear documentation

  • ALM used: Azure DevOps

  • CI system used: Azure DevOps

  • Languages of the repository: C#

According to documentation:

It states as steps:

First define the prepare task:

- task: SonarCloudPrepare@3
  inputs:
    SonarCloud: 'YourSonarCloudServerEndpoint'
    scannerMode: 'dotnet'
    msBuildVersion: '<YourSonarScannerFullVersionNumber>'
    projectKey: '<YourProjectKey>'

and then:

- task: Cache@2
  displayName: Cache SonarScanner
  inputs:
    key: '"SonarScanner" | ".NET" | "$(Agent.OS)"'
    path: '$(Agent.ToolsDirectory)/SonarScanner .NET'

but this does not really make much sense.
The cache should usually be resolved prior to the task that could potentially use the cache.

Moreover, the SonarCloudPrepare@3 requires dotnetScannerVersion in dotnetmode (not msBuildVersion - documentation seems to be wrong here)
and since that value can change i would assume it must be part of the cache key - correct?
Example:

        - task: Cache@2
          displayName: Cache SonarScanner
          inputs:
            key: '"SonarScanner" | ".NET" | "$(Agent.OS)" | "${{ parameters.SonarCloudMsBuildVersion}}"'
            path: '$(Agent.ToolsDirectory)/SonarScanner .NET'

Can you please elaborate on how the correct order of tasks should be and how the internals of the SonarCloudPrepare@3 works on using a cache that being hit after the task has run?

For brevity this is how I have currently setup my pipeline as it follows the general way of doing cache.

  1. Restore Cache if applicable.
  2. The extension is either internally aware if a cache has been restored or you inform via setting cache variable
  3. The cache task automatically cache any non hits at the end of the pipeline
- task: Cache@2
  displayName: Cache SonarScanner
  inputs:
    key: '"SonarScanner" | ".NET" | "$(Agent.OS)" | "${{ parameters.SonarCloudMsBuildVersion}}"'
    path: '$(Agent.ToolsDirectory)/SonarScanner .NET'


- task: SonarCloudPrepare@3            
  displayName: Sonarcloud Setup for analysis of the codebase
  inputs:
    SonarCloud: 'SonarCloud'
    organization: ${{ parameters.SonarCloudOrganisation }}
    scannerMode: 'dotnet'
    dotnetScannerVersion:  ${{ parameters.SonarCloudMsBuildVersion}}
    projectKey: $(SonarCloudGeneratedProjectKey)
    projectName: $(SonarCloudGeneratedProjectName)
    extraProperties: |
      sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/**/coverage.opencover.xml
      sonar.scanner.metadataFilePath=$(Agent.TempDirectory)/sonar/$(Build.BuildNumber)/report-task.txt
      $(SonarCloudDebugSettings)

Thanks.

In fact, you are correct. The prepare task itself checks if a cache is present for the downloaded scanner and is necessary to properly use the cache.

I’ll raise this to the right people to address the documentation. Thank you!

1 Like