问题
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
?)
回答1:
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 '
}
回答2:
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
来源:https://stackoverflow.com/questions/49257285/sonarqube-jenkins-git-integration-when-cloning-to-a-subdirectory