jenkins持续集成工程(jenkins集成sonar扫描)
jenkins持续集成工程(jenkins集成sonar扫描)2、安装psycopg2按照步骤选择安装3、项目根目录下新增扫描配置文件sonar-project.propertiessonar.sourceEncoding=UTF-8 sonar.projectKey=syncmanager sonar.projectName=syncmanager sonar.projectVersion=2.7.2P1 sonar.language=java sonar.source=G:/jenkins/workspace/syncmanager/src/main/java/com sonar.projectBaseDir=G:/jenkins/workspace/syncmanager/src/main/java/com sonar.java.binaries=G:/jenkins/workspace/syncmanager/target/classes/c
一、本地安装sonar scanner客户端从官网下载合适版本进行安装 https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
配置sonar-scanner.properties
#----- Default SonarQube server
sonar.host.url=http://172.18.37.214:9000
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
sonar.login=admin
sonar.password=123456
二、jenkins配置sonar扫描
1、安装sonarqube scanner插件
2、配置扫描
3、项目根目录下新增扫描配置文件sonar-project.properties
sonar.sourceEncoding=UTF-8
sonar.projectKey=syncmanager
sonar.projectName=syncmanager
sonar.projectVersion=2.7.2P1
sonar.language=java
sonar.source=G:/jenkins/workspace/syncmanager/src/main/java/com
sonar.projectBaseDir=G:/jenkins/workspace/syncmanager/src/main/java/com
sonar.java.binaries=G:/jenkins/workspace/syncmanager/target/classes/com
三、sonar数据分析并发送邮件
1、安装python
从官网选择适合的安装包 https://www.python.org/downloads/windows/
按照步骤选择安装
2、安装psycopg2
进入python安装目录的Scripts目录
安装psycopg2
安装jinja2
3、部署python脚本sonar.py以及报告模板report-template.html
import psycopg2 os sys
from jinja2 import FileSystemLoader Environment
def select_project_uuid(project_name):
db = psycopg2.connect(host="172.18.37.201" port=5432 user="postgres" password="123456" database="sonar")
cursor = db.cursor()
select_p_uuid="SELECT uuid kee FROM projects WHERE name = '%s'" %(project_name)
cursor.execute(select_p_uuid)
result = cursor.fetchone()
p_uuid = result[0]
projectKey = result[1]
db.close()
return(p_uuid projectKey)
def select_total_info(p_uuid):
total_info=[]
# 使用cursor()方法获取操作游标
db = psycopg2.connect(host="172.18.37.201" port=5432 user="postgres" password="123456" database="sonar")
cursor = db.cursor()
select_p_links = "SELECT text_value FROM project_measures WHERE text_value LIKE 'java=%' and component_uuid=" "\'" p_uuid "\'"
cursor.execute(select_p_links)
p_links = cursor.fetchone()[0].split("=")[1]
sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =%s"
for leak in [2 3 1]:
search_data = sql_info %(p_uuid leak)
cursor.execute(search_data)
total_info.append(cursor.fetchone()[0])
db.close()
return p_links total_info
def select_bugs(p_uuid):
bugs=[]
db = psycopg2.connect(host="172.18.37.201" port=5432 user="postgres" password="123456" database="sonar")
cursor = db.cursor()
sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =2 AND severity ='%s'"
for leak in ['BLOCKER' 'CRITICAL' "MAJOR" 'MINOR' 'INFO']:
search_data=sql_info % (p_uuid leak)
cursor.execute(search_data)
bugs.append(cursor.fetchone()[0])
db.close()
return bugs
def select_leaks(p_uuid):
leaks=[]
db = psycopg2.connect(host="172.18.37.201" port=5432 user="postgres" password="123456" database="sonar")
cursor = db.cursor()
sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =3 AND severity ='%s'"
for leak in ['BLOCKER' 'CRITICAL' "MAJOR" 'MINOR' 'INFO']:
search_data=sql_info % (p_uuid leak)
cursor.execute(search_data)
leaks.append(cursor.fetchone()[0])
db.close()
return leaks
def select_bad_tastes(p_uuid):
tastes=[]
db = psycopg2.connect(host="172.18.37.201" port=5432 user="postgres" password="123456" database="sonar")
cursor = db.cursor()
sql_info="SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =1 AND severity ='%s'"
for leak in ['BLOCKER' 'CRITICAL' "MAJOR" 'MINOR' 'INFO']:
search_data=sql_info % (p_uuid leak)
cursor.execute(search_data)
tastes.append(cursor.fetchone()[0])
return tastes
db.close()
curpath = os.getcwd()
table_tem_name="report-templete.html"
def generate_errmsg_table(s_lines="" total_data=[] bugs=[] leaks=[] tastes=[] report_url=""):
env = Environment(loader=FileSystemLoader(curpath 'utf-8')) # 创建一个包加载器对象
template = env.get_template(table_tem_name)
html_content = (template.render(lins=s_lines total_data=total_data bugs=bugs leaks = leaks tastes=tastes report_url=report_url))
fh = open(report_html_path 'w')
fh.write(html_content)
fh.close()
project_name = sys.argv[1]
report_html_path="report\\" project_name ".html"
p_uuid projectKey=select_project_uuid(project_name)
s_lines total_data=select_total_info(p_uuid)
bugs=select_bugs(p_uuid)
leaks=select_leaks(p_uuid)
tastes=select_bad_tastes(p_uuid)
report_url="http://172.18.37.214:9000/dashboard?id=%s" %(projectKey)
generate_errmsg_table(s_lines total_data bugs leaks tastes report_url)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="GBK">
<body>
<p style="font-weight:bold;">一、sonar扫描总体情况:</p>
<ul>
<li style="font-weight:bold;">整体运行情况:扫描代码行数:<span style="color:blue">{{lins}}</span> bugs:<span style="color:red">{{total_data[0]}}</span> 漏洞:<span style="color:red">{{total_data[1]}}</span> 坏味道:<span style="color:red">{{total_data[2]}}</span></li>
<li style="font-weight:bold;">URL地址:<a style="font-weight:bold;" href={{report_url}} >{{report_url}}</a></li>
</ul>
<p style="font-weight:bold;">二、错误信息详情:</p>
<table border="1" cellpadding="10" width="540" height="120">
<tr ><th></th><th>阻断</th><th>严重</th><th>主要</th><th>次要</th><th>提示</th><th>总数</th></tr>
<tr bgcolor=#ECFFFF><td>bugs</td><td align="center">{{bugs[0]}}</td><td align="center">{{bugs[1]}}</td><td align="center">{{bugs[2]}}</td><td align="center">{{bugs[3]}}</td><td align="center">{{bugs[4]}}</td><td align="center" style="color:red">{{total_data[0]}}</td></tr>
<tr bgcolor=#D2E9FF><td>漏洞</td><td align="center">{{leaks[0]}}</td><td align="center">{{leaks[1]}}</td><td align="center">{{leaks[2]}}</td><td align="center">{{leaks[3]}}</td><td align="center">{{leaks[4]}}</td><td align="center" style="color:red">{{total_data[1]}}</td></tr>
<tr bgcolor=#ECFFFF><td>坏味道</td><td align="center">{{tastes[0]}}</td><td align="center">{{tastes[1]}}</td><td align="center">{{tastes[2]}}</td><td align="center">{{tastes[3]}}</td><td align="center">{{tastes[4]}}</td><td align="center" style="color:red">{{total_data[2]}}</td></tr>
</table>
<br></br>
</body>
</html>
4、jenkins中配置python环境变量
环境变量配置
5、配置项目执行分析脚本
调用脚本配置
邮件内容配置