4

I have Jenkins' git plug-in clone my repository to a subdirectory of WORKSPACE. Accordingly, I set

sonar.sources=my/subdir

SCM detection doesn't seem to be informed by the above setting, however; and setting

sonar.scm.provider=git

…yields:

ERROR: Not inside a Git work tree: /path/to/my/workspace

How do I inform SonarQube's git plug-in of the location of my sources? (And why isn't it clever enough to use sonar.sources?)

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
Braden
  • 1,448
  • 10
  • 11
  • Did you ever get an answer for this? I'm experiencing the same thing. – dukethrash Feb 19 '19 at 19:31
  • Another call to see if you got an answer to this? – Ed Harrod Mar 15 '19 at 17:43
  • Here's an answer: https://stackoverflow.com/questions/62536527/sonarqube-scanner-fails-with-not-inside-a-git-worktree-error/62727475#62727475 – Puka Jul 04 '20 at 09:31
  • Does this answer your question? [SonarQube Scanner fails with “Not inside a git worktree” error](https://stackoverflow.com/questions/62536527/sonarqube-scanner-fails-with-not-inside-a-git-worktree-error) – Puka Jul 04 '20 at 09:31

2 Answers2

0

struggled with this for a full 5 hours. thougt i should share what worked for me.

make subdirectory name the same as the repo name and run mvn sonar commands from there

SonarQube uses a java library from Eclipse jgit for the git work and this checks if the sonar.projectBaseDir has .git folder and some other git files/folders inside among other things.

N.B when the jenkins Git SCM clones it puts the contents of the repo directly on the workspace no dir is created

Setup: SonarQube 7.7 Jenkins Declarative Pipeline

sample code:

checkout([$class: 'GitSCM', branches: [[name: branchName]], 
                doGenerateSubmoduleConfigurations: false, extensions: [ [$class: 'RelativeTargetDirectory', relativeTargetDir: "${env.artifactId}"]], 
                submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'credts', url:  'http://server.com/'+"${env.artifactId}"+'.git']]])


                    withSonarQubeEnv('Sonar Dev') {
                    sh 'cd ' + "${env.artifactId}" + ' && mvn  -U clean package sonar:sonar '
                }

  • "make subdirectory name the same as the repo name" - what do you mean? That's usually the case. How is it gonna help the scanner? I have this same setup and it does not work for me. – Mikhail Dec 05 '19 at 09:45
  • if your subdirectory path is 'my/subdir' and repo name is 'unibeautify' as in 'https://github.com/Unibeautify/unibeautify.git' then your path should be 'my/unibeautify' – kalabase lebelo Dec 06 '19 at 11:30
  • why: 1. N.B when the jenkins Git SCM clones it puts the contents of the repo directly on the workspace no dir is created 2. SonarQube uses a java library from Eclipse jgit for the git work and this checks if the sonar.projectBaseDir has .git folder and some other git files/folders inside among other things. – kalabase lebelo Dec 06 '19 at 11:31
0

You have to set the following property if your code is in a subdirectory

sonar.projectBaseDir

For example if your code was in mycodedir then you would do

-D"sonar.projectBaseDir=${WORKSPACE}/mycodedir"

When you do this you can then change the sources and tests parameters to references of the above directory e.g.

If you have source code in mycodedir/mysources and tests in mycodedir/tests

sonar.sources=./mysources
sonar.tests=./tests
R.Doherty
  • 41
  • 6