我已经设置 Jenkins 来自动构建我项目的几个 git 分支。构建还会触发声纳分析,作为构建后的操作。
问题是所有分支都指向同一个 Sonar 项目。我知道有一个 sonar.branch 属性。是否可以让 Jenkins 自动将 sonar.branch 属性设置为正在构建的当前 git 分支(无需更改项目的 pom.xml)?
You can try adding -Dsonar.branch.name=something
in the field MAVEN_OPTS in jenkins post-build action (Advanced configuration).
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
.
I didn't try, so i'm not sure it will work.
In a Jenkins multi-branch pipeline, the variable is 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
.
这应该工作
mvn sonar:sonar -U -Dsonar.branch.name=$BRANCH_NAME
我不确定这是否有助于您自动执行 Jenkins,但是要运行具有正确分支集的 mvn sonar:sonar,如果您在基于 Unix 的环境中,您可以运行命令(注意反引号):
mvn -Dsonar.branch=`git rev-parse --abbrev-ref HEAD` sonar:sonar
您是否使用构建参数来指定分支?
如果是,您可以轻松地告诉 Sonar 它是哪个分支,只需在配置文本区域中添加:
sonar.branch=${branch}
其中 branch 是构建参数的名称。