关于jenkins:waitar的质量与Sonar扫描仪的MSbuild

waitForQualityGate() with Sonar scanner for MSbuild

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

下面提到的链接不涉及MSBuild与waitForQualityGate的用法。
https://docs.sonarqube.org/display/SCAN/使用SonarQube Scanner进行Jenkins分析#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline

该文档讨论的是Sonar扫描仪,但我指的是MSbuild的Sonar扫描仪,它是完全不同的扫描仪。我使用此扫描仪的方式如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)一起使用时,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.

当我在endSonarMSBuild()步骤中使用waitforqualitygate()时也会出现相同的错误,如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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扫描程序,包装结束步骤很重要(但是包装开始步骤也是自动传递凭据的好主意。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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}"
            }
        }
    }
}