【问题标题】:SonarQube Scanner fails in a Jenkins pipeline command not foundSonarQube 扫描仪在找不到 Jenkins 管道命令中失败
【发布时间】:2021-10-29 23:49:02
【问题描述】:

我想从 Jenkins 管道运行 SonarQube Scanner,我按照文档进行操作。

关于错误,似乎存在扫描仪但未找到某些命令。我的 jenkins 实例在本地运行。

错误信息:

 + /usr/local/bin/sonar-scanner
/usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/bin/sonar-scanner: line 17: dirname: command not found
/usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/bin/sonar-scanner: line 18: basename: command not found
/usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/bin/sonar-scanner: line 28: dirname: command not found
File does not exist: //lib/sonar-scanner-cli-4.6.2.2472.jar
'/' does not point to a valid installation directory: /
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

脚本管道:

    node {
    stage('Preparation') {
        try{
            
        // Clone Project from bitbucket
        git url: 'https://test@bitbucket.org/test/test.git'
        
        }catch(err){
            echo err
        }
    }
    
stage('Sonarqube') {
  
      sh '/usr/local/bin/sonar-scanner'

       
}
    
    stage('end') {  
     echo "Success" 
    }
}

【问题讨论】:

  • 您的问题是sonar-scanner 而取决于在环境中找到正常的unix 命令。 dirnamebasename 应该在 /usr/bin 中。结果是它无法正确build the path 到二进制文件。检查echo $PATH 并修复。比安装 docker 更容易解决;但没有足够的信息。你是怎么安装cli的?
  • 另外,@VonC 的 answer to a different problem 和旧版本可以解决您的问题。

标签: jenkins sonarqube jenkins-pipeline sonarscanner


【解决方案1】:

您可以在管道中使用 docker 映像来访问声纳扫描仪,就像在脚本管道中一样:

node {
    stage('Preparation') {
        try {
            // Clone Project from bitbucket
            //git url: 'https://test@bitbucket.org/test/test.git'
        } catch(err) {
            echo err
        }
    }
    
    docker.image('sonarsource/sonar-scanner-cli').inside {
        stage('Sonarqube') {
            sh 'sonar-scanner'
        }
    }
    
    stage('end') {  
     echo "Success" 
    }
}

如果您没有安装 docker,您可以查看 scanner documentation 在管道期间或 jenkins 本身中安装它。

【讨论】:

  • 不确定您如何得出“Docker”是解决方案的结论......? OP 没有提到使用 docker。
  • 是的,但它是一个有效的解决方案,您的解决方案也可以工作。