Execute sonar scanner outside source code folder

I set up a local kubernetes cluster in Docker Desktop and installed SonarQube inside. I am comparing between the two ways sonar scanner can be invoked, docker image and the zip file. According to the documentation, the extracted executable from the zip file has to be run inside the root directory of the project I want to scan. Docker image doesn’t have that restriction.

I just want to know if it is actually not possible to run sonar scanner else where. I plan to invoke the sonar-scanner command programmatically so I need to be able to call that command anywhere. Should I have gone with docker image instead?

Thank you.

Hi again @zacktzeng ,

the location of the binary for the sonar-scanner-cli is not relevant at all, just the current working directory. if you want to invoke the scanner programmatically, you just need to make sure to change to the correct directory before issuing the scan command.

as an example, this would work

ls
docker-ce  grafana  sonar-flex

sonar-scanner \
  -Dsonar.projectKey=test \
  -Dsonar.sources=./grafana/ \
  -Dsonar.host.url=<your sonar host> \
  -Dsonar.login=<your sonar login>

while this would not

ls
docker-ce  grafana  sonar-flex
cd docker-ce

sonar-scanner \
  -Dsonar.projectKey=test \
  -Dsonar.sources=../grafana/ \
  -Dsonar.host.url=<your sonar host> \
  -Dsonar.login=<your sonar login>

Docker does have the same limitation but shifted to the volume mount point onside the container.

hope this helps :slight_smile:

To be fair, I don’t quite get the difference between the two. The first one is specifying a folder inside the current directory. The other method is going back a level and then choosing the same folder. Also, just to be sure, when I tested different analysis parameter, I found that setting the sonar.projectBaseDir to the absolute path of the project folder allows me to call sonar-scanner from anywhere. Is there any catch to using this option?

no there is none. the sonar.projectBaseDir just defaults to the current directory when you trigger the scanner-cli, resulting in the described behavior that you can not use a folder for your sources that is hierarchical speaking over the base dir.

I see that makes sense. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.