waitForQualityGate()使用Sonar扫描仪进行MSbuild

时间:2017-07-20 06:32:04

标签: jenkins msbuild sonarqube sonarqube-scan

当Sonar质量门因 waitForQualityGate()方法以及 Sonar Scanner for MSbuild 而失败时,有没有办法打破Jenkins构建?我找不到任何相同的文档。我所能找到的就是使用waitForQualityGate()和Sonar扫描仪,但不推荐使用通用声纳扫描仪进行MSbuild项目。

下面提到的链接没有讨论使用waitForQualityGate和MSBuild。 https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

该文档涉及Sonar Scanner,但我指的是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(),如果是,那么如何使用相同的?

1 个答案:

答案 0 :(得分:1)

在文档中,示例是使用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}"
            }
        }
    }
}