2
votes

I'd like to run SonarQube Scanner from a Jenkins pipeline and I followed the documentation.

Regarding the error, it seems that the scanner is present but some commands are not found. My jenkins instance runs in a docker.

Jenkins version : 2.46.1

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

2 Answers

2
votes

In the sonar-scanner script, there is this block

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.

As a workaround, I set the env variable JAVA_HOME.

0
votes

Make sure the PATH is complete, or check if resetting it is enough

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:

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