由于未找到命令,SonarQube扫描程序在Jenkins管道中失败

时间:2017-04-26 20:19:27

标签: jenkins sonarqube jenkins-pipeline sonarqube-scan

我想从Jenkins管道运行SonarQube扫描仪,然后我按照文档进行操作。

关于错误,似乎扫描仪存在但找不到某些命令。我的jenkins实例在一个docker中运行。

詹金斯版本:2.46.1

SonarQube扫描仪:2.6.1

+ /var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner
/var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner: line 56: which: command not found
/var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube_Scanner/bin/sonar-scanner: line 66: exec: : not found

2 个答案:

答案 0 :(得分:2)

sonar-scanner脚本中,有这个块

if [ -n "$JAVA_HOME" ]
then
  java_cmd="$JAVA_HOME/bin/java"
else
  java_cmd="$(which java)"
fi

鉴于我的JAVA_HOME未设置,脚本调用which并且命令未安装在我的容器中。

作为解决方法,我设置了env变量JAVA_HOME

答案 1 :(得分:0)

确保PATH完成,或检查重置是否足够

def sonarqubeScannerHome = tool name: 'SonarQubeScanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
withEnv(["PATH=/usr/bin: ..."]) {
  // Your call to Sonar
  sh "${sonarqubeScannerHome}/bin/sonar-scanner -e -Dsonar.host.url=..."
}

我使用了来自" Execute SonarQube Scanner within Jenkins 2 Pipeline"的设置,但使用了Sonar 2.5,there is an official support of Jenkins pipeline

def scannerHome = tool 'SonarQube Scanner 2.8';
withEnv(["PATH=/usr/bin: ..."]) {
  withSonarQubeEnv('My SonarQube Server') {
    sh "${scannerHome}/bin/sonar-scanner"
  }
}