Jenkins Pipline使用SonarScanner 检查 VUE、js 项目 中遇到的Bug

在 Jenkins 上使用 Pipline 进行集成,利用 SonarScanner 做静态代码扫描的过程中,遇到了几个问题,这里记录了一点解决办法。

在Jenkins上使用Pipline进行集成,利用SonarScanner做静态代码扫描的过程中,遇到了几个问题,这里记录了一点解决办法。

1. 脚本变量定义错误:

自动安装 SonarScanner 插件,在 Pipline 脚本中使用官方 wiki 中说明的脚本:

stage('SonarQube analysis') {
    // requires SonarQube Scanner 2.8+
    def scannerHome = tool 'SonarQube Scanner 2.8';
    withSonarQubeEnv('My SonarQube Server') {
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }

但是在扫描的时候总是报错,可以看出是语法错误:

WorkflowScript: 33: Not a valid stage section definition: "def sonarqubeScannerHome = 'SonarQubeScanner2.8'". Some extra configuration is required. @ line 33, column 9.
           stage('SonarQubeanalysis') {
           ^
WorkflowScript: 33: Unknown stage section "withSonarQubeEnv". Starting with version 0.5, steps in a stage must be in a steps block. @ line 33, column 9.
           stage('SonarQubeanalysis') {
           ^
WorkflowScript: 33: No "steps" or "parallel" to execute within stage "SonarQubeanalysis" @ line 33, column 9.
           stage('SonarQubeanalysis') {
           ^

因为在 stage 里面不是用 def 来定义变量的,于是尝试将变量定义放在脚本里,问题得到了解决:

stage('SonarQubeanalysis') {
            steps{
                script {
                    scannerHome = tool 'SonarQubeScanner2.8'
                    //这里这个tool是直接根据名称,获取自动安装的插件的路径
                }
            withSonarQubeEnv('SonarQube') {
                sh "${scannerHome}/bin/sonar-scanner sonar.projectKey=YourProjectKey sonar.sources=."
            }
            }
        }

2. 变量引用错误:

在修改成上面的脚本后,运行再次报错,找不到 sonar.sources 这个选项:

/yourpath/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQubeScanner2.8/bin/sonar-scanner sonar.projectKey=YourProjectKey sonar.source=.
ERROR: Unrecognized option: sonar.source=.
INFO: 
INFO: usage: sonar-scanner [options]
INFO: 
INFO: Options:
INFO:  -D,--define <arg>     Define property
INFO:  -h,--help             Display help information
INFO:  -v,--version          Display version information
INFO:  -X,--debug            Produce execution debug output
INFO:  -i,--interactive      Run interactively

经过大神指导,发现在运行 SonarScanner 的时候,需要用‘-D’来标记(如果这两个参数是写在 sonar-scanner.properties 里就不需要了):

stage('SonarQubeanalysis') {
            steps{
                script {
                    scannerHome = tool 'SonarQubeScanner2.8'
                }
            withSonarQubeEnv('SonarQube') {
                sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=YourProjectKey -Dsonar.sources=."
            }
            }
        }

3. 登录受限:

在上面的问题都解决了之后,发现登录所需要属性错误:

ERROR: Error during SonarQube Scanner execution
ERROR: Not authorized. Please check the properties sonar.login and sonar.password.
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

出现这个 bug 的时候,用的是 SonarQube5.2 版本,所以需要 Jenkins 通过账户密码登录,但是因为账号权限受限,所以无法连接到 Sonar 的服务器,所以后来更新账号密码就好了。

或者使用

stage('SonarQube Analysis') {
            steps {
                  script {
                        scannerHome = tool 'SonarScanner'
                    }
                withSonarQubeEnv('sonarqube') {
                  sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=xrm_xrm_xxxx -Dsonar.sources=src -Dsonar.host.url=http://192.168.1.x:9000 \
  -Dsonar.login=sqp_xxxxe"
                }
            }
        }

也可以在项目下,创建一个sonar-project.properties文件,在里面配置好参数 (推荐)

#项目的唯一标识,必填
sonar.psonar.projectKey=sonar-test-prj
项目名称
sonar.projectName=sonar-test-prj
项目版本
sonar.projectVersion=1.0
#扫描目录
sonar.sources=src
#检测语言
sonar.language=js
#扫描结果上报地址
sonar.host.url=http://127.0.0.1:9000
#登录账号
sonar.login=admin
sonar.password=xxx
#文件编码
sonar.sourceEncoding=UTF-8

参考

https://blog.csdn.net/weixin_44802620/article/details/125133724
https://blog.csdn.net/qq_44930876/article/details/128147777

猜你喜欢

转载自blog.csdn.net/agonie201218/article/details/131664647