整体架构图

image.png

具体配置过程

安装sonarqube

  第一步:

sonarqube下载地址:http://www.sonarqube.org/downloads/解压后移动到/usr/local目录下

解压后压缩包如图:

image.png

第二部:配置环境变量

image.png

第三步:配置sonar.properties

[root@localhost local]# vim sonarqube-6.7.5/conf/sonar.properties 

image.png

3.启动SonarQube (重点,这块最tm坑了)

 SonarQube 启动不能用root启动,所以新建一个用户。必须先启动elasticsearch,才能在启动sonar。sonar文件目录里面有

 elasticsearch这个目录。(这块好好看着,我在这坑了两天。)

image.png

[esuser@localhost elasticsearch]# ./bin/elasticsearch -d     -d代表后台运行

netstat   -nultp  查看端口,能看到9200和9300证明就成功了。

访问:http://192.168.6.178:9200              能给你一个json的文件。

image.png

启动sonar(用esuser启动)

[esuser@localhost sonarqube-6.7.5]# ./bin/linux-x86-64/sonar.sh start

果能访问,先按插件在sonar,要不然没插件不行。

image.png

image.png

安装SonarQube Runner和配置

下载sonar-scanner包
//解压文件
//进入文件
//编辑文件
[root@localhost local]#unzip  sonar-scanner-cli-3.0.3.778-linux.zip
[root@localhost local]#mv sonar-scanner-cli-3.0.3.778-linux  sonar-scanner
[root@localhost local]# cd sonar-scanner
//修改配置文件
[root@localhost sonar-scanner]# vim conf/sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here
 
 
 
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
 
sonar.host.url=http://192.168.6.178:9000
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://192.168.6.178:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.login=admin
sonar.password=admin

为项目创建sonar-project.properties文件

在项目的根目录下创建文件

image.png

#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
dStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=246367
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
 //开始扫描,
[root@localhost sell]# /usr/local/sonar-scanner/bin/sonar-scanner

参考:https://blog.csdn.net/u012448904/article/details/81706283

image.png

配置使用说明

1、配置 GitLab 通用账户

image.png

2、配置 sonar-gitlab-plugin 插件

下载sonar-gitlab-plugin-4.0.0.jar到sonarqube-7.4\extensions\plug×××中

image.png

admin 登录 SonarQube,点击 配置 —> 通用配置 —> GitLab —> Reporting —> 设置 GitLab User Token 以及 GitLab url。  

image.png

sonar-scanner安装

下载

sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-ci-multi-runner-darwin-amd64

2)给gitlab-runner赋可执行权限  

sudo chmod +x /usr/local/bin/gitlab-runner

3)注册runner  

# gitlab-runner register

Please enter the gitlab-ci coordinator URL:
# 示例:http://gitlab.alibaba-inc.com/ci
Please enter the gitlab-ci token for this runner:
# xxxxxx
Please enter the gitlab-ci description for this runner:
# 示例:qd_api_runner
Please enter the gitlab-ci tags for this runner (comma separated):
# 示例:analyze
Whether to run untagged builds [true/false]:
# truePlease enter the executor: docker, parallels, shell, kubernetes, docker-ssh, ssh, virtualbox, docker+machine, docker-ssh+machine:# docker
Please enter the default Docker image (e.g. ruby:2.1):
# maven:3-jdk-8

说明: 1、gitlab ci的地址以及token,从你要配置该runner到哪个项目,就去gitlab下该项目首页右侧设置—》CI/CD Pipelines—》Specific Runners下可以找到。 2、gitlab-ci tags这个很重要,在项目构建流程yaml文件里面指定tag,就是匹配使用哪个tag的runner,这里我定义了analyze,回头再配置文件里面就指定这个tag。 3、executor:执行者可以有很多种,这里我们使用docker,方便构建执行。 4、Docker image:构建Docker image时填写的image名称,根据项目代码语言不同,指定不同的镜像。我这里项目是java语言的,所以我使用官方maven:3-jdk-8镜像。  

如图:

image.png

4)安装并启动gitlab-runner

gitlab-runner ×××tall
gitlab-runner start

5).检验安装状态

./bin/sonar-scanner -h

成功:

image.png

定义项目构建流程

项目的构建流程是由项目根目录的.gitlab-ci.yml文件控制的,关于gitlab-ci详细的配置文档可以查看 这里 , 以下是一个简单的Java Maven项目的例子.gitlab-ci.yml:  

stages:
  - analyze
sonar_analyze:
  stage: analyze
  script:
    - bash /home/free-sonar-scanner-3.2-linux/bin/sonar-scanner
  tags:
    - analyze

我们提交该文件到gitlab对应项目上去。

git add .gitlab-ci.yml
git commit -m "Add .gitlab-ci.yml"
git push origin

这个时候,我们从该项目的Pipelines选项卡下可以看到,有正在运行的刚新建的analyze的这个runner的pipelines了。点击进去可以看到控制台实时输出日志

image.png

每次提出commit的时候都会触发:

image.png

然后sonarqube界面中会有对应的数据:

image.png

添加阿里插件

制作sonar插件

  1. https://mvnrepository.com中查询sonar-pmd-plugin获取

    image.png

然后下载对应的jar------sonar-pmd-plugin-3.2.1.jar:

复制对应的jar到

image.png


2.将生成的插件放到/extensions/plug×××

3.重启sonar,需切到非root账号,先启动es服务, ./elasticsearch -d  再启动sonar服务./sonar.sh start

4.查看sonar和es服务日志是否正常

image.png


image.png

5.sonar规则增加了p3c,完工。

参考:https://www.jianshu.com/p/f1bddb5e235f

参考:https://cloud.tencent.com/developer/article/1010595?from=10680

https://www.jianshu.com/p/1e59cd5cddb5

https://cloud.tencent.com/developer/article/1010612

https://www.jianshu.com/p/78ec15d2f332

http://192.168.10.59:9000/coding_rules?languages=java&open=squid%3AS2757