sonar+Jenkins 构建代码质量自动化分析平台
Posted 民工哥技术之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sonar+Jenkins 构建代码质量自动化分析平台相关的知识,希望对你有一定的参考价值。
每天10点为你分享不一样的干货
1.Sonar 介绍
2.Sonar 安装
2.1.安装环境介绍
-
Centos 7.2 -
安装JDK 1.8 -
安装Jenkins 2.89 -
安装 maven 3.5.2(非必需,用于编译一些插件) -
安装mysql 5.6 数据库 (要求版本,不低于5.6)
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
2.2.SonarQube安装
cd /data/package/
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.6.zip
mkdir /data/service/sonar/
unzip sonarqube-6.6.zip -d /data/service/sonar/
[root@c7-node1 ~]# cat /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 $*
chmod +x /etc/init.d/sonar
ln -s /data/service/sonar/sonarqube-6.6/bin/linux-x86-64/sonar.sh /usr/bin/sonar
chkconfig sonar on
[root@c7-node1 ~]# cd /data/service/sonar/sonarqube-6.6/conf/
[root@c7-node1 conf]# cp sonar.properties{,.bak}
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
2.3.sonarQube Scanner 安装
cd /data/package/
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
unzip sonar-scanner-2.8.zip -d /data/service/sonar/
ln -s /data/service/sonar/sonar-scanner-2.8/bin/sonar-scanner /usr/bin/sonar-scanner
[root@c7-node1 ~]# cd /data/service/sonar/sonar-scanner-2.8/conf/
[root@c7-node1 conf]# cp sonar-scanner.properties{,.bak}
[root@c7-node1 conf]# vim sonar-scanner.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
2.4.sonarQube runner 安装
cd /data/service/
wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
unzip sonar-runner-dist-2.4.zip -d /data/service/sonar/
ln -s /data/service/sonar/sonar-runner-2.4/bin/sonar-runner /usr/bin/sonar-runner
[root@c7-node1 ~]# cd /data/service/sonar/sonar-runner-2.4/conf/
[root@c7-node1 conf]# cp sonar-runner.properties{,.bak}
[root@c7-node1 conf]# vim sonar-runner.properties
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
[root@c7-node1 ~]# vim /etc/profile.d/sonar.sh
export SONAR_HOME=/data/service/sonar/sonarqube-6.6
export SONAR_RUNNER_HOME=/data/service/sonar/sonar-runner-2.4
export PATH=$SONAR_RUNNER_HOME/bin:$PATH
[root@c7-node1 ~]# source /etc/profile.d/sonar.sh
2.5.启动sonar
[root@c7-node1 ~]# useradd sonar
[root@c7-node1 ~]# chown -R sonar:sonar /data/service/sonar/
[root@c7-node1 ~]# su sonar
[sonar@c7-node1 root]$ /etc/init.d/sonar start
3.Sonar 插件使用及汉化
3.1.后台安装中文插件
3.2.手动安装插件
git clone https://github.com/SonarQubeCommunity/sonar-l10n-zh.git
cd sonar-l10n-zh/
git checkout sonar-l10n-zh-plugin-1.18
mvn package
cp target/sonar-l10n-zh-plugin-1.18-SNAPSHOT.jar /data/service/sonar/sonarqube-6.6/extensions/plugins/
4.Sonar 分析Python代码小例
[sonar@c7-node1 ~]$ cat python_test/sendEmail.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host="xxx" #设置服务器
mail_user="xxx" #用户名
mail_pass="xxx" #口令
sender = 'bigdata_monitor@xueleyun.com'
receivers = sys.argv[1]
content=sys.argv[3]
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receivers, 'utf-8')
subject = sys.argv[2]
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
[sonar@c7-node1 ~]$ cat python_test/sonar-project.properties
sonar.projectKey=python_test
sonar.projectName=python_test
sonar.projectVersion=1.0
sonar.sources=.
sonar.sourceEncoding=UTF-8
|
|
5.1.设置sonar中的令牌
5.2.Jenkins 安装sonar相关插件
5.3.Jenkins配置sonar插件
5.4.Jenkins job 中配置sonar
6.后记
文章系作者原创投稿。作者:西门飞冰,一名90后it男,一直在北京工作,热爱运动,热爱冒险,热爱旅行。 原文地址:http://www.fblinux.com/?p=1155。
...
点击【阅读原文】和民工哥一起学技术、搞事情~~
以上是关于sonar+Jenkins 构建代码质量自动化分析平台的主要内容,如果未能解决你的问题,请参考以下文章