Integrating .Net Core Code Coverage with Sonarqube jenkins file

I am working in .net core with C# and I written Unit Test cases.
I want to check the code coverage sonarqube with Jenkins pipeline

I generated the code coverage Report using Coverlet
I got the file “coverage.cobertura.xml”

So I need to achieve code coverage Percentage in SonarQube scan while doing build and deploying the application.
For This I need to use script as below

<
dotnet sonarscanner begin /k:"<sonar-project-key>"
    /d:sonar.login="<sonar-token>"
    /d:sonar.cs.opencover.reportsPaths=coverage.xml
dotnet build --no-incremental
coverlet .\CovExample.Tests\bin\Debug\net6.0\CovExample.Tests.dll
    --target "dotnet" 
    --targetargs "test --no-build"
    -f=opencover 
    -o="coverage.xml"
dotnet sonarscanner end /d:sonar.login="<sonar-token>"

/>
I tried to add script but it seems the format of Jenkins file as shown below is differ from the build script as shown above

Please help me out here how to integrate the code covergare build script to Jenkins file in sonarqube section.

for your referece below is the Jenkins file which i am using now.

stages {
        stage('Build') {
        agent {
         kubernetes {
            yaml easyAgents(easyTemplate: 'dotnetcore3')
        }
      }
        stages {
             stage('dotnetcore3 Build') {
        environment {
          CARID = "${CARID}"
          LOB = "${LOB}"
        }
        steps {
          container('dotnetcore3') {
            dotnetCoreBuild (
              credentialsId: "SYSTEM-ID",
              nugetPackageSourceUrl: "Url",
              generateArtifactoryName: "Artifactory",
              restorePath: "./xxxxx/xxxxxx.csproj",
              solutionPath: "./xxxxxx/xxxxxx.csproj",
              dotnetBuildCommand: "-c Release",
              windowsBuild: false
            )
            // sh "ls -la" // for debugging
          }
        }
      }
            stage('docker build') {
                steps {
                    container('docker') {
                        dockerBuild(
                            overWrite: true,
                            name: "${APPLICATION_NAME}",
                            version: "${DEPLOY_VER}",
                            namespace: "${ASSIGNEEGROUP_NORMALIZED}"
                        )
                    }
                }
            }
stage('Scans') {
            parallel {
                stage('Sonar Analysis') {
                    agent {
                        kubernetes {
                            yaml easyAgents(easyTemplate: 'dotnetcore6')
                        }
                    }
                    steps {
                        container('dotnetcore6') {
                            sonarScan(
                                projectKey: 'xxccxcxxcx',
                                applicationName: "${APPLICATION_NAME}",
                                projectVersion: "1.0.0",                                
                                sonarQualityGate: 'xcxccxc_QualityGate',
                                sonarLanguage: 'dotnetcore',
                                dotnetWindowsBuild: false,
                                sonarExclusions:'**/*.css,**/*.js,wwwroot/lib/**',
                                msSolutionPath: ['sfsfsfdsfsf.sln']
                            )
                        }
                    }
                }

Appreciate your help!!!

Hi,

The docs should help.

 
Ann

I Saw the docs, from there only i can taken the build script
My question is how to add the sonar code coverage which shown in docs in my jenkins file as stated in sonar section.
I tried below

sonarOptions :[
-d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
]
but it’s giving the error

WorkflowScript: 97: illegal colon after argument expression;
solution: a complex label expression before a colon must be parenthesized @ line 97, column 51.
sonarOptions :[�-d:sonar.cs.

can you please suggest me the how to add the script

stage('Scans') {
            parallel {
                stage('Sonar Analysis') {
                    agent {
                        kubernetes {
                            yaml easyAgents(easyTemplate: 'dotnetcore6')
                        }
                    }
                    steps {
                        container('dotnetcore6') {
                            sonarScan(
                                projectKey: 'xxccxcxxcx',
                                applicationName: "${APPLICATION_NAME}",
                                projectVersion: "1.0.0",                                
                                sonarQualityGate: 'xcxccxc_QualityGate',
                                sonarLanguage: 'dotnetcore',
                                dotnetWindowsBuild: false,
                                sonarExclusions:'**/*.css,**/*.js,wwwroot/lib/**',
                                msSolutionPath: ['sfsfsfdsfsf.sln']
                            )
                        }
                    }
                }

Hi,

Did you see the sample pipeline for using SonarScanner for .NET in the docs I linked to?

 
Ann