sonar、jenkins构建代码检查

sonar、jenkins构建代码检查

安装sonar

预置条件

1)已安装JAVA环境
    版本:JDK1.8
2)已安装有mysql数据库
    版本:mysql5.6以上
3)下载sonarQube与sonar-scanner
    版本:[sonarQube7.3](https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-7.3.zip)
    版本:[sonar-scanner3.2](https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.2.0.1227-linux.zip)

创建数据库

创建用户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';  
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';  
FLUSH PRIVILEGES;

请不要用sonar用作密码,这里只是个示例!!!!

创建sonar专用用户

$ useradd sonar
$ passwd sonar

修改配置文件

将sonar-7.3.zip上传到服务器,放置到/home/sonar 目录,并解压到当前目录即可。
修改conf目录下的sonar.properties文件
配置参考:
修改数据库连接及用户名、密码和本机IP
sonar.jdbc.username=
sonar.jdbc.password=
sonar.jdbc.url=jdbc:mysql://IP:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.web.host=

启动服务

启动sonar
切换到sonar安装目录下 /bin/linux-x86-64
#./sonar.sh start

现在通过9000端口就可以访问web服务了。
我这里启动出了问题。通过./sonar.sh console可以看到问题是Process exited with exit value [es]: 143另外在logs目录有更详细的错误信息。

解决es问题

  1. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
$ vim /etc/sysctl.conf
添加vm.max_map_count=262144
$ sysctl -p使配置生效。

配置自启动

创建自启动脚本文件/etc/init.d/sonar

$ vi /etc/init.d/sonar

添加如下内容

#!/bin/sh
#
# rc file for SonarQube
#
# chkconfig: 345 96 10
# description: SonarQube system (www.sonarsource.org)
#
### BEGIN INIT INFO
# Provides: sonar
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: SonarQube system (www.sonarsource.org)
# Description: SonarQube system (www.sonarsource.org)
### END INIT INFO

/usr/bin/sonar $*

添加启动服务

$ ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar
$ chmod 755 /etc/init.d/sonar
$ sysv-rc-conf  sonar on

如果没有sysv-rc-conf,请安装apt-get install sysv-rc-conf

安装sonar-scanner

scanner在于jenkins 进行集成的作用是:jenkins通过scanner传入要分析的工程,scanner再将这些分析结果,传给sonargube 进行呈现

在sonar目录下直接解压zip文件
unzip sonar-scanner-cli-3.2.0.1227-linux.zip

配置

和sonar配置一样

sonar.host.url=http://192.168.136.144:9000
sonar.login=admin
sonar.password=admin
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar123
sonar.jdbc.url=jdbc:mysql://192.168.136.144:3306/sonar?useUnicode=true&characterEncoding=utf8

加入PATH

修改.bash_profile文件

PATH=$PATH:$HOME/bin:/home/sonar/sonar-scanner-3.2.0.1227-linux/bin

source .bash_profile 后执行sonar-scanner -h 有输出就是好了。

配置jenkins

插件安装

系统管理->插件管理 搜索sonar, 可以看到SonarQube Scanner for Jenkins 安装即可。

配置插件

进入系统管理,会发现多了个SonarQube servers的配置项。先勾选Enable injection of SonarQube server configuration as build environment variables
填上名字和地址,token在刚进入sonar的Web页面时,会让你生成一个,就使用那个。

进入系统管理->Global Tool Configuration
新增SonarQube Scanner.
Name填上sonar-scanner-3.2
SONAR_RUNNER_HOME填上/home/sonar/sonar-scanner-3.2.0.1227-linux
保存。

使用sonar

目前公司的代码用的编程语言有.net, python, nodejs。
我以python为示测试的sonar。
只能选择在构建过程中增加。

配置好Anyalysis properties

sonar.projectKey=testSonar
sonar.projectName=testSonar
sonar.projectVersion=1.0
sonar.language=python
sonar.sources=.
sonar.login=admin
sonar.password=admin
sonar.exclusions=**/*View.java #用于忽略文件

遇到上传失败问题ERROR: Caused by: Broken pipe (Write failed)
看web.log日志发现是com.mysql.jdbc.PacketTooBigException: Packet for query is too large (19560234 > 16777216).
调整一下。

set global max_allowed_packet = 100*1024*1024;

然后重启mysql和sonar就好了。

sonar支持更多语言

下载对应的插件,直接复制到extensions/plugins目录下。
然后重启sonar即可。
我司用到了.netcore, 感觉这个稍微麻烦点。
直接用jenkins里构建步骤中的SonarQube Scanner for MSBuild - Begin AnalysisSonarQube Scanner for MSBuild - End Analysis没有成功。报错ERROR: SonarQube Scanner for MSBuild executable was not found for
安装全局工具dotnet tool install --global dotnet-sonarscanner,参考文档
最后在shell里用脚本成功了。

dotnet sonarscanner begin /k:key /n:name /v:1.0 /d:sonar.login=user /d:sonar.password=password
dotnet build
dotnet sonarscanner end /d:sonar.login=user /d:sonar.password=password
Tags:
11 Comments