Azure DevOps Pipeline picking up wrong repository for analysis

I am using a azure devops pipeline (api.UserManagement-CI) to run sonar cloud analysis. I have a code repo (api.UserManagement) which I am referring for analysis. I am using template for same. Sonar cloud should analysis for code repo (api.UserManagement). But it is actually analyzing devops pipeline (api.UserManagement-CI).

api.UserManagement-CI Code

resources:
repositories:

  • repository: templates
    name: tool.PipelineTemplates
    type: git
    ref: sonar-coverage
  • repository: apiusermanagement
    name: api.UserManagement
    type: git
    ref: sonar-coverage

stages:

  • stage: build
    jobs:
    • template: dotnet-core/dotnet-core-build.yml@templates
      parameters:
      buildConfiguration: ‘${{ variables.buildConfiguration }}’
      nugetFeed: ‘${{ variables.nugetFeed }}’
      services:
      • name: ‘UWA.UserManagement.API’
        projectFile: ‘**/*.csproj’
        repo: ‘apiusermanagement’
        sonarQube: # include sonarQube object if using SonarQube
        • runSonarQube: ‘false’
          #sonarProjectKey: ‘396f872c49c107edb6601a1f2d3b2ae65cf08780’
          #sonarProjectName: ‘EntryRequirementsAPI’
          sonarCloud:
        • runSonarCloud: ‘true’
          sonarCloudProjectKey: ‘UserManagementAPI’
          sonarCloudProjectName: ‘UserManagementAPI’
          sonarCloudProjectVersion: ‘$(VAL_uvgo_Version)’

      runDotNetCoreTests: ‘true’

      coretest: 'true'
      vmImage: 'windows-2022'
      

tool.PipelineTemplates

  • ${{ each sonar in s.sonarCloud }}:
    • task: SonarCloudPrepare@2
      displayName: ‘Run SonarCloud Analysis’
      condition: and(succeeded(), eq(‘${{ sonar.runSonarCloud }}’, ‘true’))
      inputs:
      SonarCloud: ‘SonarCloudConnection’
      organization: ‘uwa-dev’
      scannerMode: ‘MSBuild’
      projectKey: ${{ sonar.sonarCloudProjectKey }}
      projectName: ${{ sonar.sonarCloudProjectName }}
      projectVersion: ${{ sonar.sonarCloudProjectVersion }}

I not pr branch then add branch name

      ${{ if and(eq(sonar.checkForQualityGate, true), ne(variables['Build.Reason'], 'PullRequest')) }}:
        extraProperties: |
            sonar.branch.name=${{ resources.repositories['apiusermanagement'].ref }}
            sonar.qualitygate.wait=true
            sonar.qualitygate.timeout=1800
            sonar.sources=$(Build.SourcesDirectory)/api.UserManagement
            sonar.tests=$(Build.SourcesDirectory)/api.UserManagement/tests
            sonar.coverageReportPaths=$(Build.ArtifactStagingDirectory)/CoverageResults/Cobertura.xml
            sonar.testExecutionReportPaths=$(Build.ArtifactStagingDirectory)/CoverageResults/index.html

Hi,

Is the wrong code being analyzed, or is the correct code being analyzed, but with the wrong project key / name?

If the former, that’s a question of a bad checkout in your pipeline. If the latter, then I would take a look at those envvars you’re using to provide project identification to analysis and make sure they’re being set and cleared properly.

 
HTH,
Ann

Wrong code analyzed, The repo we calling from pipeline is not analysed, repo containing pipeline is analyzed.

Hi,

So you’re in one repo and you expect a different one to be analyzed?

SonarQube can only analyze the code you give it. You need to perform the checkout and run the analysis from the repo you want analyzed.

 
HTH,
Ann

Hey Ann,
Thanks for reply, Now I followed what you advised, I can see that code is being analyzed. But there is different error we never encountered.

Error during SonarScanner execution
2025-03-27T11:47:07.0218884Z ##[error]ERROR: File UWA.UserManagement.API/Controllers/ApplicationsController.cs can’t be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files
ERROR:
2025-03-27T11:47:07.0220004Z ERROR: File UWA.UserManagement.API/Controllers/ApplicationsController.cs can’t be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

Regards,
Ashish

Hi Ashish,

Are you manually setting sonar.sources and/or sonar.tests? If so, that’s likely to be the source of this error.

 
HTH,
Ann

Yes, thats what we are doing. like below

- ${{ each sonar in s.sonarCloud }}:
      - task: SonarCloudPrepare@2
        displayName: 'Run SonarCloud Analysis'
        condition: and(succeeded(), eq('${{ sonar.runSonarCloud }}', 'true'))
        inputs:
          SonarCloud: 'SonarCloudConnection'
          organization: 'uwa-dev'
          scannerMode: 'MSBuild'
          projectKey: ${{ sonar.sonarCloudProjectKey }}
          projectName: ${{ sonar.sonarCloudProjectName }}
          projectVersion: ${{ sonar.sonarCloudProjectVersion }}
#### I not pr branch then add branch name
          #${{ if and(eq(sonar.checkForQualityGate, true), ne(variables['Build.Reason'], 'PullRequest')) }}:
          extraProperties: |
            sonar.branch.name=sonar-coverage
            sonar.qualitygate.wait=true
            sonar.qualitygate.timeout=1800
            sonar.sources=$(Build.SourcesDirectory)
            sonar.exclusions=**/test/**,**/*.test.cs,**/*.json

Hi,

For .NET analysis, sonar.sources and sonar.tests are automatically found/calculated. Remove this additional configuration, and this should work fine.

 
HTH,
Ann



I dont see coverage for my branch. commit id “1328ad09” is for my pipeline repo branch.

This was actual issue.

resources:
repositories:

repository: templates
name: tool.PipelineTemplates
type: git
ref: sonar-coverage
repository: apiusermanagement
name: api.UserManagement
type: git
ref: sonar-coverage
- task: SonarCloudPrepare@2
        displayName: 'Run SonarCloud Analysis'
        condition: and(succeeded(), eq('${{ sonar.runSonarCloud }}', 'true'))
        inputs:
          SonarCloud: 'SonarCloudConnection'
          organization: 'uwa-dev'
          scannerMode: 'MSBuild'
          projectKey: ${{ sonar.sonarCloudProjectKey }}
          projectName: ${{ sonar.sonarCloudProjectName }}
          projectVersion: ${{ sonar.sonarCloudProjectVersion }}
          extraProperties: |
            sonar.branch.name=sonar-coverage
            sonar.qualitygate.wait=true
            sonar.qualitygate.timeout=1800
            sonar.exclusions=**/test/**,**/*.test.cs,**/*.json

Hi,

I’m confused.

 
Ann

yes, I thought so. as we are using 3 repos. I can send logs on your email. Please take your time. if that helps. Things are working if we have pipeline in code repo only.

Hi,

You’ll need to check out and analyze each repo individually, from its own directory. For any .NET repos, you will need to not manually specify sonar.sources or sonar.tests.

 
HTH,
Ann