Jenkins构建之代码扫描

发布时间:2020-11-14 发布网站:编程之家
编程之家收集整理的这篇文章主要介绍了Jenkins构建之代码扫描编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Sonar简介

Sonar 是一个用于代码质量管理的开放平台。通过插件机制,Sonar可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。

在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 EclipseIntelliJ IDEA 这些工具里联机查看结果;同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。

此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持

代码质量测试

代码质量七宗罪

# 编码规范:是否遵守了编码规范,遵循了最佳实践。
# 潜在的 BUG:可能在最坏情况下出现问题的代码,以及存在安全漏洞的代码。
# 文档和注释:过少(缺少必要信息)、过多(没有信息量)、过时的文档或注释。
# 重复代码:违反了 Don’t Repeat Yourself 原则。
# 复杂度:代码结构太复杂(如圈复杂度高),难以理解、测试和维护。
# 测试覆盖率:编写单元测试,特别是针对复杂代码的测试覆盖是否足够。
# 设计与架构:是否高内聚、低耦合,依赖最少。

Sonar部署

Sonar的相关下载和文档可以在下面的链接中找到:http://www.sonarqube.org/downloads/。需要注意最新版的Sonar需要至少JDK 1.8及以上版本。

Sonar的功能就是来检查代码是否有BUG。除了检查代码是否有bug还有其他的功能,比如说:你的代码注释率是多少,代码有一些建议,编写语法的建议。所以我们叫质量管理.

准备java环境和sonaqube包
# jdk下载地址
# http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html
# 卸载系统自带的java环境
[root@jenkins-8 git-2.9.5]# rpm -qa |grep jdk
[root@jenkins-8 git-2.9.5]# java
bash: java: 未找到命令

# 解压安装jdk
tar xvf jdk-8u151-linux-x64.tar.gz -C /usr/local/
cd /usr/local/
mv jdk1.8.0_151/ jdk
# 修改/etc/bashrc配置文件,末尾加入下面一行环境变量.
tail -2 /etc/bashrc
JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin

source /etc/bashrc
java -version        # 尽量不要用openjdk
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12,mixed mode)

# sonar下载地址
# http://www.sonarqube.org/downloads/
# 最新版的Sonar需要至少JDK1.8以上版本,建议用国外网络下载,此处我使用5.6
https://binaries.sonarsource.com/Distribution/sonarqube/

unzip sonarqube-6.5.zip 
mv sonarqube-6.5 /usr/local/
ln -s /usr/local/sonarqube-6.5/ /usr/local/sonarqube
配置数据库
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

rpm -ivh mysql57-community-release-el7-10.noarch.rpm 
yum install -y mysql-community-server
  
systemctl start  mysqld.service

# 修改Mysql密码下面有三种办法
# 1.刚安装好的mysql,可以从/var/log/mysqld.log获取临时密码
grep  "password"  /var/log/mysqld.log   
        [root@mysql ~]# mysql -uroot -p
    Enter password:
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'ZHOUjian.22';

# 2.mysqladmin -uroot -p password "ZHOUjian.22"
    Enter password:

# 3.实验环境不知道root密码操作方法如下
sed -i '/\[mysqld]/ a skip-grant-tables' /etc/my.cnf
systemctl restart mysqld
mysql <<EOF
        update mysql.user set authentication_string='' where user='root' and Host='localhost';
        flush privileges;
EOF
sed -i '/skip-grant/d' /etc/my.cnf
systemctl restart mysqld
mysqladmin -uroot -p password "ZHOUjian.20"
Enter password:              # 此处回车一下即可

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
 GRANT ALL ON sonar.* TO sonar@"%" IDENTIFIED BY "ZHOUjian.20";

flush privileges;
配置启动Sonar
cd /usr/local/sonarqube/conf/
vim sonar.properties 
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.web.host=0.0.0.0
sonar.web.port=9000

# 启动sonar
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
Starting SonarQube...
SonarQube is already running.

# 验证端口
ss -atnp |grep 9000
LISTEN     0      25           *:9000                     *:*                   users:(("java",pid=18028,fd=77))
访问web界面

默认admin/admin

安装中文支持

/usr/local/sonarqube/extensions/plugins/ #插件本地路径安装中文插件:

mv sonar-l10n-zh-plugin-1.11.jar /usr/local/sonarqube/extensions/plugins/

administration-system-update center-available,在后面的搜索框搜索插件名称,然后点 install 安装:

或 在 插 件 目 录 /usr/local/sonar/extensions/plugins 执 行以下命令:

 wget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zhplugin-1.11/sonar-l10n-zh-plugin-1.11.jar
 
 # 重启服务生效
 /usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart
 
# 刷新页面即可看到中文了

安装插件

可以安装各种语言插件

php,java,python

Sonar-scaner扫描器部署使用

Sonar-scaner扫描

sonarqube 通过调用扫描器 sonar-scanner 进行代码质量分析,即扫描器的具体工作就是扫描代码:

下载地址:http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner

unzip sonar-scanner-2.6.1.zip
mv sonar-scanner-2.6.1 /usr/local/
ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scanner
cd /usr/local/sonar-scanner/

grep "^[a-Z]" conf/sonar-scanner.properties 
sonar.host.url=http://149.129.38.117:9000
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=ZHOUjian.21
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
准备测试代码
unzip sonar-examples-master.zip 
mv sonar-examples-master/ /usr/local/src/
cd /usr/local/src/sonar-examples-master/

cat projects/languages/python/python-sonar-runner/sonar-project.properties 
# Required metadata
sonar.projectKey=org.sonarqube:python-simple-sonar-scanner
sonar.projectName=Python :: Simple Project : SonarQube Scanner   # 项目名称,会显示在仪表盘
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src		# 代码目录
# Language
sonar.language=py 	# 语言格式
# Encoding of the source files
sonar.sourceEncoding=UTF-8
执行扫描
# 注意看我当前目录
[root@jenkins python-sonar-runner]# pwd
/usr/local/src/sonar-examples-master/projects/languages/python/python-sonar-runner


# 手动在当前项目目录执行扫描,一下是扫描过程中信息
#  sonar-project.propertie 每个项目都要有
/usr/local/sonar-scanner/bin/sonar-scanner 
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /usr/local/src/sonar-examples-master/projects/languages/python/python-sonar-runner/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_151 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-514.26.2.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global settings
INFO: Load global settings (done) | time=252ms
WARN: Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
WARN: Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
INFO: User cache: /root/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=30ms
INFO: Download sonar-csharp-plugin-5.10.1.1411.jar
INFO: Download sonar-python-plugin-1.8.0.1496.jar
INFO: Download sonar-java-plugin-4.12.0.11033.jar
INFO: Download sonar-l10n-zh-plugin-1.11.jar
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
INFO: Download sonar-flex-plugin-2.3.jar
INFO: Download sonar-scm-git-plugin-1.2.jar
INFO: Download sonar-xml-plugin-1.4.3.1027.jar
INFO: Download sonar-php-plugin-2.10.0.2087.jar
INFO: Download sonar-scm-svn-plugin-1.5.0.715.jar
INFO: Download sonar-javascript-plugin-3.1.1.5128.jar
INFO: SonarQube server 6.5.0
INFO: Default locale: "en_US",source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=199ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=106ms
INFO: Load active rules
INFO: Load active rules (done) | time=1965ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=202ms
WARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
INFO: Publish mode
INFO: Project key: org.sonarqube:python-simple-sonar-scanner
INFO: -------------  Scan Python :: Simple Project : SonarQube Scanner
INFO: Load server rules
INFO: Load server rules (done) | time=277ms
INFO: Language is forced to py
INFO: Base dir: /usr/local/src/sonar-examples-master/projects/languages/python/python-sonar-runner
INFO: Working dir: /usr/local/src/sonar-examples-master/projects/languages/python/python-sonar-runner/.sonar
INFO: Source paths: src
INFO: Source encoding: UTF-8,default locale: en_US
INFO: Index files
INFO: 9 files indexed
INFO: Quality profile for py: Sonar way
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=115ms
INFO: Sensor Python Squid Sensor [python]
INFO: Python unit test coverage
INFO: Python integration test coverage
INFO: Python overall test coverage
INFO: Sensor Python Squid Sensor [python] (done) | time=1378ms
INFO: Sensor SonarJavaXmlFileSensor [java]
INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=0ms
INFO: Sensor Analyzer for "php.ini" files [php]
INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=13ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=27ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=157ms
INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: 5 files had no CPD blocks
INFO: Calculating CPD for 4 files
INFO: CPD calculation finished
INFO: Analysis report generated in 141ms,dir size=54 KB
INFO: Analysis reports compressed in 26ms,zip size=27 KB
INFO: Analysis report uploaded in 782ms
INFO: ANALYSIS SUCCESSFUL,you can browse http://149.129.38.117:9000/dashboard/index/org.sonarqube:python-simple-sonar-scanner
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://149.129.38.117:9000/api/ce/task?id=AXK4TU1CIpzWVToFffwU
INFO: Task total time: 20.514 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 38.804s
INFO: Final Memory: 43M/105M
INFO: ------------------------------------------------------------------------
# 接下来我们到sonar仪表看查看一下

Jenkins关联到SonarQube

jenkins安装sonar插件

Jenkins安装请看上一篇文章

要想让Jenkins关联到sonarqube需要先安装插件,在jenkins插件安装SonarQubePlugin,其次配置SonarQube server.

jenkins配置sonar

jenkins关联到sonar

配置扫描
[root@jenkins ~]# cat /usr/local/src/sonar-examples-master/projects/languages/python/python-sonar-runner/sonar-project.properties
# Required metadata
sonar.projectKey=org.sonarqube:python-simple-sonar-scanner
sonar.projectName=Python :: Simple Project : SonarQube Scanner
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=py

# Encoding of the source files
sonar.sourceEncoding=UTF-8

总结

以上是编程之家为你收集整理的Jenkins构建之代码扫描全部内容,希望文章能够帮你解决Jenkins构建之代码扫描所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478
编程之家官方公众号

微信公众号搜索 “ 程序精选 ” ,选择关注!

微信公众号搜索 “ 程序精选 ”
精选程序员所需精品干货内容!