Jenkins+SonarQube持续集成平台

1 jenkins和   SnoarQube 和 sonar-scanner  之间有依赖关系,版本需要符合条件最后才可以生成报表

  持续集成环境:Jenkins

  • 所需系统环境: java 8 及以上
    • 代码托管:Git 2.22 或者 gitlab
    • 审查工具:SonarQube
      • 该工具由两部分组成,分别是 SonarQube本身(即是审查服务器),和 sonar-scanner(审查服务端)

        SonarQube和sonar-scanner的关系就像是 github 官网和我们本地的 git 软件的关系

      • snoar-scanner = sonar-runner 是同一个软件在不同版本下的名字,网上的教程两者基本上是通用

            

1 安装jenkins

下载链接地址  

链接:https://pan.baidu.com/s/1KFmC2Fj4WUOtf1h4qJd8Kg
提取码:0br8

SonarQube   版本号 7.6    不能是7.9之上,不然,需要java 版本11

sonar-scanner 提取码:cy1n

mysql 5.7.19 提取码:pwwv    这个版本最好是5.6. 5.7  都行,对版本也有限制

2 安装mysql

3.首先需要安装 MySql 数据库(为了方便可配置mysql相关环境变量,同时第一次进入mysql需要修改密码,此处都略过,百度一下)

root 登录 mysql,创建 sonar 数据库和用户授权。这个

    CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;         #新建数据库;数据库中不用自己建表,执行分析命令时会自动在库中建表
    CREATE USER 'sonar' IDENTIFIED BY 'sonar';             #新建用户
    GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';              #赋权限,所有pc 都可以访问;还需要设置用户密码
    GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
    FLUSH PRIVILEGES;           #刷新缓存立即生效

4 安装 SonarQube

  sonarqube已解压

  在路径下的 conf 文件夹下修改 sonar.properties

url是数据库连接地址,username是数据库用户名,jdbc.password是数据库密码,login是sonarqube的登录名,sonar.password是sonarqube的密码

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

  sonar.jdbc.username=root
       sonar.jdbc.password=walker123
       sonar.sorceEncoding=UTF-8
       sonar.login=admin
       sonar.password=admin

  之后打开 bin 目录下对应的 StartSnoar.bat 就可以启动 Snoar 服务器了。可通过浏览器 http://127.0.0.1:9000 查看,就可以访问了

5
配置 sonar-scanner(SonarQube和sonar-scanner的关系就像是 github 官网和我们本地的 git 软件的关系);必须版本合适
  

  1.配置 sonar-scanner 目录下 conf 中的 snoar-scanner.properties     sonar-scanner 

  sonar.jdbc.url=jdbc:mysql://mysql:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
  sonar.jdbc.username=root 
  sonar.jdbc.password=root
  

  配置环境变量

  a.新建变量,name=SONAR_RUNNER_HOME。value=D:sonarsonar-scanner-2.5

  b.打开path,输入%SONAR_RUNNER_HOME%in;

  cmd  执行如下命令    sonar-runner -v      未报错即ok

6  去我们 git 项目的文件夹里,新建sonar-project.properties文件,内容

其中:projectName是项目名字,sources是源文件所在的目录,sonar.language  代码语言,python  是py

看有的没有配置

# must be unique in a given SonarQube instance
sonar.projectKey=my:project
# this is the name displayed in the SonarQube UI
sonar.projectName=apiautocore
sonar.projectVersion=1.0
sonar.language=go
# Path is relative to the sonar-project.properties file. Replace "" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=src
# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

7.设置成功后,启动sonarqube服务,并启动cmd

8 .在cmd进入项目所在的根目录,输入命令:sonar-runner,分析成功后会出现下图

 访问地址   localhost:9000

 

 sonarqube生成token 值

9 .在配置页对SonarQube servers进行配置: 

系统配置–系统设置–SonarQube servers

 

 
soarqube 中文插件地址 ;将中文的jia包放在 sonarqube-7.6extensionsplugins 目录重启服务即可
https://github.com/SonarQubeCommunity/sonar-l10n-zh/tags
 
FQA 1 snoarqube  启动失败,1  任务管理器中结束 java的进程,在temp目录下删除所有文件,cmd 执行   StartSonar.bat  命令即可以正常重启 sonarqube的服务
 
转载 https://www.cnblogs.com/can-i-do/p/11173669.html

 

 
原文地址:https://www.cnblogs.com/pythonwork/p/13039192.html