0. 简介

最近一直苦于自己写的代码的开发的质量不高,急需要一种可以适用于C++代码的代码质量检测工具,这里发现SonarQube可以很好的适用于Ubuntu环境,并能够很好的与Gitlab兼容,从而保证代码的敏捷式开发。

1. SonarQube安装

  1. ubuntu中安装jdk
    详细过程可参考:https://blog.csdn.net/qq_26709459/article/details/80466239
  2. ubuntu中安装mysql
    详细过程可参考:https://blog.csdn.net/qq_26709459/article/details/79115723

    安装成功之后,新建数据库”sonarqube”(数据库名称也可自己定义)。

     # 创建用户 sonarqube
     sudo adduser sonarqube
     # 切换到sonarqube用户
     su sonarqube
    
  3. 下载并安装sonarQube
    注:建议运行内存在2G或2G已上。

    • 下载sonarQube
      下载地址:https://www.sonarqube.org/downloads/
      在这里插入图片描述
    • 这里我们需要选择Community版本下载、解压并安装
        sudo unzip sonarqube-7.3.zip
      
      在这里插入图片描述
    • 配置SonarQube
      编辑$SONAR_HOME/conf/sonar.properties文件

        sonar.jdbc.username=**your_name**
      
        sonar.jdbc.password=**your_password**
      
        sonar.jdbc.url=jdbc:mysql://localhost:3306/your_database_name?# 后续不用更改
      
        sonar.web.javaOpts=-Xmx1024m -Xms512m -XX:+HeapDumpOnOutOfMemoryError # 如果你想分配sonar更多的内存
      
        sonar.path.data=**/var/sonarqube/6_5_7/data  #数据路径,保证sonarquebe可以读写
      
        sonar.path.temp=**/var/sonarqube/6_5_7/temp #临时文件路径,保证sonarquebe可以读写
      
        sonar.web.context=/ #路径
      
        sonar.web.port=9000 #端口
      
    • 启动
      进入”bin”目录查看文件:

        ./$SONAR_HOME/bin/linux-x86-64/bin/sonar.sh start
      

      在这里插入图片描述

    • 在浏览器中输入http://your_server_address:9000/,查看sonarqube是否能够正常打开。然后在configure->market中搜索chinese pack,点击安装。

      默认账号和密码:
      账号:admin
      密码:admin
      在这里插入图片描述

      2. C++插件安装

  4. 安装C++插件,由于sonar自带的CFamily收费,但是Github上有大神公开了免费的版本sonar-cxx

  5. 将下载好的插件移动到sonar插件目录,然后重启服务,并进入localhost:9000/updatecenter查看。
     ```bash
     mv sonar-cxx-plugin* /etc/sonarqube-5.6.7/extensions/plugins
     ```
     ![在这里插入图片描述](https://img-blog.csdnimg.cn/f371984d130947c6aa42df4f5c6fe5c2.png)
    
  6. 创建Quality Profiles
    在sonarqube web页面中,进入Quality Profiles,创建Quality Profiles,选择语言为“c++”
    在这里插入图片描述

  7. 添加现有规则
    进入Rules页面,左侧菜单选中“Repository”,可以看到sonar-cxx插件自带(sonar自带?)有多种代码检测工具,包括最强大的PC-lint
    在这里插入图片描述

  8. 点击选中“c++”相关的分析工具,在整个网页右上角点击“Bulk Change”,将选中的规则应用至上一步创建的Quality Profile(cpp-test)中。
    在这里插入图片描述

  9. 选择刚刚添加的规则,点击应用,等待一下即可。
    激活完毕后,可以在这里看到规则数量。
    在这里插入图片描述

3. 本地测试

  1. 安装Cppcheck和sonar-scanner
wget https://github.com/danmar/cppcheck/archive/1.89.zip
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip
  1. 下载完后,解压出来,并重新命名sonar_scanner和配置sonar.properties
#重命名为sonar-scanner
mv sonar-scanner-cli-3.0.3.778-linux sonar-scanner
# 修改sonar_scanner.properties
[sonar@bogon ~]$ cd sonar-scanner/conf/
[sonar@bogon conf]$ ls
sonar-scanner.properties
[sonar@bogon conf]$vim sonar-scanner.properties
#把注释去掉就行
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
  1. 将添加sonar、cppcheck、sonar-scanner添加到环境变量中
vim /etc/profile

#添加一下代码(根据自己路径修改)
export SONAR_HOME=/home/sonar/sonarqube-7.1
export SONAR_SCANNER_HOME=/home/sonar/sonar-scanner
export CPPCHECK_HOME=/home/sonar/cppcheck-1.88
export PATH=$PATH:$SONAR_HOME:$SONAR_SCANNER_HOME/bin:$CPPCHECK_HOME

source /etc/profile
  1. 登录后台,并创建一个令牌
    在这里插入图片描述
  2. 测试例子,在扫描配置项里面做如下配置
[/home/chenpk/sippro]$vim sonar-project.properties
sonar.projectKey=sippro    #sonar平台中相对应项目的key
sonar.projectName=sippro   #sonar平台中相对应项目的名字
sonar.projectVersion=1.0   #sonar平台中相对应项目的项目版本
sonar.sources=./           #sonar检测的源文件目录,‘.’表示当前根目录下的所有文件目录
sonar.language=c++         #sonar检测的编程语言种类
sonar.sourceEncoding=UTF-8 #sonar平台中相对应项目的编码格式

[/home/chenpk/sippro]$ls
build  cmake  CMakeLists.txt  eva  han  help  muduo  MysqlInterface  README.md  sonar-project.properties
  1. 执行sonar-scanner命令进行检测
[/home/chenpk/sippro]$sonar-scanner
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/chenpk/sippro/sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-862.el7.x86_64 amd64
INFO: User cache: /home/chenpk/.sonar/cache
INFO: SonarQube server 7.1.0
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=193ms
INFO: Server id: AW0e9nx7Z8oUve48CK6d
INFO: User cache: /home/chenpk/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=98ms
INFO: Load/download plugins
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
.......
INFO: 93 files had no CPD blocks
INFO: Calculating CPD for 150 files
INFO: CPD calculation finished
INFO: Analysis report generated in 690ms, dir size=3 MB
INFO: Analysis reports compressed in 610ms, zip size=1 MB
INFO: Analysis report uploaded in 333ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://10.206.142.87:19000/dashboard/index/sippro
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://10.206.142.87:19000/api/ce/task?id=AW0fYZa2lfgfkyZw33vG
INFO: Task total time: 19.459 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 21.966s
INFO: Final Memory: 98M/771M
INFO: ------------------------------------------------------------------------
  1. 检测完之后,登录平台查看在这里插入图片描述

4. Gitlab 自动化测试

在这里插入图片描述
这里的Gitlab提交的部分操作是由CI/CD的测试工程师提出

  1. 软件开发工程师提交代码至Gitlab;
  2. 触发Gitlab-CI,启动gitlab-runner Docker镜像准备运行测试;
  3. Gitlab-CI触发sonar_runner Docker镜像启动,并进行代码分析;
  4. 代码分析结果在commit记录作一次comment;
  5. Gitlab管理员合并代码至develop或master分支;
  6. 触发Gitlab-CI,启动gitlab-runner Docker镜像准备运行测试;
  7. Gitlab-CI触发sonar_runner Docker镜像启动,并进行代码分析;
  8. 代码分析结果保存至SonarQube平台数据库中;
  9. 测试/开发组等用户访问SonarQube网页,查看分析的具体结果。

由于本人不太懂相关的专业知识,这里只是作为了解,并在本地测试代码。这里贴上大佬的详细操作步骤,希望给做测试的初学者提供一些帮助。

5. 参考链接

https://www.cnblogs.com/firebet/p/14009021.html

https://www.jianshu.com/p/de3a37dea4ca

https://www.jianshu.com/p/75bc44c14176

https://blog.csdn.net/weixin_45926121/article/details/103241456

https://blog.csdn.net/qq_15559817/article/details/100736498