Perplexed by directory and home issues when using Sonar-Scanner with Jenkins on Windows

I am running SonarQube and Sonar-Scanner on a Windows host. This Windows host is also a Jenkins worker.

When I add the SonarQube stages to my Jenkinsfile it causes several changes to occur and does not function properly.

		stage('SonarQube Analysis') {
			steps {
				withSonarQubeEnv(credentialsId: 'sonarqube-webhook', installationName: 'SonarQube') {
				bat '''
				    %scannerHome%\\bin\\sonar-scanner.bat -X 
				'''
				}
			}
		}
		stage('Wait for gate') {
            steps {
                script {
                    waitForQualityGate webhookSecretId: 'sonarqube-webhook', abortPipeline: true
                }
            }
        }

Once this is added I start getting build steps occurring in C:\Users\Sonarqube which confuses me. There is on reason for the build to be impacted, the SonarQube stages have not even been executed when this occurs. Also, I do not know why it would be using C:\Users\Sonarqube, this user has absolutely no relation to the pipeline which runs as the Jenkins user.

_POC:
  Running POC on Application.h/hpp
  cmd.exe /C "C:\Users\sonarqube\AppData\Local\Temp\tmpdffffffffffffffffffffffffffffffffffff.cmd"
  .\poc.exe  Source_Code\Properties\Application.hpp -o ".\Generated\poc_Application.cpp"

After adding Debug to the Sonar-Scanner executable I can see that it too is using the C:\Users\Sonarqube directory

12:32:56.407 INFO  SonarScanner CLI 6.2.1.4610
12:32:56.411 INFO  Java 17.0.12 Eclipse Adoptium (64-bit)
12:32:56.413 INFO  Windows 10 10.0 amd64
12:32:56.429 DEBUG Scanner max available memory: 7 GB
12:32:56.465 DEBUG Create: C:\Users\sonarqube\.sonar\cache
12:32:56.466 INFO  User cache: C:\Users\sonarqube\.sonar\cache
12:32:56.467 DEBUG Create: C:\Users\sonarqube\.sonar\cache\_tmp

The first thing I want to fix is not using C:\Users\Sonarqube for anything, how do I specify the directory used for Sonar-Scanner on a Windows host?

Hey there.

Three things stand out to me.

I’m rather surprised to see %scannerHome% rather than ${scannerHome} which has been in all the tutorial examples for quite some time. Where are you getting your example from?

I’m also surprised to not see a line like this in your piepline:

def scannerHome = tool '<sonarqubeScannerInstallation>'

To define where the sonar-scanner should be found (matching a tool installation as documented here). Maybe the job is instead picking up a SonarScanner installation you already have under the SonarQube user?

Thanks for the reply, I have made a lot more headway and gotten this working properly but the odd issue persists with it using the Sonarqube user directory.

pipeline {
    agent {
        label 'WORKER0'
    }
    environment { 
        scannerHome = tool name: 'sonar-scanner'
    }
    stages {
		stage('SonarQube Analysis') {
			steps {
				withSonarQubeEnv(credentialsId: 'sonarqube-project', installationName: 'SonarQube') {
				bat '''
				    "%scannerHome%\\bin\\sonar-scanner.bat"
				'''
				}
			}
		}
		stage('Wait for gate') {
            steps {
                waitForQualityGate(webhookSecretId: 'sonarqube-webhook', abortPipeline: true)
            }
        }
    }
}

I am able to scan, push results, and succeed in my waitgate but C:\Users\SonarQube is still referenced in the logs

I also can make this work without scannerHome, but still have the user issue. This makes me certain it is not defaulting to a scanner that was installed with the webserver

bat '''
    "C:\\SonarQube\\Scanner\\sonar-scanner-6.2.1.4610-windows-x64\\bin\\sonar-scanner.bat" -Dsonar.projectKey=PROJ -Dsonar.sources=. -Dsonar.host.url=https://contoso.com -Dsonar.token=TOKEN
'''

Can you print the value of scannerHome in your pipeline? I would be curious to see what it resolves to.

Hi, it does successfully print C:\SonarQube\Scanner\sonar-scanner-6.2.1.4610-windows-x64\

I found allusions to a user_home variable in user posts, but see no such variable in documentation.

You can indeed adjust SONAR_USER_HOME (documented here).

I still don’t know why it would default to C:\Users\sonarqube though, unless the agent is running at that user.

I suppose it would be difficult to test in production, but on a test server, I’d want to know what happens if this user doesn’t exist or is named something else. As this user says in the Jenkins community:

Jenkins will start things under the user it is itself running when it runs on the controller. Or as the user the agent is running as. Jenkins does not perform user switching.