title: waitForQualityGate() with Sonar scanner for MSbuild
body: <p>Is there any way to break Jenkins build when Sonar quality gate fails with <strong>waitForQualityGate()</strong> method along with <strong>Sonar Scanner for MSbuild</strong>? 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.</p>
  
  <p>The below mentioned link does not talk about usage waitForQualityGate with MSBuild.
  <a href="https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline" rel="nofollow noreferrer">https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline</a> </p>
  
  <p>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.</p>
  
  <pre><code>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"
  }
  }
  </code></pre>
  
  <p>Now when I use <code>waitforqualitygate()</code> with <code>beginSonarMSBuild(String VERSION)</code>as shown below: </p>
  
  <pre><code>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...
  }
  </code></pre>
  
  <p>I get this error msg <code>java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.</code></p>
  
  <p>Also I get the same error when I use <code>waitForQualityGate()</code> with <code>endSonarMSBuild()</code> step as shown below.</p>
  
  <pre><code>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}"
      }
     }
    }
   }
  </code></pre>
  
  <p>So the question I have is, does Sonar scanner for MSBuild even support <code>waitForQualityGate()</code>, if yes, then how to use the same?</p>