简体   繁体   English

由于找不到命令,SonarQube Scanner在Jenkins管道中失败

[英]SonarQube Scanner fails in a Jenkins pipeline due to command not found

I'd like to run SonarQube Scanner from a Jenkins pipeline and I followed the documentation. 我想从Jenkins管道运行SonarQube Scanner,然后按照文档进行操作。

Regarding the error, it seems that the scanner is present but some commands are not found. 关于错误,似乎存在扫描仪,但未找到某些命令。 My jenkins instance runs in a docker. 我的jenkins实例在docker中运行。

Jenkins version : 2.46.1 Jenkins版本:2.46.1

SonarQube Scanner : 2.6.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

In the sonar-scanner script, there is this block sonar-scanner脚本中,存在此块

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

And given that my JAVA_HOME was unset, the script called which and the command is not installed inside my container. 鉴于未设置我的JAVA_HOME ,该脚本调用了which并且该命令未安装在我的容器中。

As a workaround, I set the env variable JAVA_HOME . 解决方法是,设置环境变量JAVA_HOME

Make sure the PATH is complete, or check if resetting it is enough 确保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=..."
}

I used the setup from " Execute SonarQube Scanner within Jenkins 2 Pipeline ", but with Sonar 2.5, there is an official support of Jenkins pipeline : 我使用了“ Jenkins 2管道内的Execute SonarQube扫描仪 ”中的设置,但是使用Sonar 2.5, Jenkins管道得到了官方支持

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM