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