Jenkins集成sonar检测指定分支

时间:2022-07-22
本文章向大家介绍Jenkins集成sonar检测指定分支,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

问题描述

当我们使用jenkins集成sonar做代码检测时,实际上是通过maven的插件,执行mvn打包命令时指定sonar来进行静态代码检测,这种方式进行检测时,有一个问题,那就是sonar的界面上并没有显示检测时的分支,而是master分支,因此会造成代码检测报告不是很准确!

解决办法

下载sonar上显示分支的插件

该插件的github地址:
https://github.com/mc1arke/sonarqube-community-branch-plugin/releases

然后根据文档将地址拷贝到指定目录

Either build the project or download a compatible release version of the plugin JAR. Copy the plugin JAR file to the extensions/plugins/ and the lib/common/ directories of your SonarQube instance and restart SonarQube.

cp sonarqube-community-branch-plugin-1.2.0.jar lib/common/
cp sonarqube-community-branch-plugin-1.2.0.jar extensions/plugins/

重启sonar

./bin/linux-x86-64/sonar.sh restart

修改Jenkins的pipeline

mvn package install -Dmaven.test.skip=true sonar:sonar -Dsonar.branch.name=${Branch} -Dsonar.projectKey=${app_name}

增加: -Dsonar.branch.name=${Branch} 这一段即可,再次构建便会看到界面上以显示正常