2

我正在尝试通过 Jenkins 声明性管道执行 sonarqube 分析。

当我在 jenkinfile 中使用以下命令执行 sonarqube 分析时,它可以工作。但是当我在 jenkinsfile 中使用下面的函数时,它给了我错误。但是我想使用 withSonarQubeEnv() 函数进行声纳分析,因为我需要使用“waitForQualityGate”。

withSonarQubeEnv('sonarqube')
{
}
stage("Sonarqube Analysis") {
 steps {
  container('ibmcloudcli') { 
    echo "Sonarqube Analysis."
    sh label: '', script: """
      /opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner   
     -Dsonar.host.url=XXXX                                       -Dsonar.login=XXXX
-Dsonar.sources=/home/jenkins/agent/workspace/${JOB_NAME}/src -Dsonar.projectBaseDir=/home/jenkins/agent/workspace/${JOB_NAME}/src  -Dsonar.projectKey=${JOB_NAME} 
-Dsonar.language=java 
-Dsonar.java.binaries=/home/jenkins/agent/workspace/${JOB_NAME}/target -Dsonar.junit.reportPaths=/home/jenkins/agent/workspace/${JOB_NAME}/target/surefire-reports 
-Dsonar.jacoco.reportPath=/home/jenkins/agent/workspace/${JOB_NAME}/target/site/jacoco/jacoco.xml 
   """

SonarQube Scanner 3.2.0.1227(安装在 Jenkins slave pod 中)

SonarQube 服务器 7.9.3

JENKINS_VERSION - 2.204.5

仅供参考:sonar-scanner.properties 文件不包含任何信息,所有信息均通过命令提供

并遇到以下问题:

Sonarqube Analysis.
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: sonarqube
[Pipeline] {
[Pipeline] sh
+ /opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner
INFO: Scanner configuration file: /opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 4.15.0-106-generic amd64
INFO: User cache: /root/.sonar/cache
INFO: SonarQube server 7.9.3
INFO: Default locale: "en_US", source code encoding: "US-ASCII" (analysis is platform dependent)
WARN: SonarScanner will require Java 11 to run starting in SonarQube 8.x
INFO: Load global settings
INFO: Load global settings (done) | time=119ms
INFO: Server id: XXXX
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=72ms
INFO: Load/download plugins (done) | time=373ms
INFO: Process project properties
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 2.205s
INFO: Final Memory: 8M/393M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
[Pipeline] }
WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeeded?
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // container
[Pipeline] }

这是 Jenkinsfile “声纳分析”阶段

stage("Sonarqube Analysis") {
           steps {
             container('ibmcloudcli') {   
                echo "Sonarqube Analysis."
                withSonarQubeEnv('sonarqube') {
                  sh '''/opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner
                        -Dsonar.host.url=http://XXXX:9000
                        -Dsonar.login=XXXX
            -Dsonar.sources=/home/jenkins/agent/workspace/${JOB_NAME}/src 
            -Dsonar.projectBaseDir=/home/jenkins/agent/workspace/${JOB_NAME}/src  
            -Dsonar.projectKey=myfirstproject
            //-Dsonar.organization=default-organization
            -Dsonar.projectName=myfirstproject
            -Dsonar.projectVersion=1.0.0-alpha
            -Dsonar.language=java 
            -Dsonar.sourceEncoding=UTF-8
            -Dsonar.java.binaries=/home/jenkins/agent/workspace/${JOB_NAME}/target
            -Dsonar.junit.reportPaths=/home/jenkins/agent/workspace/${JOB_NAME}/target/surefire-reports 
            -Dsonar.jacoco.reportPath=/home/jenkins/agent/workspace/${JOB_NAME}/target/site/jacoco/jacoco.xml  
            -X
            '''
                }           
         }    
           }
        }       

4

2 回答 2

0

请添加 jenkins Prepare SonarQube Scanner 环境必须为真。

于 2020-07-29T10:08:54.477 回答
0

您的扫描仪参数缺少反斜杠。因此 sh 会将它们视为单独的命令,而不是一个长命令。

stage("Sonarqube Analysis") {
           steps {
             container('ibmcloudcli') {   
                echo "Sonarqube Analysis."
                withSonarQubeEnv('sonarqube') {
                  sh '''/opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner \
                        -Dsonar.host.url=http://XXXX:9000 \
                        -Dsonar.login=XXXX \
                        -Dsonar.sources=/home/jenkins/agent/workspace/${JOB_NAME}/src \
                        -Dsonar.projectBaseDir=/home/jenkins/agent/workspace/${JOB_NAME}/src  \
                        -Dsonar.projectKey=myfirstproject \
                        //-Dsonar.organization=default-organization \
                        -Dsonar.projectName=myfirstproject \
                        -Dsonar.projectVersion=1.0.0-alpha \
                        -Dsonar.language=java  \
                        -Dsonar.sourceEncoding=UTF-8 \
                        -Dsonar.java.binaries=/home/jenkins/agent/workspace/${JOB_NAME}/target \
                        -Dsonar.junit.reportPaths=/home/jenkins/agent/workspace/${JOB_NAME}/target/surefire-reports \
                        -Dsonar.jacoco.reportPath=/home/jenkins/agent/workspace/${JOB_NAME}/target/site/jacoco/jacoco.xml \
                        -X
                  '''
                }
             }
           }
} 
于 2020-11-24T04:22:16.167 回答