1
votes

Is there any way to break Jenkins build when Sonar quality gate fails with waitForQualityGate() method along with Sonar Scanner for MSbuild? I could not find any documentation for the same. All I could find is the usage of waitForQualityGate() along with Sonar scanner, but the general sonar scanner is not recommended for MSbuild projects.

The below mentioned link does not talk about usage waitForQualityGate with MSBuild. https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

That documentation talks about Sonar Scanner, but I am referring to Sonar scanner for MSbuild which is a different scanner altogether. The way I use this scanner is as shown below.

void beginSonarMSBuild(String VERSION){
    stage('Begin Sonar Analysis') {
    def MSBuildScannerHome = tool 'sonar-scanner-msbuild-3.0.0.629';
    withSonarQubeEnv('civil sonar') {
    bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe begin /k:mcdc 
    /n:mc-design-converter /v:${VERSION}.$BUILD_NUMBER /d:sonar.sourceEncoding=UTF-8 
    }
  }
}
void build(){  
    stage ('Build'){
    bat "Nuget restore SOMEHTING.sln"
    bat "MSBuild.exe SOMETHING.csproj "
   }
}
void endSonarMSBuild(){
    stage ('Complete Sonar Analysis'){
    def MSBuildScannerHome = tool 'sonar-scanner-msbuild-3.0.0.629';
    bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe end"
}
}

Now when I use waitforqualitygate() with beginSonarMSBuild(String VERSION)as shown below:

void beginSonarMSBuild(String VERSION){
    stage('Begin Sonar Analysis') {
    def MSBuildScannerHome = tool 'sonar-scanner-msbuild-3.0.0.629';
    withSonarQubeEnv('civil sonar') {
    bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe begin /k:mcdc 
    /n:mc-design-converter /v:${VERSION}.$BUILD_NUMBER /d:sonar.sourceEncoding=UTF-8 
    }
  }
    stage("Quality Gate"){
      timeout(time: 1, unit: 'MINUTES') {
      def qg = waitForQualityGate()
        if (qg.status != 'OK') {
            error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
    }
}

void build(){
scripts here...
}
void endSonarMSBuild(){
scripts here...
}

I get this error msg java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.

Also I get the same error when I use waitForQualityGate() with endSonarMSBuild() step as shown below.

void beginSonarMSBuild(String VERSION){
stage('Begin Sonar Analysis') {
scripts here...
}

void build(){
scripts here... 
}

void endSonarMSBuild(){
stage ('Complete Sonar Analysis'){
def MSBuildScannerHome = tool 'sonar-scanner-msbuild-3.0.0.629';
bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe end"
}
stage("Quality Gate"){
  timeout(time: 1, unit: 'MINUTES') {
  def qg = waitForQualityGate()
    if (qg.status != 'OK') {
        error "Pipeline aborted due to quality gate failure: ${qg.status}"
    }
   }
  }
 }

So the question I have is, does Sonar scanner for MSBuild even support waitForQualityGate(), if yes, then how to use the same?

1
I don't really get your question. The buil automatically fails when the code does not pass the sonar quality gate. And waitForQualityGate() is the right method for that. - arifCee
But where to use waitForQualityGate()? as part of SonarQube.Scanner.MSBuild.exe begin or SonarQube.Scanner.MSBuild.exe end step? Please note that sonar scanner is different from Sonar scanner for MSbuild - amal jayaraj

1 Answers

1
votes

On the documentation, the example is made with the scanner for Maven but it should work fine with any scanner as long as you wrap it in a withSonarQubeEnv step.

For the scanner for MSBuild, it is important to wrap the end step (but wrapping the begin step is also a good idea to automatically pass credentials.

void beginSonarMSBuild(String VERSION) {
    stage('Begin SonarQube Analysis') {
        def MSBuildScannerHome = tool 'sonar-scanner-msbuild-3.0.0.629';
        withSonarQubeEnv('civil sonar') {
            bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe begin /k:mcdc 
    /n:mc-design-converter /v:${VERSION}.$BUILD_NUMBER /d:sonar.sourceEncoding=UTF-8 
        }
    }
}
void build() {  
    stage ('Build') {
        bat "Nuget restore SOMEHTING.sln"
        bat "MSBuild.exe SOMETHING.csproj"
   }
}
void endSonarMSBuild() {
    stage ('Complete SonarQube Analysis') {
        withSonarQubeEnv('civil sonar') {
            def MSBuildScannerHome = tool 'sonar-scanner-msbuild-3.0.0.629';
            bat "${MSBuildScannerHome}\\SonarQube.Scanner.MSBuild.exe end"
        } // Will collect task id
    }
    stage("Quality Gate"){
        timeout(time: 1, unit: 'MINUTES') {
            def qg = waitForQualityGate()
            if (qg.status != 'OK') {
              error "Pipeline aborted due to quality gate failure: ${qg.status}"
            }
        }
    }
}