在 Jenkins 中使用多个 git 分支进行声纳分析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15596522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sonar analysis using multiple git branches in Jenkins
提问by spyk
I have setup Jenkins to automatically build several git branches of my project. The build also triggers Sonar analysis, as a post-build action.
我已经设置 Jenkins 来自动构建我的项目的几个 git 分支。构建还会触发声纳分析,作为构建后的操作。
The problem is that all branches point to the same Sonar project. I know that there is a sonar.branch property. Is it possible to have Jenkins automatically set sonar.branch property to the current git branch being built (without having to change project's pom.xml)?
问题是所有分支都指向同一个 Sonar 项目。我知道有一个 sonar.branch 属性。是否可以让 Jenkins 自动将 sonar.branch 属性设置为当前正在构建的 git 分支(无需更改项目的 pom.xml)?
回答by ben75
You can try adding -Dsonar.branch.name=something
in the field MAVEN_OPTS in jenkins post-build action (Advanced configuration).
您可以尝试-Dsonar.branch.name=something
在 jenkins 构建后操作(高级配置)中添加字段 MAVEN_OPTS。
I don't know how to resolve something
, since I don't know how you configure your jenkins job... but it will probably be something like $git.branch
.
我不知道如何解决something
,因为我不知道你如何配置你的 jenkins 工作......但它可能类似于$git.branch
.
I didn't try, so i'm not sure it will work.
我没有尝试,所以我不确定它会起作用。
In a Jenkins multi-branch pipeline, the variable is BRANCH_NAME
.
在 Jenkins 多分支管道中,变量是BRANCH_NAME
.
P.S.While reading your question it's difficult to say if you are using maven or not to trigger sonar. If you are not using maven: there is a project properties
field in the Jenkins config where you can define sonar.branch
.
PS在阅读您的问题时,很难说您是否使用 maven 来触发声纳。如果您不使用 maven:Jenkins 配置中有一个project properties
字段,您可以在其中定义sonar.branch
.
回答by Gnani
This should work
这应该工作
mvn sonar:sonar -U -Dsonar.branch.name=$BRANCH_NAME
回答by JeanMarc
I am not sure if this helps you with the automated Jenkins execution, but to run a mvn sonar:sonar with the proper branch set, you can run the command (note the back-quotes), if you are in a Unix based environment:
我不确定这是否可以帮助您自动执行 Jenkins,但是要使用正确的分支集运行 mvn sonar:sonar,如果您在基于 Unix 的环境中,您可以运行命令(注意反引号):
mvn -Dsonar.branch=`git rev-parse --abbrev-ref HEAD` sonar:sonar
回答by Thomas Decaux
Did you use build parameters in order to specify the branch?
您是否使用构建参数来指定分支?
If yes, you can easily tell to Sonar which branch is it, juste add in config textarea:
如果是,你可以很容易地告诉声纳它是哪个分支,只需在配置文本区域中添加:
sonar.branch=${branch}
Where branch is the name of the build parameter.
其中 branch 是构建参数的名称。