基于 Sonar 在 Jenkins 上的实战操作

本次线上技术分享我将分享与介绍:基于 Sonar 在 Jenkins 上的实战操作

1、一笔带过简介一下 Sonar 的应用场景。

2、Sonar 插件在 Jenkins 上的配置。

3、Sonar 在 Jenkins 上的实战配置。

[root@i-0mug]#
[root@i-0mug]# wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/2.1.0/sonar-gitlab-plugin-2.1.0.jar
–2020-03-07 21:46:35-- https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/2.1.0/sonar-gitlab-plugin-2.1.0.jar
Resolving github.com (github.com)… 13.229.188.59
Connecting to github.com (github.com)|13.229.188.59|:443… connected.
HTTP request sent, awaiting response… 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/58255049/f15c9912-8f29-11e7-9844-aa353e71a80d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200307%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200307T134521Z&X-Amz-Expires=300&X-Amz-Signature=2d6503b7ad556ac4d27fd6e99dc0cf1889e9b4daa2720b7611c5cadc5c0bf653&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dsonar-gitlab-plugin-2.1.0.jar&response-content-type=application%2Foctet-stream [following]
–2020-03-07 21:46:35-- https://github-production-release-asset-2e65be.s3.amazonaws.com/58255049/f15c9912-8f29-11e7-9844-aa353e71a80d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200307%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200307T134521Z&X-Amz-Expires=300&X-Amz-Signature=2d6503b7ad556ac4d27fd6e99dc0cf1889e9b4daa2720b7611c5cadc5c0bf653&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dsonar-gitlab-plugin-2.1.0.jar&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)… 52.216.142.92
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.142.92|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 3293347 (3.1M) [application/octet-stream]
Saving to: ‘sonar-gitlab-plugin-2.1.0.jar’

100%[=============================================================================================================================================>] 3,293,347 99.0KB/s in 40s

2020-03-07 21:47:20 (79.9 KB/s) - ‘sonar-gitlab-plugin-2.1.0.jar’ saved [3293347/3293347]

[root@i-0mug]#

[root@i-0mug]#
[root@i-0mug]# wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/3.0.1/sonar-gitlab-plugin-3.0.1.jar
–2020-03-08 16:39:59-- https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/3.0.1/sonar-gitlab-plugin-3.0.1.jar
Resolving github.com (github.com)… 52.74.223.119
Connecting to github.com (github.com)|52.74.223.119|:443… connected.
HTTP request sent, awaiting response… 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/58255049/c8679ac8-3273-11e8-9515-cf76c78abbea?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200308%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200308T084000Z&X-Amz-Expires=300&X-Amz-Signature=084b76e14fdf96fe6b34119c8242152f8d89cba3ef1dbe0eb62e33a0993bcb0d&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dsonar-gitlab-plugin-3.0.1.jar&response-content-type=application%2Foctet-stream [following]
–2020-03-08 16:40:00-- https://github-production-release-asset-2e65be.s3.amazonaws.com/58255049/c8679ac8-3273-11e8-9515-cf76c78abbea?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200308%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200308T084000Z&X-Amz-Expires=300&X-Amz-Signature=084b76e14fdf96fe6b34119c8242152f8d89cba3ef1dbe0eb62e33a0993bcb0d&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dsonar-gitlab-plugin-3.0.1.jar&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)… 52.216.112.139
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.112.139|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 7859547 (7.5M) [application/octet-stream]
Saving to: ‘sonar-gitlab-plugin-3.0.1.jar’

3% [====> ] 287,672 7.45KB/s eta 17m 0s
[root@i-0mug]#
[root@i-0mug]#

mav 执行构建指令,代码编译成功。

最后 SonarQube 扫描分析代码质量的时候,报错了。

扫描默认 master 分支,没有任何问题,也不会报错,但是切换研发分支进行扫描,就一直报这个错,无法分析分支代码质量。

有兴趣的话,可以一起写一个 Jenkins 和 Sonarqube 的交互式教程
https://github.com/jenkins-zh/jenkins-zh-courses/issues/10

这好事啊,可以着手写写。

有时候扫描指定分支,偶尔还会报错,无法分析分支质量信息,这问题已完美解决,记录一下。

上图是正常的扫描分支信息

1赞

下图就是扫描分支时报的错,无法分析分支的代码质量。

不过遇到上面的错,可以按下面的方式完美的解决错误。

可以直接使用指令执行分支扫描,扫描完成发布质量报告到 SonarQube 服务器上即可。

上图所示添加一段指令进行扫描分析,代码片段如下:

#!/bin/bash
ymd=date +%Y%m%d_%H:%M:%S;
mvn sonar:sonar -Dsonar.projectKey=caauth -Dsonar.host.url=http://101.139.29.153:39000 -Dsonar.login=964cb251ef0d0b325c7f23be7c7553d2cdead65d -Dsonar.branch.name=dev3.1 -Dsonar.sourceEncoding=UTF-8 -Dsonar.language=java -Dsonar.projectVersion=1.0

上面的指令执行完成,如下图的结果显示。

说明分支扫描已完成,质量报告也已生成。

SonarQube 服务器上显示分支信息如下