带有用于 MSbuild 的 Sonar 扫描仪的 waitForQualityGate()

waitForQualityGate() with Sonar scanner for MSbuild

当 Sonar 质量门通过 waitForQualityGate() 方法以及 Sonar Scanner for MSbuild 失败时,是否有任何方法可以破坏 Jenkins 构建?我找不到相同的任何文档。我所能找到的只是 waitForQualityGate() 与声纳扫描仪的用法,但不建议将一般声纳扫描仪用于 MSbuild 项目。

下面提到的link没有讲waitForQualityGate与MSBuild的用法。 https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

该文档讨论的是 Sonar 扫描仪,但我指的是适用于 MSbuild 的 Sonar 扫描仪,它是完全不同的扫描仪。我使用这个扫描仪的方式如下图

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"
}
}

现在,当我将 waitforqualitygate()beginSonarMSBuild(String VERSION) 一起使用时,如下所示:

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...
}

我收到此错误消息 java.lang.IllegalStateException: Unable to get SonarQube task id and/or server name. Please use the 'withSonarQubeEnv' wrapper to run your analysis.

当我使用 waitForQualityGate()endSonarMSBuild() 步骤时,我也会遇到同样的错误,如下所示。

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}"
    }
   }
  }
 }

所以我的问题是,MSBuild 的 Sonar 扫描器甚至支持 waitForQualityGate(),如果支持,那么如何使用它?

在文档中,该示例是使用 Maven 的扫描器制作的,但只要您将其包装在 withSonarQubeEnv 步骤中,它应该适用于任何扫描器。

对于 MSBuild 的扫描器,包装结束步骤很重要(但包装开始步骤也是自动传递凭据的好主意。

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}"
            }
        }
    }
}