0
votes

In the process of getting up to speed with Jenkins pipelines I'm converting some old builds which use the FreeStyle type projects to using Pipelines. The old Freestyle build works well.

My builds are normally done within a container environment (spun up for just that build, over SSH) and ends with a SonarQube analysis for QA.

The sonarqube step (using Jenkins SonarQube plugin) automatically injects the sonarqube scanner with correct arguments into the container to connect to the SonarQube server as seen in this example from the console logs of the Jenkins buid:

22:47:13 Unpacking https://repo1.maven.org/maven2/org/sonarsourc/scanner/cli/sonar-scanner-cli/3.3.0.1492/sonar-scanner-cli-3.3.0.1492.zip to /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Aditus_SonarQube on docker-000b7eccw94td on Docker on master
22:47:13 [g7ctrl-server] $ /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Aditus_SonarQube/bin/sonar-scanner -Dsonar.host.url=https://myserver.com/sonarqube ******** -Dproject.settings=/home/jenkins/workspace/g7ctrl-server/sonar-project.properties -Dsonar.projectBaseDir=/home/jenkins/workspace/g7ctrl-server
22:47:14 INFO: Scanner configuration file: /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Aditus_SonarQube/conf/sonar-scanner.properties
22:47:14 INFO: Project root configuration file: /home/jenkins/workspace/g7ctrl-server/sonar-project.properties
22:47:14 INFO: SonarQube Scanner 3.3.0.1492

Having read Analyzing in a Jenkins pipeline I conclude that it does not apply since the Scanner is not installed. Also reading Execute SonarQube Scanner within Jenkins 2 Pipeline since it assumes the same.

Is there any automation (equivalent to FreeStyle build step) or do I need to write shell-script to download, unpack, and install the scanner manually into the container?

Would appreciate if someone could shed a light on this? (or some best-practice examples - since I'm a rookie on pipeline scripting) I would assume others have faced this issue as well)

Update: (Almost Solved)

Using the snippet generator one could setup standard tools

stage('SonarQube analysis') {
  def sonarqubeScannerHome = tool name: 'Aditus SonarQube', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
  withSonarQubeEnv('Aditus SonarQube') {
    sh "${sonarqubeScannerHome}/bin/sonar-scanner -Dsonar.host.url=https://myserver.com/sonarqube -Dproject.settings=/home/jenkins/workspace/g7ctrl-server/sonar-project.properties -Dsonar.projectBaseDir=/home/jenkins/workspace/g7ctrl-server"
  }
}

Defining the tool will trigger Jenkins to download, unpack and install the Scanner Tool. However, despite the project setting parameters being the same as in the freestyle build (that works) the scanner believes the root file is not set as can be seen from the logs

Unpacking https://repo1.maven.org/maven2/org/sonarsource/scanner/cli/sonar-scanner-cli/3.3.0.1492/sonar-scanner-cli-3.3.0.1492.zip to /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Aditus_SonarQube on docker-000dsh7hwihbw on Docker on master
[Pipeline] sh
+ /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Aditus_SonarQube/bin/sonar-scanner -Dsonar.host.url=https://myserver.com/sonarqube -Dproject.settings=/home/jenkins/workspace/g7ctrl-server/sonar-project.properties -Dsonar.projectBaseDir=/home/jenkins/workspace/g7ctrl-server
INFO: Scanner configuration file: /home/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Aditus_SonarQube/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE

Most likely there is a path problem in my lack of mastery of the pipeline scripts that I fail to see. (The scanner is called with identical parameters in both Freestyle & Pipeline case)

1
Just out of curiosity, in withSonarQubeEnv could you just check if the properties file is available in the context? Like just sh "cat YOUR_FILE"?hakamairi

1 Answers

0
votes

SOLVED

Since the workspace path is dependent on the jenkins project name the path was wrong. When I setup the scanner parameters I simply copied from my (working) freestyle project with the name of the original Jenkins project in the path and not the new pipeline project name.

A complete pipeline stage for downloading, installing and running the scanner can hence simply be written as:

stage('SonarQube analysis') {
    def sonarqubeScannerHome = tool name: '<SONARQUBE-TOOL-NAME>'
    sh "${sonarqubeScannerHome}/bin/sonar-scanner"
}

Where SONARQUBE-TOOL-NAME must be the same name as you have given to the sonar tool in the Global Tool Configuration in Jenkins (the tool configuration also determines what version and how to get the tool)

If you run behind a reverse proxy you might have an issue finding the SonarQube server (especially if you run in a container) if it only has a local Bonjour name in which case you need to add the server address as parameter, also, if a non-default name (or path) is used for the project properties is needs to be added. So with parameters the script is

stage('SonarQube analysis') {
    def sonarqubeScannerHome = tool name: '<SONARQUBE-TOOL-NAME>'
    sh "${sonarqubeScannerHome}/bin/sonar-scanner -Dsonar.host.url=https://<SONAR-QUBE-SERVER> -Dproject.settings='sonar-project.properties' -Dsonar.projectBaseDir=."
}

Note: Depending on the configuration the path to the project.settings file must be configured accordingly.

Credit: Thanks to hakamairi who got me revisiting my path assumptions.