SonarQube代码扫描

2023-11-17

一、代码质量测试

1. 代码测试工具 SonarQube:

官方网站:https://www.sonarqube.org/

SonarQube 是一个用于代码质量管理的开放平台,通过插件机制,SonarQube 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins等)不同,SonarQube并不是简单的把不同的代码检查工具结果(例如FindBugs,PMD等)直接显示在web页面上,而是通过不同的插件针对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便的对不同规模和种类的工程进行代码质量管理。在对其他工具的支持方面,Sonar不仅提供了对IDE的支持,可以在Eclipse和Interlij IDEA 这些工具里联机查看结果,同时Sonar还对大量的持续集成工具提供了接口支持,可以很方便的在持续集成中使用SnoarQube的插件还可以对java以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。
2. 安装sonarqube
在jenkins-master中安装sonarqube

[root@jenkins src]# ll
-rw-r--r-- 1 root root 143286376 Aug 22 16:56 sonarqube-6.5.zip
[root@jenkins src]# unzip sonarqube-6.5.zip 
Archive:  sonarqube-6.5.zip
   creating: sonarqube-6.5/
   creating: sonarqube-6.5/bin/
   creating: sonarqube-6.5/bin/windows-x86-32/
  inflating: sonarqube-6.5/bin/windows-x86-32/wrapper.exe  
   creating: sonarqube-6.5/bin/windows-x86-32/lib/
  inflating: sonarqube-6.5/bin/windows-x86-32/lib/wrapper.dll  
   creating: sonarqube-6.5/bin/macosx-universal-64/
[root@jenkins src]# ln -s /app/src/sonarqube-6.5 /app/sonarqube
[root@jenkins src]# ll /app/
  tal 0
▽rwxrwxrwx 1 root root  21 Aug 21 17:49 jdk -> /app/src/jdk1.8.0_60/
lrwxrwxrwx 1 root root  22 Aug 22 17:02 sonarqube -> /app/src/sonarqube-6.5
drwxr-xr-x 4 root root 170 Aug 22 17:01 src

修改sonarqube配置文件

[root@jenkins conf]# vim /app/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar   #配置数据库用户名
sonar.jdbc.password=123456  #配置数据库密码
sonar.web.port=9000
sonar.web.host=0.0.0.0

3. 安装数据库(注意:只支持5.6以上)

#将数据库导入linux,执行mysql安装脚本(如果没有mysql用户必须先创建mysql用户)
[root@jenkins-slave tools]# ll
total 498272
-rw-r--r-- 1 root root       256 Aug 22 17:11 my.cnf
-rw-r--r-- 1 root root 328979165 Aug 22 17:05 mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root      1470 Aug 22 17:11 mysql-install.sh
[root@jenkins-slave tools]# sh mysql-install.sh 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package gcc-4.8.5-36.el7_6.2.x86_64 already installed and latest version
Package gcc-c++-4.8.5-36.el7_6.2.x86_64 already installed and latest version
Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version
Package autoconf-2.69-11.el7.noarch already installed and latest version
Package net-tools-2.0-0.24.20131004git.el7.x86_64 already installed and latest version
Package lrzsz-0.12.20-36.el7.x86_64 already installed and latest version
Package iotop-0.6-4.el7.noarch already installed and latest version
Package lsof-4.87-6.el7.x86_64 already installed and latest version
Package iotop-0.6-4.el7.noarch already installed and latest version
Package 1:bash-completion-2.1-6.el7.noarch already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.160-5.el7 will be updated
---> Package vim-enhanced.x86_64 2:7.4.160-6.el7_6 will be an update
--> Processing Dependency: vim-common = 2:7.4.160-6.el7_6 for package: 2:vim-enhanced-7.4.160-6.el7_6.x86_64
--> Running transaction check
---> Package vim-common.x86_64 2:7.4.160-5.el7 will be updated
---> Package vim-common.x86_64 2:7.4.160-6.el7_6 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================
 Package                         Arch                      Version                                Repository                  Size
===================================================================================================================================
Updating:
 vim-enhanced                    x86_64                    2:7.4.160-6.el7_6                      updates                    1.0 M
Updating for dependencies:
 vim-common                      x86_64                    2:7.4.160-6.el7_6                      updates                    5.9 M

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

‘/usr/bin/mysql’ -> ‘/usr/local/mysql/bin/mysql’
‘/var/lib/mysql/mysql.sock’ -> ‘/data/mysql/mysql.sock’
Starting MySQL......... SUCCESS! 

创建数据库

[root@jenkins-slave tools]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.01 sec)
#创建并授权用户
mysql> grant all on sonar.* to sonar@"10.0.0.%" identified by "123456";
Query OK, 0 rows affected (0.01 sec)

授权完之后再jenkins-master中查看mysql是否能够登陆到10.0.0.177

[root@jenkins conf]# mysql -usonar -p123456 -h 10.0.0.177
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 
MySQL [(none)]> exit

4. 启动sonarQube

[root@jenkins linux-x86-64]# /app/sonarqube/bin/linux-x86-64/sonar.sh  --help 
Usage: /app/sonarqube/bin/linux-x86-64/sonar.sh { console | start | stop | restart | status | dump }
[root@jenkins linux-x86-64]# /app/sonarqube/bin/linux-x86-64/sonar.sh  start
Starting SonarQube...
Started SonarQube.

启动成功后可以登陆界面 10.0.0.175:9000 (master)
在这里插入图片描述
用户名和密码都是admin——admin
请添加图片描述
sonarQube也是基于插件的,所以需要安装插件
请添加图片描述
汉化包不兼容6.x版本
请添加图片描述
导入汉化包到 /app/sonarqube/extensions/plugins/

[root@jenkins sonarqube]# cd extensions/plugins/
[root@jenkins plugins]# 
#这里面都是java插件包
[root@jenkins plugins]# ll
total 44832
-rw-r--r-- 1 root root      129 Aug  1  2017 README.txt
-rw-r--r-- 1 root root 11069007 Jul 31  2017 sonar-csharp-plugin-5.10.1.1411.jar
-rw-r--r-- 1 root root  1618672 Jul 31  2017 sonar-flex-plugin-2.3.jar
-rw-r--r-- 1 root root  4900103 Aug  1  2017 sonar-java-plugin-4.12.0.11033.jar
-rw-r--r-- 1 root root  3261889 Aug  1  2017 sonar-javascript-plugin-3.1.1.5128.jar
-rw-r--r-- 1 root root  3733262 Jul 31  2017 sonar-php-plugin-2.10.0.2087.jar
-rw-r--r-- 1 root root  4024311 Aug  1  2017 sonar-python-plugin-1.8.0.1496.jar
-rw-r--r-- 1 root root  3233128 Jul 31  2017 sonar-scm-git-plugin-1.2.jar
-rw-r--r-- 1 root root  6676141 Aug  1  2017 sonar-scm-svn-plugin-1.5.0.715.jar
-rw-r--r-- 1 root root  7368250 Aug  1  2017 sonar-xml-plugin-1.4.3.1027.jar
[root@jenkins plugins]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring sonar-l10n-zh-plugin-1.11.jar...
  100%      35 KB      35 KB/sec    00:00:01       0 Errors  
#上传进去之后重启sonarqube

重新进入网址已经是中文的了
请添加图片描述

二、部署扫描器 sonar-scanner

  1. 部署sonar-scanner
    sonarqube通过调用扫描器sonar-scanner 进行代码质量分析,即扫描器的具体工作就是扫描代码。
    然后sonar-scanner 扫描完成后会把扫描结果也写入到数据库中,所以sonarqube是基于数据库读出结果,所以就可以查到扫描结果了

下载地址:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/

[root@jenkins src]# unzip sonar-scanner-2.6.1.zip 
Archive:  sonar-scanner-2.6.1.zip
   creating: sonar-scanner-2.6.1/bin/
  inflating: sonar-scanner-2.6.1/bin/sonar-scanner  
  inflating: sonar-scanner-2.6.1/bin/sonar-runner  
   creating: sonar-scanner-2.6.1/conf/
  inflating: sonar-scanner-2.6.1/conf/sonar-scanner.properties  
   creating: sonar-scanner-2.6.1/lib/
  inflating: sonar-scanner-2.6.1/lib/sonar-scanner-cli-2.6.1.jar  
  inflating: sonar-scanner-2.6.1/bin/sonar-runner.bat  
  inflating: sonar-scanner-2.6.1/bin/sonar-scanner.bat  

修改配置文件(连接数据库的)

[root@jenkins bin]# vim /app/sonar-scanner/conf/sonar-scanner.properties 
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=123456

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
sonar.jdbc.url=jdbc:mysql://10.0.0.177:3306/sonar?useUnicode=true&characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
~                                                                          

sonar-scanner 是不需要启动的,他是被调用的,需要在扫描的目录执行这个命令,代码在哪里路径就在哪执行

[root@jenkins bin]# /app/sonar-scanner/bin/sonar-scanner 

然后上传各种语言的代码进行测试

#里面存放着各种语言的代码
[root@jenkins python-sonar-runner]# ll /app/src/sonar-examples-master
total 4
drwxr-xr-x 7 root root 130 Jul 25  2016 plugins
drwxr-xr-x 5 root root  85 Jul 25  2016 projects
-rw-r--r-- 1 root root 493 Jul 25  2016 README.md

可以进入到目录中进行查看,可以看到各种语言的代码,我们进入到python中对python代码进行质量测试

[root@jenkins languages]# cd /app/src/sonar-examples-master/projects/languages/
[root@jenkins languages]# ls
abap     cobol   css     generic-coverage  javascript      php    python                    swift  web
android  cpp     erlang  groovy            multi-language  pli    rpg                       vb6    xml
c        csharp  flex    java              objc            plsql  sonar-project.properties  vbnet

然后进入到python-sonar-runner中进行代码测试

[root@jenkins sonar-scanner]# cd /app/src/sonar-examples-master/projects/languages/python/python-sonar-runner
[root@jenkins python-sonar-runner]# ls
README.md  sonar-project.properties  src  validation.txt
[root@jenkins python-sonar-runner]# /app/sonar-scanner/bin/sonar-scanner 
INFO: Scanner configuration file: /app/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /app/src/sonar-examples-master/projects/languages/python/python-sonar-runner/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-957.12.2.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global settings
INFO: Load global settings (done) | time=261ms
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=116ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
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=122ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=246ms
INFO: Load active rules
INFO: Load active rules (done) | time=1754ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=254ms
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=331ms
INFO: Language is forced to py
INFO: Base dir: /app/src/sonar-examples-master/projects/languages/python/python-sonar-runner
INFO: Working dir: /app/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=11ms
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=614ms
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=10ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=35ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=61ms
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 112ms, dir size=54 KB
INFO: Analysis reports compressed in 27ms, zip size=27 KB
INFO: Analysis report uploaded in 1424ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost: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://localhost:9000/api/ce/task?id=AWy72tmj-QGXbgTpuyz-
INFO: Task total time: 9.425 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 12.871s
INFO: Final Memory: 43M/107M
INFO: ------------------------------------------------------------------------

然后到sonar-qube网站中查看已经多了一个代码扫描结果
请添加图片描述
进入到里面可以看到报错代码的具体行数
请添加图片描述
我们可以自己写段代码进行测试

[root@jenkins python-test]# mkdir src
#手动创建一个python代码目录
[root@jenkins python-test]# pwd
/app/src/sonar-examples-master/projects/languages/python/python-test
#将python中别的配置文件cp一份
[root@jenkins python-test]# cp ../python-sonar-runner/sonar-project.properties .
[root@jenkins python-test]# ll
total 4
-rw-r--r-- 1 root root 338 Aug 23 15:02 sonar-project.properties
drwxr-xr-x 2 root root   6 Aug 23 15:01 src
#将src目录改名
[root@jenkins python-test]# mv src source
#修改配置文件
[root@jenkins python-test]# vim sonar-project.properties 
# Required metadata
sonar.projectKey=python-test
sonar.projectName=python-test
sonar.projectVersion=1.0
  
▽ Comma-separated paths to directories with sources (required)
sonar.sources=source

# Language
sonar.language=py

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

进入代码目录中写一段正确的python代码

[root@jenkins python-test]# cd source/
[root@jenkins source]# vim test.py
#/usr/bin/env python
print "hello world"
#然后可以测试,可以看到代码是正确的
[root@jenkins source]# python test.py 
hello world

然后修改一下python代码让代码出错然后进行测试,可以看到执行代码已经报错

[root@jenkins source]# vim test.py    
#/usr/bin/env python
print "hello world"
print hello
[root@jenkins source]# python test.py 
hello world
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    print hello
NameError: name 'hello' is not defined

然后执行扫描的命令

[root@jenkins python-test]# /app/sonar-scanner/bin/sonar-scanner 
INFO: Scanner configuration file: /app/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /app/src/sonar-examples-master/projects/languages/python/python-test/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-957.12.2.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global settings
INFO: Load global settings (done) | time=534ms
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=202ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
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=53ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=127ms
INFO: Load active rules
INFO: Load active rules (done) | time=2179ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=160ms
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: python-test
INFO: -------------  Scan python-test
INFO: Load server rules
INFO: Load server rules (done) | time=118ms
INFO: Language is forced to py
INFO: Base dir: /app/src/sonar-examples-master/projects/languages/python/python-test
INFO: Working dir: /app/src/sonar-examples-master/projects/languages/python/python-test/.sonar
INFO: Source paths: source
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 1 file indexed
INFO: Quality profile for py: Sonar way
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=14ms
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=181ms
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=5ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=15ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=15ms
INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: 1 file had no CPD blocks
INFO: Calculating CPD for 0 files
INFO: CPD calculation finished
INFO: Analysis report generated in 90ms, dir size=20 KB
INFO: Analysis reports compressed in 11ms, zip size=6 KB
INFO: Analysis report uploaded in 716ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/python-test
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://localhost:9000/api/ce/task?id=AWy9UfSW-QGXbgTpuy0A
INFO: Task total time: 8.471 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 12.703s
INFO: Final Memory: 43M/106M
INFO: ------------------------------------------------------------------------

此时再去sonarqube网站上查看,已经多了2个问题了
请添加图片描述
然后我们再把source中的python代码修改正确,然后再次测试看看情况

[root@jenkins python-test]# vim source/test.py 
#/usr/bin/env python
print("hello world")

[root@jenkins python-test]# /app/sonar-scanner/bin/sonar-scanner 
INFO: Scanner configuration file: /app/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /app/src/sonar-examples-master/projects/languages/python/python-test/sonar-project.properties
INFO: SonarQube Scanner 2.6.1
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-957.12.2.el7.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Load global settings
INFO: Load global settings (done) | time=142ms
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=8ms
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
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=230ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=61ms
INFO: Load active rules
INFO: Load active rules (done) | time=862ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=73ms
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: python-test
INFO: -------------  Scan python-test
INFO: Load server rules
INFO: Load server rules (done) | time=83ms
INFO: Language is forced to py
INFO: Base dir: /app/src/sonar-examples-master/projects/languages/python/python-test
INFO: Working dir: /app/src/sonar-examples-master/projects/languages/python/python-test/.sonar
INFO: Source paths: source
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 1 file indexed
INFO: Quality profile for py: Sonar way
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=41ms
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=569ms
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=5ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=17ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=25ms
INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: 1 file had no CPD blocks
INFO: Calculating CPD for 0 files
INFO: CPD calculation finished
INFO: Analysis report generated in 128ms, dir size=20 KB
INFO: Analysis reports compressed in 14ms, zip size=6 KB
INFO: Analysis report uploaded in 54ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/python-test
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://localhost:9000/api/ce/task?id=AWy9VWr5-QGXbgTpuy0B
INFO: Task total time: 6.789 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 9.013s
INFO: Final Memory: 43M/106M
INFO: ------------------------------------------------------------------------

再去网址上查看,已经恢复正常
请添加图片描述
请添加图片描述
然后如何将代码扫描放到jenkins中,
代码扫描2中方式:
1,在代码里执行shell命令放进去扫描
2,在代码克隆完之后用jenkins触发,帮我们做扫描

三、Jenkins关联到SonarQube

1. 安装插件,配置jenkins关联到sonarQube
首先安装插件,在jenkins插件安装界面安装Sonar 插件SonarQube Plugin
请添加图片描述
安装成功之后需要到系统配置中配置sonarQube
请添加图片描述
请添加图片描述
填写sonarQube名字,可以随意些,URL需要写sonarqube服务器,我这里jenkins和sonarqube在同一台机器,tonken不用设置请添加图片描述

2. 让jeknins关联到sonarscanner
添加扫描器
系统管理Global Tool Configuration 全局工具配置
请添加图片描述
配置sonarQube Scanner 配置
请添加图片描述

3. 配置扫描
选择自己的项目
请添加图片描述
选择配置项目

请添加图片描述
构建execute sonarqube sacnner
请添加图片描述
将配置文件的内容修改成如下格式,然后将Execute SonarQube Scanner 和Excute Shell调换位置,先扫描后执行命令,填写完成然后点击保存。

sonar.projectKey=testDemo
sonar.projectName=testDemo
sonar.projectVersion=1.0
sonar.source=./   
sonar.language=php
sonar.sourceEncoding=UTF-8

请添加图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SonarQube代码扫描 的相关文章

  • 创建 jar 文件 - 保留文件权限

    我想知道如何创建一个保留其内容的文件权限的 jar 文件 我将源代码和可执行文件打包在一个 jar 文件中 该文件将在使用前提取 人们应该能够通过运行批处理 shell 脚本文件立即运行示例和演示 然后他们应该能够修改源代码并重新编译所有内
  • 仅打印“docker-container ls -la”输出中的“Names”列

    发出时docker container ls la命令 输出如下所示 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a67f0c2b1769 busybox tail f dev
  • 如何禁用 GNOME 桌面屏幕锁定? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 如何阻止 GNOME 桌面在几分钟空闲时间后锁定屏幕 我已经尝试过官方手册了在红帽 https access redhat com doc
  • nginx 上的多个网站和可用网站

    通过 nginx 的基本安装 您的sites available文件夹只有一个文件 default 怎么样sites available文件夹的工作原理以及如何使用它来托管多个 单独的 网站 只是为了添加另一种方法 您可以为您托管的每个虚拟
  • Linux TUN/TAP:无法从 TAP 设备读回数据

    问题是关于如何正确配置想要使用 Tun Tap 模块的 Linux 主机 My Goal 利用现有的路由软件 以下为APP1和APP2 但拦截并修改其发送和接收的所有消息 由Mediator完成 我的场景 Ubuntu 10 04 Mach
  • 无法从 jenkins 作为后台进程运行 nohup 命令

    更新 根据下面的讨论 我编辑了我的答案以获得更准确的描述 我正在尝试从詹金斯运行 nohup 命令 完整的命令是 nohup java jar home jar server process 0 35 jar prod gt gt var
  • Linux中的定时器类

    我需要一个计时器来以相对较低的分辨率执行回调 在 Linux 中实现此类 C 计时器类的最佳方法是什么 有我可以使用的库吗 如果您在框架 Glib Qt Wx 内编写 那么您已经拥有一个具有定时回调功能的事件循环 我认为情况并非如此 如果您
  • 加载数据infile,Windows和Linux的区别

    我有一个需要导入到 MySQL 表的文件 这是我的命令 LOAD DATA LOCAL INFILE C test csv INTO TABLE logs fields terminated by LINES terminated BY n
  • 如何使用GDB修改内存内容?

    我知道我们可以使用几个命令来访问和读取内存 例如 print p x 但是如何更改任何特定位置的内存内容 在 GDB 中调试时 最简单的是设置程序变量 参见GDB 分配 http sourceware org gdb current onl
  • vector 超出范围后不清除内存

    我遇到了以下问题 我不确定我是否错了或者它是一个非常奇怪的错误 我填充了一个巨大的字符串数组 并希望在某个点将其清除 这是一个最小的例子 include
  • 无法使用 wget 在 CentOS 机器上安装 oracle jdk

    我想在CentOS上安装oracle java jdk 8 我无法安装 java jdk 因为当我尝试使用命令安装 java jdk 时 root ADARSH PROD1 wget no cookies no check certific
  • Pyaudio 安装错误 - “命令‘gcc’失败,退出状态 1”

    我正在运行 Ubuntu 11 04 Python 2 7 1 并想安装 Pyaudio 于是我跑了 sudo easy install pyaudio 在终端中 进程退出并显示以下错误消息 Searching for pyaudio Re
  • 如何在Linux内核源代码中打印IP地址或MAC地址

    我必须通过修改 Linux 内核源代码来稍微改变 TCP 拥塞控制算法 但为了检查结果是否正确 我需要记录 MAC 或 IP 地址信息 我使用 PRINTK 函数来打印内核消息 但我感觉很难打印出主机的MAC IP地址 printk pM
  • NPTL 和 POSIX 线程有什么区别?

    NPTL 和 POSIX 线程之间的基本区别是什么 这两者是如何演变的 POSIX 线程 pthread 不是一个实现 它是几个函数的 API 规范 纸上的标准 英文 其名称以pthread 以及定义在
  • Linux:在文件保存时触发 Shell 命令

    我想在修改文件时自动触发 shell 命令 我认为这可以通过注册 inotify 挂钩并调用来在代码中完成system 但是是否有更高级别的 bash 命令可以完成此任务 尝试 inotify 工具 我在复制链接时遇到问题 抱歉 但 Git
  • Linux 可执行文件与 OS X“兼容”吗?

    如果您在基于 Linux 的平台上用 C 语言编译一个程序 然后将其移植以使用 MacOS 库 它会工作吗 来自编译器的核心机器代码在 Mac 和 Linux 上兼容吗 我问这个问题的原因是因为两者都是 基于 UNIX 的 所以我认为这是真
  • Linux:如何从特定端口发送TCP数据包?

    如何打开原始套接字以从特定 TCP 端口发送 我希望所有连接始终来自临时端口以下的一系列端口 如果您正在使用raw套接字 然后只需在数据包标头中填写正确的 TCP 源端口即可 相反 如果您使用 TCP 套接字接口 socket connec
  • 安装J语言的JQt IDE,出现错误

    我一直按照这里的说明进行操作 http code jsoftware com wiki System Installation Linux http code jsoftware com wiki System Installation L
  • iptables通过注释删除特定规则

    我需要删除一些具有相同评论的规则 例如 我有带有 comment test it 的规则 所以我可以像这样获得它们的列表 sudo iptables t nat L grep test it 但是我怎样才能删除所有带有注释 测试它 的 PR
  • jpegtran 优化而不更改文件名

    我需要优化一些图像 但不更改它们的名称 jpegtran copy none optimize image jpg gt image jpg 但是 这似乎创建了 0 的文件大小 当我对不同的文件名执行此操作时 大小仍然完全相同 怎么样 jp

随机推荐

  • 由Vite读音引发的英语颠覆

    前段时间出了个项目Vite 被读成va t 结果后面发现是读vi t 后面又引出了height的读音 我们有些人读he t 有些人读ha t 结果是读ha t 我也是读了20年的he t 因为有个weight是读we t的 所以想当然的以为
  • 对象和字符串之间的相互转换

    原文链接 对象和字符串之间的相互转换 编程屋 相关依赖
  • Python——12306图片验证码

    本次爬虫 我们来模拟一下12306的验证码验证 本次练习用到的模块 requests re base64 urllib3 第一步 按F12查看验证码图片的信息 提取URL https kyfw 12306 cn passport captc
  • 前端页面之间url传参

    function getUrlParam name var reg new RegExp name var r window location search substr 1 match reg ECMAScript v3 已从标准中删除了
  • 如何设置office2003为默认打开方式

    如何设置office2003为默认打开方式 当系统同时安装 office 2003和 office 2007 或2010 两个版本的 office办公软件的时候 双击打开一个office文档 Word Excel Powerpoint 默认
  • 【CV with Pytorch】第 10 章 :计算机视觉的可解释人工智能

    大多数机器学习和深度学习模型都缺乏解释和解释结果的方法 由于深度学习模型的动态特性和不断增加的最先进模型 当前的模型评估基于准确度分数 这使得机器学习和深度学习成为黑盒模型 这导致对应用模型缺乏信心 对生成的结果缺乏信任 有多个库可以帮助我
  • 刷题统计(蓝桥杯)

    刷题统计 问题描述 小明决定从下周一开始努力刷题准备蓝桥杯竞赛 他计划周一至周五每天 做 a 道题目 周六和周日每天做 b 道题目 请你帮小明计算 按照计划他将在 第几天实现做题数大于等于 n 题 输入格式 输入一行包含三个整数 a b 和
  • 数据结构基础:2.顺序表。

    顺序表的介绍和实现 一 线性表 1 基本概念 二 顺序表 1 基本概念 分类 1 静态顺序表 分类 2 动态顺序表 2 动态顺序表的功能接口的实现 0 顺序表打印 1 初始化和删除 2 尾插尾删 3 头插头删 4 任意位置插入删除 5 查找
  • 面试题 01.08. 零矩阵

    难度中等 编写一种算法 若M N矩阵中某个元素为0 则将其所在的行与列清零 示例 1 输入 1 1 1 1 0 1 1 1 1 输出 1 0 1 0 0 0 1 0 1 示例 2 输入 0 1 2 0 3 4 5 2 1 3 1 5 输出
  • 并发编程相关面试题(持续更新)

    1 juc包提供了哪些工具 用在什么场景 1 提供了比 synchronized 更加高级的各种同步结构 包括 CountDownLatch CyclicBarrier Semaphore 等 场景 可以实现更加丰富的多线程操作 比如利用
  • TTL反相器、OC门、TS门、推挽输出、开漏输出

    TTL反相器 这是一个TTL反相器 这是经过了很多工程师多种设计最终沉淀出来的电路 这个电路是比较成熟的 我们只需要对这个电路进行解析即可 不需要再去研究当初是如何设计出来的 学过CMOS应该知道 右侧的输出级其实也是个推挽输出 因为长得像
  • 记录一次小问题!ssh脚本打包vue 执行 cnpm run build 无法继续向下走

    问题 ssh脚本打包vue 执行 cnpm run build 无法继续向下走 发现一直停留在 DONE Build complete The dist directory is ready to be deployed INFO Chec
  • JS内存泄漏排查

    定义 内存泄漏 应用程序不再需要的内存 由于某种原因 内存没有返回到操作系统或可用内存池中 原因 1 意外的全局变量 JS 在处理未声明的变量时 对未声明的变量的引用会在全局对象内创建一个新变量 这些全局变量是无法进行垃圾回收的 除非将它们
  • 东方财富四千余支股票2023年度上半年收盘价格走势涨跌可视化分析

    本文数据和图表内容仅供参考 数据仅供参考 数据仅供参考 数据仅供参考 如下是案例A300028的股票2023年收盘价走势及涨跌幅曲线 案例 A股300024 案例 600182走势图 走势分析图谱 原始结果已保存到压缩文件 通过python
  • Python+Selenium练习篇之18-自定义浏览器窗口大小

    本文来学习下如何通过Selenium方法 设置符合不同测试场景浏览器窗口大小 例如 你有一台机器 最大支持1366 768 你完全可以利用这个机器测试不同分辨率下的场景 相关测试脚本代码如下 coding utf 8 import time
  • 区块链技术的应用和数字资产,将会是未来不可阻挡的趋势和潮流

    前言 数字资产的发展 很可能是人类近千年金融史上又一次激动人心的变革 如果让你回到七年前重新选择一次投资机会的话 你可能会选择投资股票 然后坐等15年大牛市资产翻几倍 你也可能会投资房地产 然后坐等16年房地产爆发 资产翻番 但你万万想不到
  • javascript正则表达式学习

    REGEXP对象 javascript通过内置对象RegExp支持正则表达式 有两种方法实例化RegExp对象 1 字面量 var reg bis b var reg2 bis b g 正则规则放在 内 bv表示单词边界 g表示全文搜索 如
  • 树莓派作为客户端与WemosD1作为服务器的无线通信(局域网通信)

    参考 树莓派作为客户端与WemosD1作为服务器的无线通信 局域网通信 作者 图触靓 发布时间 2020 07 26 21 50 39 网址 https blog csdn net bhbhhyg article details 10759
  • 2023年“大湾区杯” AI for Science正式开赛!

    2023年 大湾区杯 粤港澳 AI for Science 科技竞赛 赛题发布 01 AI for Science 赛题设置 本次竞赛设置三大赛道 分别为常规赛道 题号CG2301 CG2312 百度赛道 题号BD2301 BD2308 华
  • SonarQube代码扫描

    一 代码质量测试 1 代码测试工具 SonarQube 官方网站 https www sonarqube org SonarQube 是一个用于代码质量管理的开放平台 通过插件机制 SonarQube 可以集成不同的测试工具 代码分析工具