简体   繁体   中英

Sonar analysis using multiple git branches in Jenkins

I have setup Jenkins to automatically build several git branches of my project. The build also triggers Sonar analysis, as a post-build action.

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)?

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 .

PS 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

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.

为了实现这一点,我通常使用-Dsonar.branch.name=${env.CHANGE_BRANCH}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM