[GitLab CI] Cover multiple jobs with SonarScanner for .NET

Hi

GitLab: self-hosted
SonarQube: self-hosted

I have a working (mostly) GitLab CI configuration for my .Net 3.1 solution.
Currently, my pipeline consists of multiple stages, simplified example:

  • Build
  • Test
  • Pack
  • Deploy

Build stage includes Build job which looks like this (simplified):

build:
  stage: build
  only:
    - develop
  variables:
    GIT_DEPTH: 0
  cache:
    key: ${CI_JOB_NAME}
    paths:
      - .sonar/cache
  script:
    - dotnet sonarscanner begin
    - dotnet build
    - dotnet sonarscanner end

Test stage includes Unit Test job which looks like this (simplified):

unit_tests:
  stage: test
  only:
    - develop
  allow_failure: false
  before_script:
    - dotnet tool install -g dotnet-reportgenerator-globaltool
  script:
    - dotnet sonarscanner begin
    - dotnet test --collect:"XPlat Code Coverage" -l:trx -r:$CI_PROJECT_DIR/results
    - trx2junit ./results/*.trx
    - reportgenerator -reports:'./results/**/coverage.cobertura.xml' -targetdir:'CoverageReports' -reporttypes:'Cobertura;TextSummary;SonarQube;Badges';
    - dotnet sonarscanner end
  artifacts:
    when: always
    paths:
      - results/*.xml
      - CoverageReports/*.xml
      - CoverageReports/*.txt
      - CoverageReports/*.png
      - CoverageReports/*.svg
    reports:
      junit: results/*.xml

This mainly works as expected, I am still struggling with getting Unit test results to SonarQube, the coverage I was able to import, but that is a separate issue.

The issue that I am trying to resolve is that the current setup will produce two SonarQube reports, one for Build stage and one for Test stage.
As you can see I was already playing around with trying to cache some Sonarqube data, but I am not sure how to handle this properly.

The end result I am looking for is to keep separate Build and Test stages, but have only one SonarQube analysis/report.

Does anyone know how to achieve this?

Hi,

You’re going to need to combine your stages. As described in the docs

Between the begin and end steps, you need to build your project, execute tests and generate code coverage data.

 
HTH,
Ann

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.