日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

使用jenkins sonar進(jìn)行代碼掃描,并發(fā)送自定義郵件

 印度阿三17 2019-05-23

jenkins架構(gòu)

1、一臺(tái)機(jī)器作為jenkins master不進(jìn)行構(gòu)建操作,只負(fù)責(zé)調(diào)度其他slave節(jié)點(diǎn)執(zhí)行任務(wù)

2、一臺(tái)slave機(jī)器作為執(zhí)行機(jī)器存放從gitlab上拉取的代碼,使用sonar-scanner進(jìn)行代碼掃描和使用sonarqube進(jìn)行頁(yè)面展示,

步驟

1、在執(zhí)行機(jī)上安裝sonarqube和sonar-scanner兩個(gè)工具

  執(zhí)行機(jī)器主要任務(wù)有

  1、存儲(chǔ)代碼

  2、進(jìn)行代碼掃描

  3、根據(jù)自己編寫的python腳本生成自定義的郵件內(nèi)容

  4、sonar頁(yè)面展示

下載地址:。。。。。。。

sonarqube安裝及配置mysql數(shù)據(jù)庫(kù):http://www./article/6431255831/

sonar-scanner安裝:http://www./article/1870255571/

2、jenkins master機(jī)器配置

  2.1 安裝插件:SonarQube Scanner for Jenkins

  

?

  2.2 系統(tǒng)管理》系統(tǒng)設(shè)置配置sonarqube

?

  ?2.3 系統(tǒng)管理》全局工具配置sonarqube scanner

?

3、配置節(jié)點(diǎn)(將slave機(jī)器注冊(cè)到master上,以供后續(xù)master調(diào)用)

  3.1 增加節(jié)點(diǎn)

  

?

?  3.2 節(jié)點(diǎn)配置

  

  3.4 節(jié)點(diǎn)啟動(dòng)

  

4、創(chuàng)建job

  4.1 創(chuàng)建自由風(fēng)格的任務(wù)

   Restrict where this project can be run(指定此項(xiàng)目在哪個(gè)機(jī)器上運(yùn)行),指向我們新建的slave節(jié)點(diǎn)機(jī)器

  

  拉取代碼:

  構(gòu)建步驟新增代碼掃描配置

  前提:由于要執(zhí)行sonar.py腳本,所以jenkins所在機(jī)器要有python3環(huán)境,且安裝了pymysql、jinja2,

  進(jìn)入到sonar.py所在目錄,執(zhí)行命令:call E:\Python36\python.exe E:\sonar\sonar_script\sonar.py?項(xiàng)目名

?

sonar.projectKey=A-yto-steward
sonar.projectName=A網(wǎng)-客戶管家
sonar.projectVersion=1.0
sonar.sources=./
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.java.binaries=./
sonar.login=admin
sonar.password=admin

?

cd ../..
cd sonar_script
call E:\Python36\python.exe E:\sonar\sonar_script\sonar.py A網(wǎng)-客戶管家

在執(zhí)行機(jī)如下目錄放sonar.py和table.html文件

sonar.py腳本內(nèi)容

#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Time   : 2018/11/20 13:16
# @Author : wnaglihua
# @File   : sonar.py

import pymysql,os,sys
from jinja2 import FileSystemLoader,Environment

def select_project_uuid(project_name):
    db = pymysql.connect(host="192.168.207.160", port=3306, user="sonar", passwd="sonar", db="sonar")
    cursor = db.cursor()
    select_p_uuid="SELECT project_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()方法獲取操作游標(biāo)
    db = pymysql.connect(host="192.168.207.160", port=3306, user="sonar", passwd="sonar", db="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 = pymysql.connect(host="192.168.207.160", port=3306, user="sonar", passwd="sonar", db="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 = pymysql.connect(host="192.168.207.160", port=3306, user="sonar", passwd="sonar", db="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 = pymysql.connect(host="192.168.207.160", port=3306, user="sonar", passwd="sonar", db="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="table.html"    
def generate_errmsg_table(s_lines="", total_data=[], bugs=[],leaks=[],tastes=[],report_url=""):
    env = Environment(loader=FileSystemLoader(curpath, 'utf-8'))  # 創(chuàng)建一個(gè)包加載器對(duì)象
    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://192.168.207.140:9000/dashboard?id=%s" %(projectKey)
generate_errmsg_table(s_lines,total_data,bugs,leaks,tastes,report_url)

table.html腳本內(nèi)容

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="GBK">
<body>
<p style="font-weight:bold;">一、總體情況:</p>
<ul>
<li style="font-weight:bold;">整體運(yùn)行情況:掃描代碼行數(shù):<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;">二、錯(cuò)誤信息詳情:</p>
<table border="1" cellpadding="10" width="540" height="120">
    <tr ><th></th><th>阻斷</th><th>嚴(yán)重</th><th>主要</th><th>次要</th><th>提示</th><th>總數(shù)</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><span style="font-weight:bold;"><b style="color:red">代碼掃描度量通過(guò)準(zhǔn)則:</b></span>
<br><span style="font-size:14px">新覆蓋率<80%;
<br><span style="font-size:14px">新代碼中的重復(fù)行密度 (%)>30%;
<br><span style="font-size:14px">新代碼可維護(hù)率劣于A;
<br><span style="font-size:14px">新代碼可靠率劣于A;
<br><span style="font-size:14px">新代碼安全率劣于A;
<br></br>
</body>
</html>

  郵件配置

  安裝插件:Email Extension

  在系統(tǒng)管理》》系統(tǒng)設(shè)置中設(shè)置

?

?

?

job中國(guó)配置發(fā)送郵件

內(nèi)容選擇HTML,打開高級(jí)選項(xiàng)

增加觸發(fā)器,并打開高級(jí)選項(xiàng)

輸入發(fā)送郵箱列表,以英文逗號(hào)分隔,和郵件內(nèi)容,html就是上面步驟生成的

  

?  構(gòu)建完成后會(huì)收到如下格式郵件

  

?

來(lái)源:http://www./content-4-204151.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多