jenkins:集成sonar代码扫描+发送邮件
前提:
Jenkins
JDK
目录:
1、安装sonar插件:SonarQube Scanner for Jenkins
2、安装SonarQube
3、安装sonar-scanner
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1.安装sonar插件:SonarQube Scanner for Jenkins
2、安装SonarQube,并配置mysql数据库
2.1 下载
下载地址:https://www.sonarqube.org/downloads/
2.2 解压
下载后放在/usr/local目录下,并解压
unzip sonarqube-7.4.zip
2.3 配置环境变量
vi /etc/profile
里面写如下
#set sonarqube export SONAR_HOME=/usr/local/sonarqube-7.4 PATH=$PATH:$SONAR_HOME/bin
使配置生效
source /etc/profile
2.4 启动并测试
进入到启动目录
/usr/local/sonarqube-7.4/bin/linux-x86-64
执行启动命令
./sonar.sh start
启动时要查看es.log和sonar.log日志,还有web.log,关于数据库配置的错误会在此处显示
tail -f /usr/local/sonarqube-6.7.6/logs/es.log
tail -f /usr/local/sonarqube-6.7.6/logs/sonar.log
tail -f /usr/local/sonarqube-6.7.6/logs/web.log
用root启动会报错,请参考我的另一篇文章:https://www.cnblogs.com/gcgc/p/10239590.html
所有问题解决完后启动,并测试,访问ip+port,OK了
如果默认的9000端口被占用了,则修改默认端口 vi /usr/local/sonarqube-7.4/conf/sonar.properties
注意sonar中还集成了elastic search默认端口为9001,所以要查看9001否也被占用lsof -i:9001,如果被占用,也是修改上面的文件
2.5 sonar配置mysql数据库
sonar自带测试数据库,但是具体应用还是的换其他数据,这里我们使用mysql,
创建sonar数据库
DROP DATABASE 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;
2.6 修改配置文件
修改vi /usr/local/sonarqube-7.4/conf/sonar.properties文件,如下的蓝色加粗字体
# Property values can: #- reference an environment variable, for example sonar.jdbc.url= ${env:SONAR_JDBC_URL} #- be encrypted. See https://redirect.sonarsource.com/doc/settings-encryption.html #--------------------------------------------------------------------------------------------------# DATABASE # # IMPORTANT: #- The embedded H2 database is used by default. It is recommended for tests but not for# production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer. #-Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.# User credentials. # Permissions to create tables, indices and triggers must be granted to JDBC user. # The schema must be created first. #sonar.jdbc.username=#sonar.jdbc.password=sonar.jdbc.username=sonar sonar.jdbc.password=sonarsonar.sorceEncoding=UTF-8 sonar.login=admin sonar.password=admin#-----Embedded Database (default) # H2 embedded database server listening port, defaults to9092#sonar.embeddedDatabase.port=9092#-----DEPRECATED #----- MySQL >=5.6 && <8.0# Support of MySQL is droppedin Data Center Editions and deprecated inall other editions # Only InnoDB storage engine is supported (not myISAM). # Only the bundled driver is supported. It can not be changed. sonar.jdbc.url=jdbc:mysql://192.168.207.160:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false #----- Oracle 11g/12c # The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/. # Only the thin client is supported, and only the versions11.2.x or 12.2.x must be used. See # https://jira.sonarsource.com/browse/SONAR-9758 for more details. # If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000 #sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE#----- PostgreSQL 9.3or greater # By default the schema named"public" is used. It can be overridden with the parameter "currentSchema". #sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema #----- Microsoft SQLServer 2014/2016and SQL Azure # A database named sonar must exist and its collation must becase-sensitive (CS) and accent-sensitive (AS) # Use the following connectionstring ifyou want to use integrated security with Microsoft Sql Server # Do not set sonar.jdbc.username or sonar.jdbc.password propertyifyou are using Integrated Security # For Integrated Security to work, you have to download the Microsoft SQL JDBC driver package from # https://www.microsoft.com/en-us/download/details.aspx?id=55539 # and copy sqljdbc_auth.dll to your path. You have to copy the 32 bit or 64bit version of the dll # depending upon the architecture of your server machine. #sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true # Use the following connectionstring if you want to use SQL Auth whileconnecting to MS Sql Server. # Set the sonar.jdbc.username and sonar.jdbc.password appropriately. #sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar #-----Connection pool settings # The maximum number of active connections that can be allocated # at the sametime, or negative forno limit. # The recommended value is1.2 * max sizes of HTTP pools. For example ifHTTP ports are # enabled with default sizes (50, see property sonar.web.http.maxThreads) #then sonar.jdbc.maxActive should be 1.2 * 50 = 60. #sonar.jdbc.maxActive=60# The maximum number of connections that can remain idleinthe # pool, without extra ones being released, or negativeforno limit. #sonar.jdbc.maxIdle=5# The minimum number of connections that can remain idleinthe pool, # without extra ones being created, or zero to create none. #sonar.jdbc.minIdle=2# The maximum number of milliseconds that the pool willwait(when there # are no available connections)fora connection to be returned before # throwing an exception, or<= 0 to waitindefinitely. #sonar.jdbc.maxWait=5000#sonar.jdbc.minEvictableIdleTimeMillis=600000#sonar.jdbc.timeBetweenEvictionRunsMillis=30000#--------------------------------------------------------------------------------------------------# WEB SERVER # Web server is executedina dedicated Java process. By default heap size is 512Mb. # Use the following property to customize JVM options. # Recommendations: # # The HotSpot Server VM is recommended. The property-server should be added ifserver mode # is not enabled by default on your environment: # http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html # # Startup can belong if entropy source is shortof entropy. Adding #-Djava.security.egd=file:/dev/./urandom is an option to resolve the problem. # See https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Entropy_Source # #sonar.web.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError# Same as previous property, but allows to not repeat all other settings like-Xmx #sonar.web.javaAdditionalOpts=# Binding IP address. For servers withmore than one IP address, this property specifies which# address will be usedforlistening on the specified ports. # By default, ports will be used on all IP addresses associated with the server. #sonar.web.host=0.0.0.0# Web context. When set, it must start with forward slash (for example /sonarqube). # The default value is root context (empty value). #sonar.web.context=# TCP portfor incoming HTTP connections. Default value is 9000. sonar.web.port=9000# The maximum number of connections that the server will accept and process at any giventime. # When this number has been reached, the server will not accept anymore connections until# the number of connections falls below this value. The operating system may still accept connections # based on the sonar.web.connections.acceptCount property. The default value is50. #sonar.web.http.maxThreads=50# The minimum number of threads always kept running. The default value is5. #sonar.web.http.minThreads=5# The maximum queue lengthforincoming connection requests when all possible request processing # threads areinuse. Any requests received when the queue is full will be refused. # The default value is25. #sonar.web.http.acceptCount=25# By default users are logged out and sessions closed when server is restarted. # If you prefer keeping user sessions open, a secret should be defined. Value is # HS256 key encoded with base64. It must be uniqueforeach installation of SonarQube. # Example of command-line: #echo -n "type_what_you_want" | openssl dgst -sha256 -hmac "key" -binary |base64 #sonar.auth.jwtBase64Hs256Secret=# The inactivity timeout duration of user sessions,inminutes. After the configured # period oftime, the user is logged out. # The default value is set to3 days (4320minutes) # and cannot be greater than3months. Value must be strictly positive. #sonar.web.sessionTimeoutInMinutes=4320# A passcode can be defined to access some web services from monitoring # tools without having to use the credentials of a system administrator. # Check the Web API documentation to knowwhichweb services are supporting this authentication mode. # The passcode should be providedin HTTP requests with the header "X-Sonar-Passcode". # By default feature is disabled. #sonar.web.systemPasscode=#--------------------------------------------------------------------------------------------------# SSO AUTHENTICATION# Enable authentication using HTTP headers #sonar.web.sso.enable=false# Name of the header to get the userlogin. # Only alphanumeric,'.' and '@'characters are allowed #sonar.web.sso.loginHeader=X-Forwarded-Login# Name of the header to get the user name #sonar.web.sso.nameHeader=X-Forwarded-Name# Name of the header to get the user email (optional) #sonar.web.sso.emailHeader=X-Forwarded-Email# Name of the header to get the list of usergroups, separated by comma (optional). # If the sonar.sso.groupsHeader is set, the user will belong to thosegroups if groups exist inSonarQube. # If none of the providedgroups exists inSonarQube, the user will only belong to the default group. # Note that the default group will always be set. #sonar.web.sso.groupsHeader=X-Forwarded-Groups# Interval used to know when to refresh name, email andgroups. # During this interval,if for instance the name of the user is changed inthe header, it will only be updated after X minutes. #sonar.web.sso.refreshIntervalInMinutes=5#--------------------------------------------------------------------------------------------------# LDAP CONFIGURATION# Enable the LDAP feature # sonar.security.realm=LDAP# Set totrue when connecting to a LDAP server using a case-insensitive setup. # sonar.authenticator.downcase=true# URL of the LDAP server. Note thatif you are using ldaps, then you should installthe server certificate into the Java truststore. # ldap.url=ldap://localhost:10389 # Bind DN is the username of an LDAP user to connect (or bind) with. Leave this blankforanonymous access to the LDAP directory (optional) # ldap.bindDn=cn=sonar,ou=users,o=mycompany# Bind Password is the password of the user to connect with. Leave this blankforanonymous access to the LDAP directory (optional) # ldap.bindPassword=secret# Possible values: simple| CRAM-MD5 | DIGEST-MD5 | GSSAPI See http://java.sun.com/products/jndi/tutorial/ldap/security/auth.html (default: simple) # ldap.authentication=simple# See : #* http://java.sun.com/products/jndi/tutorial/ldap/security/digest.html # * http://java.sun.com/products/jndi/tutorial/ldap/security/crammd5.html # (optional) # ldap.realm=example.org# Context factory class (optional) # ldap.contextFactoryClass=com.sun.jndi.ldap.LdapCtxFactory# Enable usage of StartTLS (default :false) # ldap.StartTLS=true# Follow or not referrals. See http://docs.oracle.com/javase/jndi/tutorial/ldap/referral/jndi.html (default: true) # ldap.followReferrals=false# USER MAPPING# Distinguished Name (DN) of the root nodein LDAP from which to search forusers (mandatory) # ldap.user.baseDn=cn=users,dc=example,dc=org# LDAP user request. (default: (&(objectClass=inetOrgPerson)(uid={login})) ) # ldap.user.request=(&(objectClass=user)(sAMAccountName={login}))# AttributeinLDAP defining the user’s real name. (default: cn) # ldap.user.realNameAttribute=name# AttributeinLDAP defining the user’s email. (default: mail) # ldap.user.emailAttribute=email# GROUP MAPPING# Distinguished Name (DN) of the root nodein LDAP from which to search for groups. (optional, default: empty) # ldap.group.baseDn=cn=groups,dc=example,dc=org# LDAP group request (default: (&(objectClass=groupOfUniqueNames)(uniqueMember={dn})) ) # ldap.group.request=(&(objectClass=group)(member={dn}))# Property used to specifiy the attribute to be usedfor returning the list of user groups inthe compatibility mode. (default: cn) # ldap.group.idAttribute=sAMAccountName#--------------------------------------------------------------------------------------------------# COMPUTE ENGINE # The Compute Engine is responsibleforprocessing background tasks. # Compute Engine is executedina dedicated Java process. Default heap size is 512Mb. # Use the following property to customize JVM options. # Recommendations: # # The HotSpot Server VM is recommended. The property-server should be added ifserver mode # is not enabled by default on your environment: # http://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html # #sonar.ce.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError# Same as previous property, but allows to not repeat all other settings like-Xmx #sonar.ce.javaAdditionalOpts=#--------------------------------------------------------------------------------------------------# ELASTICSEARCH # Elasticsearch is used to facilitate fast and accurate information retrieval. # It is executedina dedicated Java process. Default heap size is 512Mb. # #--------------------------------------------------# Word of cautionforLinux users on 64bits systems #--------------------------------------------------# Please ensure Virtual Memory on your system is correctly configuredforElasticsearch to run properly # (see https://www.elastic.co/guide/en/elasticsearch/reference/5.5/vm-max-map-count.html for details). # # When SonarQube runs standalone, a warning such as the following may appearin logs/es.log: #"max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]"# When SonarQube runs as a cluster, however, Elasticsearch will refuse to start. ## JVM options of Elasticsearch process #sonar.search.javaOpts=-Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError# Same as previous property, but allows to not repeat all other settings like-Xmx #sonar.search.javaAdditionalOpts=# Elasticsearch port. Default is9001. Use 0 to get a freeport. # As a security precaution, should be blocked by a firewall and not exposed to the Internet. sonar.search.port=9002# Elasticsearch host. The search server will bind this address and the search client will connect to it. # Default is loopback address. # As a security precaution, should NOT be set to a publicly available address. #sonar.search.host=#--------------------------------------------------------------------------------------------------# UPDATE CENTER# Update Center requires an internet connection to request https://update.sonarsource.org # It is enabled by default. #sonar.updatecenter.activate=true# HTTP proxy (default none) #http.proxyHost=#http.proxyPort=# HTTPS proxy (defaults are values of http.proxyHost and http.proxyPort) #https.proxyHost=#https.proxyPort=# NT domain nameifNTLM proxy is used #http.auth.ntlm.domain=# SOCKS proxy (default none) #socksProxyHost=#socksProxyPort=# Proxy authentication (usedforHTTP, HTTPS and SOCKS proxies) #http.proxyUser=#http.proxyPassword=#--------------------------------------------------------------------------------------------------# LOGGING# SonarQube produces logsin 4 logs files located inthe same directory (see property sonar.path.logs below), # one per process: # Main process (aka. App) logsinsonar.log # Web Server (aka. Web) logsinweb.log # Compute Engine (aka. CE) logsince.log # Elasticsearch (aka. ES) logsines.log # # All4files follow the same rolling policy (see sonar.log.rollingPolicy and sonar.log.maxFiles) but it applies # individually (eg.if sonar.log.maxFiles=4, there can be at most 4 of each files, ie. 16 files intotal). # # All4 files have logs inthe same format: #1 2 3 4 5 6#|-----------------| |---| |-|--------------------||------------------------------| |------------------------------------------------------------------------------------------------------------------------------|#2016.11.16 16:47:00 INFO ce[AVht0dNXFcyiYejytc3m][o.s.s.c.t.CeWorkerCallableImpl] Executed task | project=org.sonarqube:example-java-maven | type=REPORT | id=AVht0dNXFcyiYejytc3m | submitter=admin | time=1699ms # #1: timestamp. Format is YYYY.MM.DD HH:MM:SS # YYYY: year on4digits # MM: month on2digits # DD: day on2digits # HH: hour of day on2 digits in 24hours format # MM: minutes on2digits # SS: seconds on2digits #2: log level. # Possible values (inorder of descending criticality): ERROR, WARN, INFO, DEBUG and TRACE #3: process identifier. Possible values: app (main), web (Web Server), ce (Compute Engine) and es (Elasticsearch) #4: SQ thread identifier. Can be empty. # In the Web Server,ifpresent, it will be the HTTP request ID. # In the Compute Engine,ifpresent, it will be the task ID. #5: logger name. Usually a class canonical name. # Package names are truncated to keep the whole field to20characters max #6: log payload. Content of this field does not follow any specific format, can vary inlength and include line returns. # Some logs, however, will follow the convention to provide datain payload in the format "| key=value"# Especially, log of profiled pieces of code will end with"| time=XXXXms".# Global level of logs (applies to all4processes). # Supported values are INFO (default), DEBUG and TRACE #sonar.log.level=INFO# Level of logs of each process can be controlled individually with their respective properties. # When specified, they overwrite the level defined at global level. # Supported values are INFO, DEBUG and TRACE #sonar.log.level.app=INFO #sonar.log.level.web=INFO #sonar.log.level.ce=INFO #sonar.log.level.es=INFO# Path to log files. Can be absolute or relative to installation directory. # Default is<installation home>/logs #sonar.path.logs=logs# Rolling policy of log files #- based on time if value starts with "time:", for example by day ("time:yyyy-MM-dd") # or by month ("time:yyyy-MM") #- based on size if value starts with "size:", for example "size:10MB"#- disabled if value is "none". That needs logs to be managed by an external system like logrotate. #sonar.log.rollingPolicy=time:yyyy-MM-dd# Maximum number of files to keepifa rolling policy is enabled. #- maximum value is 20on size rolling policy #- unlimited on time rolling policy. Set to zero to disable old filepurging. #sonar.log.maxFiles=7# Access log is the list of all the HTTP requests received by server. If enabled, it is stored #in the file {sonar.path.logs}/access.log. This file follows the same rolling policy as other log file# (see sonar.log.rollingPolicy and sonar.log.maxFiles). #sonar.web.accessLogs.enable=true# Format of access log. It is ignoredif sonar.web.accessLogs.enable=false. Possible values are: #- "common" is the Common Log Format, shortcut to: %h %l %u %user %date "%r" %s %b #- "combined" is another format widely recognized, shortcut to: %h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"#- else a custom pattern. See http://logback.qos.ch/manual/layouts.html#AccessPatternLayout. # The login of authenticated user is not implemented with "%u" but with "%reqAttribute{LOGIN}" (since version 6.1). # The value displayedfor anonymous users is "-". # The SonarQube's HTTP request ID can be added to the pattern with "%reqAttribute{ID}" (since version 6.2). # If SonarQube is behind a reverse proxy, thenthe following value allows to display the correct remote IP address: #sonar.web.accessLogs.pattern=%i{X-Forwarded-For} %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}" "%reqAttribute{ID}"# Default value (which was "combined" before version 6.2) is equivalent to "combined + SQ HTTP request ID": #sonar.web.accessLogs.pattern=%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}" "%reqAttribute{ID}"#--------------------------------------------------------------------------------------------------# OTHERS# Delayin seconds between processing of notification queue. Default is 60seconds. #sonar.notifications.delay=60# Paths to persistent data files (embedded database and search index) and temporary files. # Can be absolute or relative to installation directory. # Defaults are respectively<installation home>/data and <installation home>/temp #sonar.path.data=data #sonar.path.temp=temp# Telemetry-Share anonymous SonarQube statistics # By sharing anonymous SonarQube statistics, you help us understand how SonarQube is used so we can improve the product to work even betterforyou. # We don't collect source code or IP addresses. And we don't share the data with anyone else. # To see an example of the data shared:login as a global administrator, call the WS api/system/infoand check the Statistics field. #sonar.telemetry.enable=true#--------------------------------------------------------------------------------------------------# DEVELOPMENT- only fordevelopers # The following properties MUST NOT be usedinproduction environments.# Elasticsearch HTTP connector #sonar.search.httpPort=-1
重新启动sonar,好了测试一下
3、安装sonar-scanner
3.1 下载
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
3.2 解压
下载后放在/usr/local目录下,并解压
unzip sonar-scanner-cli-3.2.0.1227-linux.zip
3.3 配置环境变量
和上面一样修改/etc/profile文件,修改完记得 source /etc/profile
#set sonarqube export SONAR_HOME=/usr/local/sonarqube-7.4 export SONAR_SCANNER_HOME=/usr/local/sonar-scanner-3.2.0.1227-linux PATH=$PATH:$SONAR_HOME/bin:$SONAR_SCANNER_HOME/bin
修改完记得 source /etc/profile
3.4 测试
进入/usr/local/sonar-scanner-3.2.0.1227-linux/bin执行:
./sonar-scanner -h
如果出现如下就说明安装好了
[root@iZbp1bb2egi7w0ueys548qZ bin]# ./sonar-scanner -h INFO: INFO: usage: sonar-scanner [options] INFO: INFO: Options: INFO: -D,--define <arg> Define property INFO: -h,--help Display help information INFO: -v,--version Display version information INFO: -X,--debug Produce execution debug output
4、在Jenkins中配置sonar
SonarScanner和SonarQube的关系类似于客户端与服务端,由于SonarScanner工具需要把扫描的代码及结果发送到SonarQube服务器上,所以需要配置SonarQube服务地址。
在Jenkins系统配置>>系统设置中配置如下:
5、在Jenkins中配置sonar-scanner
在全局工具配置中配置如下:
6、增加构建步骤
Analysis properties 内容如下
sonar.projectKey=e-auth sonar.projectName=电商用户服务-auth sonar.projectVersion=1.0sonar.sources=./sonar.language=java sonar.sourceEncoding=UTF-8sonar.java.binaries=./sonar.login=admin sonar.password=admin
7、构建一下
8、添加自定义邮件模板
在jenkins所在机器上/root/.jenkins目录下,新建/sonar_script文件夹,里面放如下脚本:sonar.py和table.html
sonar.py内容如下
#!/usr/bin/python#-*- coding:utf-8 -*-importpymysql,os,sysfrom jinja2 importFileSystemLoader,Environmentdefselect_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)defselect_total_info(p_uuid):total_info=[]#使用cursor()方法获取操作游标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()returnp_links,total_infodefselect_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()returnbugsdefselect_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()returnleaksdefselect_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])returntastesdb.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')) #创建一个包加载器对象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内容:
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="GBK"> <body> <pstyle="font-weight:bold;">一、总体情况:</p> <ul> <listyle="font-weight:bold;">整体运行情况:扫描代码行数:<spanstyle="color:blue">{{lins}}</span>, bugs:<spanstyle="color:red">{{total_data[0]}}</span>, 漏洞:<spanstyle="color:red">{{total_data[1]}}</span>, 坏味道:<spanstyle="color:red">{{total_data[2]}}</span></li> <listyle="font-weight:bold;">URL地址:<astyle="font-weight:bold;"href={{report_url}}>{{report_url}}</a></li> </ul> <pstyle="font-weight:bold;">二、错误信息详情:</p> <tableborder="1"cellpadding="10"width="540"height="120"><tr><th></th><th>阻断</th><th>严重</th><th>主要</th><th>次要</th><th>提示</th><th>总数</th></tr><trbgcolor=#ECFFFF><td>bugs</td><tdalign="center">{{bugs[0]}}</td><tdalign="center">{{bugs[1]}}</td><tdalign="center">{{bugs[2]}}</td><tdalign="center">{{bugs[3]}}</td><tdalign="center">{{bugs[4]}}</td><tdalign="center"style="color:red">{{total_data[0]}}</td></tr><trbgcolor=#D2E9FF><td>漏洞</td><tdalign="center">{{leaks[0]}}</td><tdalign="center">{{leaks[1]}}</td><tdalign="center">{{leaks[2]}}</td><tdalign="center">{{leaks[3]}}</td><tdalign="center">{{leaks[4]}}</td><tdalign="center"style="color:red">{{total_data[1]}}</td></tr><trbgcolor=#ECFFFF><td>坏味道</td><tdalign="center">{{tastes[0]}}</td><tdalign="center">{{tastes[1]}}</td><tdalign="center">{{tastes[2]}}</td><tdalign="center">{{tastes[3]}}</td><tdalign="center">{{tastes[4]}}</td><tdalign="center"style="color:red">{{total_data[2]}}</td></tr> </table> <br></br> </body> </html>
9、添加执行自定义生成邮件模板步骤
前提:由于要执行sonar.py脚本,所以jenkins所在机器要有python3环境,且安装了pymysql、jinja2,
centos安装python3:
进入到sonar.py所在目录,执行命令:python3 sonar.py 项目名
执行完成后会在/root/.jenkins/sonar_script目录下生成html文件,
10、配置发送邮件
10.1 安装插件:Email Extension
10.2 在系统管理》》系统设置中设置
11、发送邮件
在job中添加步骤
内容选择HTML,打开高级选项
增加触发器,并打开高级选项
输入发送邮箱列表,以英文逗号分隔,和邮件内容,html就是上面步骤生成的
最终的邮件如下图:
转载于:https://www.cnblogs.com/gcgc/p/10829948.html
jenkins:集成sonar代码扫描+发送邮件相关推荐
- Jenkins集成Sonar(3/3)- 安装SonarQube Scanner进行扫描(离线安装)
安装方式选择: 在线安装:直接在Jenkins>系统管理>插件管理>可选插件,搜索Sonar 离线安装:下载sonar插件,将插件解压放入~/.jenkins/plugins/下即可 ...
- 基于docker使用jenkins集成sonar
前言 基于jenkins可以完成项目的可持续集成,同时由于jenkins强大的插件管理功能,可以集成众多的其他插件完成项目管理中的其他功能,比如可以使用jenkins集成sonar完成对项目构建的时候 ...
- 看这里,全网最详细的Sonar代码扫描平台搭建教程
01 Sonar安装 官网:https://www.sonarqube.org/ 1.sonar简介 sonar是一款静态代码质量分析工具,支持Java.Python.PHP.JavaScript.C ...
- Jenkins集成Sonar Qube
前文目录 Jenkins安装部署使用_南宫乘风的博客-CSDN博客 Jenkins入门配置_南宫乘风的博客-CSDN博客 Sonar Qube介绍 Sonar Qube是一个开源的代码分析平台,支持J ...
- Sonar代码扫描常见规则总结
Sonar代码扫描常见规则 最近公司项目交付,交付前集成,功能,性能,安全种种测试工作就来了,由于测试离职,被抓壮丁,兼职起测试修改工作.小公司,平时敲(ctrl+c)代码(ctrl+v) 时,同事也 ...
- 【sonar集成jenkins实现静态代码扫描】
一 jenkins安装 由于公司的jenkins是用于做服务部署,不能够安装集成需要的sonarqube-scanner插件,所以我们需要自己安装一个jenkins,jenkins的安装可以通过hom ...
- java代码扫描项目,sonar代码扫描实现(基于java-maven)
Sonar介绍 Sonar是一个用于代码扫描检测的开放平台.可以实现对不同语言(java.python.php.C++.C#等等)的项目代码进行分析,同时可以集成不同的测试工具.代码分析工具,以及持续 ...
- jenkins集成sonar
1.sonar介绍 sonar是sonarqube的缩写,是一款用来检查代码质量的开源软件. sonar由两部分组成: ----sonarqube(监控端,sonar server):用来收集监控信息 ...
- jenkins配置sonar并扫描C#代码
背景:我的jenkins搭在linux上 1.下载插件 下载插件sonarqube scanner,用来集成sonarqube,在配置任务的时候才有sonarscanner的选项. 2.全局工具配置 ...
最新文章
- 快速求区间和的有趣算法——树状数组
- 捕捉mysql中不可忽视的知识点(一)
- Siri:开启智能语音营销时代
- 一键生成表结构说明文档的参考,数据字典生成方式参考
- C#程序集相关的概念
- mariadb数据库增删改查
- struts2学习(3)struts2核心知识II
- android搜索框功能实现_巧用 Trie 树,实现搜索引擎关键词提示功能
- 面向对象编程,设计原则,设计模式
- IIS网站本机可以访问但局域网其他机器无法访问 解决方法
- 计算机应用基础试卷结果分析,计算机应用基础试卷分析
- 阿米洛键盘取消win_阿米洛键盘驱动|阿米洛樱花键盘驱动下载v1.0官方版 - 欧普软件下载...
- 电动汽车相关功率计算
- 剪辑师的基本素养--了解四种特写类型
- 2018高中计算机学考,关于2018—2019学年普通高中学业水平考试科目考试时间安排的通知...
- bzoj4564: [Haoi2016]地图 仙人掌的圆方树 莫队 分块
- 【raspberry pi】树莓派3测评
- windows神器,让你的效率直线提升
- 学英语看美剧学地道英文必看的10部美剧!
- LeetCode刷题之---上一个排序