学习Jenkins(一)
1 2 3 4 5 6 7 8 9 | [root@linux-node2 ~] # yum install -y java-1.8.0 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.cn99.com Package 1:java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 already installed and latest version Nothing to do [root@linux-node2 ~] # |
2、安装Jekins ?
1 2 3 4 5 | cd /etc/yum .repos.d/ wget http: //pkg .jenkins.io /redhat/jenkins .repo rpm -- import http: //pkg .jenkins.io /redhat/jenkins .io.key yum install -y jenkins systemctl start jenkins |
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 3、检查启动情况。看到占用端口默认是8080 ?
1 2 3 4 5 6 7 8 9 10 11 12 | [root@linux-node2 yum.repos.d] # netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1 /systemd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1151 /sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1244 /master tcp6 0 0 :::3306 :::* LISTEN 5755 /mysqld tcp6 0 0 :::111 :::* LISTEN 1 /systemd tcp6 0 0 :::8080 :::* LISTEN 8727 /java tcp6 0 0 :::22 :::* LISTEN 1151 /sshd tcp6 0 0 ::1:25 :::* LISTEN 1244 /master [root@linux-node2 yum.repos.d] # |
4、访问Jenkins 在浏览器输入http://192.168.58.12:8080来访问jenkins。 为了安全考虑,首先需要解锁Jenkins,请在/var/lib/jenkins/secrets/initialAdminPassword中查看文件。 5、密码 [root@linux-node2 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword 4687aa33576b4b748888bb60a88b2e4c [root@linux-node2 ~]# 页面上需要把上面的密码输入进去

复制密码进去

Jenkins只是个平台,真正干活的是插件 啥插件都有 选择它推荐的

安装过程
由于网络原因,有一些插件会安装失败 可以点击retry。很高概率会成功 下面的Subversion Plug-in 是svn插件

OWASP插件,它每年出一个top 10 的安全漏洞
开放式Web应用程序安全项目(OWASP)是一个非营利组织,不附属于任何企业或财团。
因此,由OWASP提供和开发的所有设施和文件都不受商业因素的影响。
OWASP支持商业安全技术的合理使用,它有一个论坛,在论坛里信息技术专业人员可以发表和传授专业知识和技能。

设置Admin用户和密码

设置用户名和密码

登陆Jenkins


修复之后,重启jenkins,打开页面不报错了

Hudson是Jenkins的前身,是基于Java开发的一种持续集成工具,用于监控程序重复的工作,
包括:
1、持续的软件版本发布/测试项目。
2、监控外部调用执行的工作。
HUDSON被收购了。,然后写这个的人又搞了个Jenkins
装gitlab插件
目前已经部署好了gitlab,准备和gitlab集成。要安装gitlab插件
右边搜索gitlab
勾选这2个

下载中



创建任务

构建一个自由风格的软件项目


源码管理这里可以看到它支持git

系统管理--系统设置

看到url等

邮件相关

已经安装的插件

高级这里可以看到
更新插件的时候可以使用代理
也可以本地上传插件

或者你放在这个目录下,可以把别人的整个目录拷贝过来,重启Jenkins即可生效 ?
1 2 3 4 | [root@linux-node2 plugins] # cd /var/lib/jenkins/plugins/ [root@linux-node2 plugins] # pwd /var/lib/jenkins/plugins [root@linux-node2 plugins] # |
jenkins官网和官方文档 jenkins官网 https://jenkins.io/index.html

一些文档 https://jenkins.io/doc/developer/

插件列表 http://javadoc.jenkins.io/plugin/

它还有salt插件呢



点击add弹出下面界面

类型里可以选择




点击下面
和刚才一样
配置deploy key
我们可以配置deploy key
如下配置
node2生成密钥对 ?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | [root@linux-node2 ~] # ssh-keygen -t rsa Generating public /private rsa key pair. Enter file in which to save the key ( /root/ . ssh /id_rsa ): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/ . ssh /id_rsa . Your public key has been saved in /root/ . ssh /id_rsa .pub. The key fingerprint is: 23:21:be:de:78:f7:82:40:b8:38:fa:6f:05:39:06:86 root@linux-node2.nmap.com The key's randomart image is: +--[ RSA 2048]----+ | . | |E o | | . o... | | ..*. . | | . +.o. S | |o . .... . | |.. .o . | |. ..oo o | | ..o+... o. | +-----------------+ [root@linux-node2 ~] # [root@linux-node2 ~] # cd .ssh/ [root@linux-node2 . ssh ] # cat id_rsa.pub ssh -rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz0QBxB9kIv8H6Uxe+clLvM99 /7QOYTCVZzQKb0Q9Song1Yy9oryCcYWHogN30j80Ctbybu +3PAh4VIfz4SjY1tNd /Dv2Il3caz +5chvX0OL DJCNl2w+Du7HjwdyszlzVc1rP4QTL /yYvrUz1fgBonRKa0/Y5CK7jEzQZ1 +ZMuN /E/ubrDX5FIp0jfPPnbtTryWW8BMloOb +Q8EFKnaI6Wbyx9JzslOIawCkM77LL9RhxMbQ6A3QoU HozmlFnhfu7AXnhfCdPb02xx4RCOAFoY9pEtOH+eHzT7 /SQVf8VzEYO1vIe4TDP6sNN40/1pjUdBPcGiBMidM6pLk/j6CaxV root@linux-node2.nmap.com [root@linux-node2 . ssh ] # |
下面这里此项目的private deploy key设置的地方。

把key复制进去,同时title起一个简单明了的名字

添加成功(public deploy key要删除,防止冲突)

这里是public deploy key设置的地方

如下位置

node2拉取下看看,需要有git工具 ?
1 2 3 4 5 6 7 8 9 | [root@linux-node2 ~] # yum install git -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.cn99.com Package git-1.8.3.1-6.el7_2.1.x86_64 already installed and latest version Nothing to do [root@linux-node2 ~] # |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@linux-node2 ~] # git clone git@192.168.58.11:web/web-demo.git Cloning into 'web-demo' ... remote: Counting objects: 13, done . remote: Compressing objects: 100% (8 /8 ), done . remote: Total 13 (delta 0), reused 0 (delta 0) Receiving objects: 100% (13 /13 ), done . [root@linux-node2 ~] # ls anaconda-ks.cfg tools web-demo [root@linux-node2 ~] # cd web-demo/ [root@linux-node2 web-demo] # ls index.html README.md [root@linux-node2 web-demo] # cat index.html hehehehhehehe version 2.0 [root@linux-node2 web-demo] # |
node2上已经没问题,开始配置jenkins的认证
这里应该输入私钥
复制私钥
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [root@linux-node2 ~] # cat .ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAs9EAcQfZCL /B +lMXvnJS7zPff+0DmEwlWc0Cm9EPUqJ4NWMv aK8gnGFh6IDd9I /NArW8m7vtzwIeFSH8 +Eo2NbTXfw79iJd3Gs /uXIb19DiwyQjZ dsPg7ux48HcrM5c1XNaz+EEy /8mL61M9X4AaJ0SmtP2OQiu4xM0GdfmTLjfxP7m6 w1+RSKdI3zz527U68llvATJaDm /kPBBSp2iOlm8sfSc7JTiGsApDO +yy /UYcTG0O gN0KFB6M5pRZ4X7uwF54XwnT29NsceEQjgBaGPaRLTh /nh80 + /0kFX/FcxGDtbyH uEwz+rDTeNP9aY1HQT3BogTInTOqS5P4+gmsVQIDAQABAoIBAQCKQGMqhHIl++lU YZab4qJJL4TjgoOFEOaWyLz8uBwj2K67vWqx /hQAqA7zCEGeywFA2CIPSspooqQm c19gNw6+9C /LjOb5ohr9Dmmh0lJAV2X060znt2q1yDKkD7uzisqIerscYGSprXha kkFZr9Br6KiT927tCViiV1BkXGMA4NAyFHQQBa493kYZ9tRM89nfzQXK8Ttjulp3 1qD3bPebsMWp6JuCtnFsRwfoYRnsYHHG3FsD5QOIV1tcKiT8v6fV5CklVejOn5O3 +992oNz0UqXuHD0GhFRGRo7Xro2XAsSpyxH0NPv6Aclz9eU6t3hXF+lg33l+tKYJ HUrHvt9xAoGBANnNXmu72pyEhIMbWTw6qR2WAy5UL+hdggDilGbsTX9x33eZiaQR bNT7qcX2CRq7pmXiH4o6LmljkkVYTGS /ZIkp6QZZp9CnE2Ywb0oo +UwfKPAXLMpd 2xBjg03YSzD6ElsPKeVgx78 //UGbcWZWMbw7i9k//ho2j3XMKwHIIABPAoGBANNa MK6VGJDX /PPvre +ktF50bKj4VDWNdT5nEvjuEG98zIEIh5geAgPVYMXeBELlKmvw Az /eto5puvtPF2bXk/Y75xJ/pYha5KIhQYhbt +GLk71pWywu9wvZopkEViVc2Tf9 vIo+SFJEjxgQeotJG0S24kCJfNfoh9oIwGSGDxwbAoGALel7yOwNlkbdBCIkumyH L8RqoT5 /5tSnTfZ1YAjaAU6I09ZazGob7f2q +R73Jlp+JiX3jhTbqb2inNmTKP2r fLqH8528oWBG15jILPLcMm6zG4FXIMWOIzNG /4bjusxMSEO3CjJPiQI9KbJRwkfT LiHDRiWWNAJiVWuXeUWtcUMCgYEAwgmjeCTaEOC7ExmRDtqf3bf1qouShDgC0++9 s1ydKj3hYvV5x2aFYw+bcQ2Ly /6pG7 +WvCS0ZbscN209huWUqhQE6S7Xwn /wOAlg KNhL0X5bfSIk2kOj+ /UrGmCo1lSP +J+zYRgdPapD1UwLtaTArC1bOvT+8RpC3yH5 wFBzvtECgYEAyYLpBV8vNzY7ilCmGctXtj5gsAvxOkYMVdaPkCXDr50qHBYYsHj4 22FdOq75zWgrIQo6Poke3IQcz4NML/+Z08r2qa7+4z5Jx5BnwXAnWEB9omcbANgD m+h33eEXWr12L363c+ /RVdZPINkmhaS69u4/Njh +dI1lL8Ta+8Ihkow= -----END RSA PRIVATE KEY----- [root@linux-node2 ~] # |
复制进去
添加描述
点击OK

继续编辑配置

这样就不报错了 下面还可以指定从哪个分支拉取,默认从master拉取,有些公司使用release分支

源码浏览器

gitlab的url
去gitlab上找到http的url

找出gitlab的版本 ?
1 2 3 | [root@linux-node1 ~] # rpm -qa | grep gitlab gitlab-ce-9.1.0-ce.0.el7.x86_64 [root@linux-node1 ~] # |

版本只支持2位

拉到最下面保存

点击立即构建

查看控制台


从控制台可以看到它的工作路径 ?
1 2 3 4 | [root@linux-node2 ~] # cd /var/lib/jenkins/workspace/auto-deploy [root@linux-node2 auto-deploy] # ls index.html README.md [root@linux-node2 auto-deploy] # |
git show查看版本
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@linux-node2 auto-deploy] # git show commit 7983661ca0d2be489b4442c3d49d137d2a1026df Author: nmap <nmap@ test .com> Date: Fri Apr 28 22:20:59 2017 +0800 2.0 diff --git a /index .html b /index .html index f95f8e7..d77c60b 100644 --- a /index .html +++ b /index .html @@ -1,2 +1,2 @@ hehehehhehehe -version 2.1 +version 2.0 [root@linux-node2 auto-deploy] # |
有个.get的目录 ?
1 2 3 | [root@linux-node2 auto-deploy] # ls -a . .. .git index.html README.md [root@linux-node2 auto-deploy] # |
持续代码质量管理-Sonar部署
能检查代码里有没有bug,比如你代码里有个死循环的bug,不用运行,就能检测出来
它还会给你代码打分(代码质量)
Sonar 是一个用于代码质量管理的开放平台。
通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,
Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,
而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;
同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。
Sonar部署
Sonar的相关下载和文档可以在下面的链接中找到:http://www.sonarqube.org/downloads/。需要注意最新版的Sonar需要至少JDK 1.8及以上版本。
1 2 3 4 5 6 7 8 9 10 | [root@linux-node1 ~] # yum install -y java-1.8.0 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.163.com * updates: mirrors.zju.edu.cn Package 1:java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 already installed and latest version Nothing to do [root@linux-node1 ~] # |
下载sonar包并安装 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | wget https: //sonarsource .bintray.com /Distribution/sonarqube/sonarqube-5 .6.zip [root@linux-node1 src] # pwd /usr/local/src [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonarqube-5.6.zip [root@linux-node1 src] # [root@linux-node1 src] # yum install unzip -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.163.com * updates: mirrors.cn99.com Package unzip-6.0-16.el7.x86_64 already installed and latest version Nothing to do [root@linux-node1 src] # [root@linux-node1 src] # unzip sonarqube-5.6.zip [root@linux-node1 src] # mv sonarqube-5.6 /usr/local/ [root@linux-node1 src] # ln -s /usr/local/sonarqube-5.6/ /usr/local/sonarqube [root@linux-node1 src] # |
准备Sonar数据库 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@linux-node1 src] # /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf & [root@linux-node1 src] # /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.30-log MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> GRANT ALL ON sonar.* TO 'sonar' @ 'localhost' IDENTIFIED BY 'sonar@pw' ; mysql> GRANT ALL ON sonar.* TO 'sonar' @ '%' IDENTIFIED BY 'sonar@pw' ; mysql> FLUSH PRIVILEGES; |
配置Sonar ?
1 2 3 4 | [root@linux-node1 src] # cd /usr/local/sonarqube/conf/ [root@linux-node1 conf] # ls sonar.properties wrapper.conf [root@linux-node1 conf] # |
1 2 3 4 | [root@linux-node1 conf] # vim sonar.properties sonar.jdbc.username=sonar sonar.jdbc.password=sonae@pw sonar.jdbc.url=jdbc:mysql: //localhost :3306 /sonar ?useUnicode= true &characterEncoding=utf8&rewriteBatchedStatements= true &useConfigs=maxPerformance |
修改之后如下
它默认监听的地址和端口 这里不用改,知道就行了

启动Sonar 你可以在Sonar的配置文件来配置Sonar Web监听的IP地址和端口,默认是9000端口。 ?
1 2 3 4 | [root@linux-node1 conf] # /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start Starting SonarQube... Started SonarQube. [root@linux-node1 conf] # |
第一次启动慢,因为它要创建表 打开页面

关于sonar的目录结构 ?
1 2 3 4 5 6 7 | [root@linux-node1 conf] # cd /usr/local/sonarqube [root@linux-node1 sonarqube] # ls bin conf COPYING data extensions lib logs temp web [root@linux-node1 sonarqube] # cd logs/ [root@linux-node1 logs] # ls access.log sonar.log [root@linux-node1 logs] # |
插件存放位置 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@linux-node1 logs] # cd /usr/local/sonarqube [root@linux-node1 sonarqube] # ls bin conf COPYING data extensions lib logs temp web [root@linux-node1 sonarqube] # cd extensions/ [root@linux-node1 extensions] # ll total 4 drwxr-xr-x 2 root root 6 Apr 29 16:34 deprecated drwxr-xr-x 2 root root 6 Apr 29 16:35 downloads drwxr-xr-x 3 root root 19 Jan 11 2016 jdbc-driver drwxr-xr-x 2 root root 4096 Apr 29 16:34 plugins [root@linux-node1 extensions] # cd plugins/ [root@linux-node1 plugins] # pwd /usr/local/sonarqube/extensions/plugins [root@linux-node1 plugins] # [root@linux-node1 plugins] # ll total 21952 -rw-r--r-- 1 root root 128 Apr 11 2016 README.txt -rw-r--r-- 1 root root 7797781 Apr 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 3191477 Apr 28 2016 sonar-java-plugin-3.13.1.jar -rw-r--r-- 1 root root 1678073 Apr 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 3233128 Apr 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 Apr 7 2016 sonar-scm-svn-plugin-1.3.jar [root@linux-node1 plugins] # |
安装中文插件


用户名和密码默认都是admin

配置的地方

可以选择升级一些东西,也可以搜索

点击右边的安装,实际上它是wget那个jar包,然后放在响应目录下

提示需要重启

提示重启才生效

restart

另外一种下载插件的途径 从gitlab上下载 https://github.com/SonarQubeCommunity

搜索


wget下载jar即可

查看已经安装的 默认情况下它只能分析java和JS的,因为它只安装了java和JS的语言插件 如果想让它分析python,和php的。需要让它安装对应的插件

安装php和python的语言插件

安装css插件

###############################################################################
安装sonar Scanner
打开 http://www.sonarqube.org/官网 找到下面扫描器 通过这个扫描器可以分析代码分析

因此你也的安装这个扫描器

上传sonar-scanner-2.6.1.zip ?
1 2 3 4 5 6 7 8 9 | [root@linux-node1 plugins] # cd /usr/local/src/ [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonarqube-5.6.zip [root@linux-node1 src] # rz -E rz waiting to receive. [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonar-scanner-2.6.1.zip sonarqube-5.6.zip [root@linux-node1 src] # |
下载中文插件包失败,可以通过迅雷下载

手动下载中文插件并上传到此目录下 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [root@linux-node1 plugins] # pwd /usr/local/sonarqube/extensions/plugins [root@linux-node1 plugins] # ll total 29180 -rw-r--r-- 1 root root 128 Apr 11 2016 README.txt -rw-r--r-- 1 root root 7797781 Apr 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 3191477 Apr 28 2016 sonar-java-plugin-3.13.1.jar -rw-r--r-- 1 root root 1678073 Apr 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 3733262 Apr 29 17:26 sonar-php-plugin-2.10.0.2087.jar -rw-r--r-- 1 root root 3663678 Apr 29 17:11 sonar-python-plugin-1.7.0.1195.jar -rw-r--r-- 1 root root 3233128 Apr 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 Apr 7 2016 sonar-scm-svn-plugin-1.3.jar [root@linux-node1 plugins] # rz -E rz waiting to receive. [root@linux-node1 plugins] # ll total 29216 -rw-r--r-- 1 root root 128 Apr 11 2016 README.txt -rw-r--r-- 1 root root 7797781 Apr 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 3191477 Apr 28 2016 sonar-java-plugin-3.13.1.jar -rw-r--r-- 1 root root 1678073 Apr 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 36538 Apr 29 17:34 sonar-l10n-zh-plugin-1.11.jar -rw-r--r-- 1 root root 3733262 Apr 29 17:26 sonar-php-plugin-2.10.0.2087.jar -rw-r--r-- 1 root root 3663678 Apr 29 17:11 sonar-python-plugin-1.7.0.1195.jar -rw-r--r-- 1 root root 3233128 Apr 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 Apr 7 2016 sonar-scm-svn-plugin-1.3.jar [root@linux-node1 plugins] # |
重启sonar,让插件生效 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@linux-node1 plugins] # /usr/local/sonarqube/bin/linux-x86-64/sonar.sh stop Stopping SonarQube... Waiting for SonarQube to exit ... Stopped SonarQube. [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start Starting SonarQube... Started SonarQube. [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # lsof -i:9000 COMMAND PID USER FD TYPE DEVICE SIZE /OFF NODE NAME java 43957 root 77u IPv4 195191 0t0 TCP *:cslistener (LISTEN) [root@linux-node1 plugins] # |
重启后刷新页面,看到变成中文的了

代码规则这里

一些规则,都是些公共的用法,它会告诉你什么叫好,什么叫不好 装了语言插件才会出现对应代码规则

什么情况下算bug

sonar-scanner才是真正扫描代码的,它连接sonar之后,扫描代码,然后把扫描结果存入数据库,sonar界面进行展示 ?
1 2 3 4 5 | [root@linux-node1 plugins] # cd /usr/local/src/ [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonar-scanner-2.6.1.zip sonarqube-5.6.zip [root@linux-node1 src] # |
解压和安装,并创建软链接 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@linux-node1 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@linux-node1 src] # ll total 422960 -rw-r--r-- 1 root root 314592758 Apr 9 13:38 mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz -rw-r--r-- 1 root root 118012667 Apr 29 15:57 sonarqube-5.6.zip drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 -rw-r--r-- 1 root root 501750 Apr 29 17:16 sonar-scanner-2.6.1.zip [root@linux-node1 src] # mv sonar-scanner-2.6.1 /usr/local/ [root@linux-node1 src] # ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scanner [root@linux-node1 src] # |
查看 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@linux-node1 local ] # cd /usr/local/ [root@linux-node1 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 46 Apr 4 14:15 mysql -> /usr/local/mysql-5 .6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 29 16:01 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share lrwxrwxrwx 1 root root 25 Apr 29 16:00 sonarqube -> /usr/local/sonarqube-5 .6/ drwxr-xr-x 10 root root 111 Jun 1 2016 sonarqube-5.6 lrwxrwxrwx 1 root root 31 Apr 29 17:53 sonar-scanner -> /usr/local/sonar-scanner-2 .6.1/ drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 108 Apr 29 17:52 src [root@linux-node1 local ] # |
把sonar-scanner和sonarqube关联起来 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@linux-node1 local ] # cd sonar-scanner [root@linux-node1 sonar-scanner] # ll total 0 drwxr-xr-x 2 root root 92 May 12 2016 bin drwxr-xr-x 2 root root 37 May 12 2016 conf drwxr-xr-x 2 root root 40 May 12 2016 lib [root@linux-node1 sonar-scanner] # cd conf/ [root@linux-node1 conf] # ll total 4 -rw-r--r-- 1 root root 775 May 12 2016 sonar-scanner.properties [root@linux-node1 conf] # pwd /usr/local/sonar-scanner/conf [root@linux-node1 conf] # |
取消下面注释

保存,退出
找一些代码 对它进行分析
sonar官方提供了个样板包,有各种源的代码
打开下面地址
https://github.com/SonarSource
找到下面东西
https://github.com/SonarSource/sonar-scanning-examples

下载

上传并解压sonar-scanning-examples-master.zip 里面有很多语言 ?
1 2 3 4 5 6 7 8 9 10 11 | [root@linux-node1 ~] # ls anaconda-ks.cfg sonar-scanning-examples-master ks.cfg sonar-scanning-examples-master.zip [root@linux-node1 ~] # cd sonar-scanning-examples-master [root@linux-node1 sonar-scanning-examples-master] # ls objc-llvm-coverage sonarqube-scanner-build-wrapper-linux swift-coverage sonarqube-scanner sonarqube-scanner-maven sonarqube-scanner-ant sonarqube-scanner-msbuild [root@linux-node1 sonar-scanning-examples-master] # cd sonarqube-scanner [root@linux-node1 sonarqube-scanner] # ls copybooks coverage-report sonar-project.properties src |
下面可以看到很多源码 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@linux-node1 src] # pwd /root/sonar-scanning-examples-master/sonarqube-scanner/src [root@linux-node1 src] # ll total 0 drwxr-xr-x 2 root root 46 Mar 23 18:26 abap drwxr-xr-x 2 root root 25 Mar 23 18:26 cobol drwxr-xr-x 2 root root 39 Mar 23 18:26 css drwxr-xr-x 2 root root 33 Mar 23 18:26 directory drwxr-xr-x 2 root root 22 Mar 23 18:26 erlang drwxr-xr-x 2 root root 47 Mar 23 18:26 flex drwxr-xr-x 2 root root 53 Mar 23 18:26 java drwxr-xr-x 2 root root 22 Mar 23 18:26 javascript drwxr-xr-x 2 root root 49 Mar 23 18:26 package drwxr-xr-x 2 root root 21 Mar 23 18:26 php drwxr-xr-x 2 root root 73 Mar 23 18:26 pli drwxr-xr-x 2 root root 44 Mar 23 18:26 python drwxr-xr-x 2 root root 26 Mar 23 18:26 rpg drwxr-xr-x 2 root root 75 Mar 23 18:26 samples drwxr-xr-x 2 root root 40 Mar 23 18:26 sql drwxr-xr-x 2 root root 26 Mar 23 18:26 swift drwxr-xr-x 2 root root 59 Mar 23 18:26 vb6 drwxr-xr-x 2 root root 25 Mar 23 18:26 web drwxr-xr-x 2 root root 29 Mar 23 18:26 xml [root@linux-node1 src] # |
查看配置文件 ?
1 2 3 4 5 6 | [root@linux-node1 sonarqube-scanner] # ll total 8 drwxr-xr-x 2 root root 75 Mar 23 18:26 copybooks drwxr-xr-x 2 root root 39 Mar 23 18:26 coverage-report -rw-r--r-- 1 root root 647 Mar 23 18:26 sonar-project.properties drwxr-xr-x 21 root root 4096 Mar 23 18:26 src |
下面配置文件
项目里必须有上面这个配置文件,没有这个配置文件,无法对其进行扫描 ?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [root@linux-node1 sonarqube-scanner] # cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 sonar.sources=src,copybooks sonar.sourceEncoding=UTF-8 ## Cobol Specific Properties # comma-separated paths to directories with copybooks sonar.cobol.copy.directories=copybooks # comma-separated list of suffixes sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy ## Flex Specific Properties # retrieve code coverage data from the Cobertura report sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml # PL/I Specific Properties sonar.pli.marginLeft=2 sonar.pli.marginRight=0 [root@linux-node1 sonarqube-scanner] # |
样例最新的地址 https://github.com/SonarSource/sonar-scanning-examples
进行扫描
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | [root@linux-node1 sonarqube-scanner] # /usr/local/sonar-scanner/bin/sonar-scanner INFO: Scanner configuration file : /usr/local/sonar-scanner/conf/sonar-scanner .properties INFO: Project root configuration file : /root/sonar-scanning-examples-master/sonarqube-scanner/sonar-project .properties INFO: SonarQube Scanner 2.6.1 INFO: Java 1.8.0_131 Oracle Corporation (64-bit) INFO: Linux 3.10.0-229.el7.x86_64 amd64 INFO: User cache: /root/ .sonar /cache INFO: Load global repositories INFO: Load global repositories ( done ) | time =153ms 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 =32ms 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 5.6 INFO: Default locale: "en_US" , source code encoding: "UTF-8" INFO: Process project properties INFO: Load project repositories INFO: Load project repositories ( done ) | time =33ms INFO: Load quality profiles INFO: Load quality profiles ( done ) | time =49ms INFO: Load active rules INFO: Load active rules ( done ) | time =582ms 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: ------------- Scan Example of SonarQube Scanner Usage INFO: Load server rules INFO: Load server rules ( done ) | time =124ms INFO: Base dir : /root/sonar-scanning-examples-master/sonarqube-scanner INFO: Working dir : /root/sonar-scanning-examples-master/sonarqube-scanner/ .sonar INFO: Source paths: src, copybooks INFO: Source encoding: UTF-8, default locale: en_US INFO: Index files INFO: 13 files indexed INFO: Quality profile for java: Sonar way INFO: Quality profile for js: Sonar way INFO: Quality profile for php: Sonar way INFO: Quality profile for py: Sonar way INFO: JaCoCoSensor: JaCoCo report not found : /root/sonar-scanning-examples-master/sonarqube-scanner/target/jacoco . exec INFO: JaCoCoItSensor: JaCoCo IT report not found: /root/sonar-scanning-examples-master/sonarqube-scanner/target/jacoco-it . exec INFO: Sensor JavaSquidSensor INFO: Configured Java source version (sonar.java. source ): none INFO: JavaClasspath initialization... WARN: Bytecode of dependencies was not provided for analysis of source files, you might end up with less precise results. Bytecode can be provided using sonar.java.libraries property INFO: JavaClasspath initialization done : 1 ms INFO: JavaTestClasspath initialization... WARN: Bytecode of dependencies was not provided for analysis of test files, you might end up with less precise results. Bytecode can be provided using sonar.java. test .libraries property INFO: JavaTestClasspath initialization done : 1 ms INFO: Java Main Files AST scan... INFO: 2 source files to be analyzed INFO: Java Main Files AST scan done : 639 ms WARN: Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@26e0d39c, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@56382bc9 are disabled. INFO: Java Test Files AST scan... INFO: 2 /2 source files have been analyzed INFO: 0 source files to be analyzed INFO: Java Test Files AST scan done : 5 ms INFO: Sensor JavaSquidSensor ( done ) | time =1306ms INFO: Sensor Lines Sensor INFO: 0 /0 source files have been analyzed INFO: Sensor Lines Sensor ( done ) | time =6ms INFO: Sensor PythonXUnitSensor INFO: Sensor PythonXUnitSensor ( done ) | time =24ms INFO: Sensor SurefireSensor INFO: parsing /root/sonar-scanning-examples-master/sonarqube-scanner/target/surefire-reports INFO: Sensor SurefireSensor ( done ) | time =7ms INFO: Sensor JavaScriptSquidSensor INFO: 1 source files to be analyzed INFO: Sensor JavaScriptSquidSensor ( done ) | time =486ms INFO: Sensor SCM Sensor INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it. INFO: Sensor SCM Sensor ( done ) | time =0ms INFO: Sensor Python Squid Sensor INFO: 1 /1 source files have been analyzed INFO: Python unit test coverage INFO: Python integration test coverage INFO: Python overall test coverage INFO: Sensor Python Squid Sensor ( done ) | time =384ms INFO: Sensor PHP sensor INFO: 1 source files to be analyzed INFO: 1 /1 source files have been analyzed INFO: No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property) INFO: No PHPUnit unit test coverage report provided (see 'sonar.php.coverage.reportPath' property) INFO: No PHPUnit integration test coverage report provided (see 'sonar.php.coverage.itReportPath' property) INFO: No PHPUnit overall coverage report provided (see 'sonar.php.coverage.overallReportPath' property) INFO: Sensor PHP sensor ( done ) | time =717ms INFO: Sensor Analyzer for "php.ini" files INFO: Sensor Analyzer for "php.ini" files ( done ) | time =3ms INFO: Sensor org.sonar.plugins.javascript.lcov.UTCoverageSensor INFO: Sensor org.sonar.plugins.javascript.lcov.UTCoverageSensor ( done ) | time =0ms INFO: Sensor org.sonar.plugins.javascript.lcov.ITCoverageSensor INFO: Sensor org.sonar.plugins.javascript.lcov.ITCoverageSensor ( done ) | time =0ms INFO: Sensor Zero Coverage Sensor INFO: Sensor Zero Coverage Sensor ( done ) | time =36ms INFO: Sensor Code Colorizer Sensor INFO: Sensor Code Colorizer Sensor ( done ) | time =1ms INFO: Sensor CPD Block Indexer INFO: JavaCpdBlockIndexer is used for java INFO: DefaultCpdBlockIndexer is used for js INFO: DefaultCpdBlockIndexer is used for php INFO: DefaultCpdBlockIndexer is used for py INFO: Sensor CPD Block Indexer ( done ) | time =81ms INFO: Calculating CPD for 7 files INFO: CPD calculation finished INFO: Analysis report generated in 108ms, dir size=64 KB INFO: Analysis reports compressed in 40ms, zip size=38 KB INFO: Analysis report uploaded in 298ms INFO: ANALYSIS SUCCESSFUL, you can browse http: //localhost :9000 /dashboard/index/org .sonarqube:sonarqube-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 =AVu6Wsaeyj9VSYmN4Z_C INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time : 9.136s INFO: Final Memory: 51M /252M INFO: ------------------------------------------------------------------------ |
网页上查看扫描报告

网页上显示的名称和配置文件配置的名称一样 ?
1 2 3 4 | [root@linux-node1 sonarqube-scanner] # cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 |
点击进去

右边是自己已经下载的插件,然后才会匹配代码

点开重复的

继续点开可以看到重复的块

它会把重复的部分标识出来

jenkins要集成sonar,要装插件

它会把依赖安装上

Jenkins实战Sonar与Jenkins结合
这里可以看到sonar的配置

点击下面
相关配置项

先填这些信息,保存


找到扫描器的配置

取消自动安装,到这里意识到sonar Scanner要和jenkins装一个机器上

之前安装在了node1上,需要把它迁移到node2上,和jenkins在一个机器上
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@linux-node1 local ] # cd /usr/local/ [root@linux-node1 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 46 Apr 4 14:15 mysql -> /usr/local/mysql-5 .6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 30 14:55 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share lrwxrwxrwx 1 root root 25 Apr 29 16:00 sonarqube -> /usr/local/sonarqube-5 .6/ drwxr-xr-x 10 root root 111 Jun 1 2016 sonarqube-5.6 lrwxrwxrwx 1 root root 31 Apr 29 17:53 sonar-scanner -> /usr/local/sonar-scanner-2 .6.1/ drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 108 Apr 29 17:52 src [root@linux-node1 local ] # |
把整个目录拷贝过去 ?
1 2 3 | [root@linux-node1 local ] # rsync -az sonar-scanner-2.6.1 192.168.58.12:/usr/local/ root@192.168.58.12's password: [root@linux-node1 local ] # |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [root@linux-node2 ~] # cd /usr/local/ [root@linux-node2 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 35 Apr 9 16:50 mysql -> mysql-5.6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 9 20:56 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 54 Apr 9 16:50 src [root@linux-node2 local ] # ln -s sonar-scanner-2.6.1 sonar-scanner [root@linux-node2 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 35 Apr 9 16:50 mysql -> mysql-5.6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 9 20:56 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share lrwxrwxrwx 1 root root 19 Apr 30 15:16 sonar-scanner -> sonar-scanner-2.6.1 drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 54 Apr 9 16:50 src [root@linux-node2 local ] # |
1 2 3 4 5 6 | [root@linux-node2 conf] # pwd /usr/local/sonar-scanner/conf [root@linux-node2 conf] # ll total 4 -rw-r--r-- 1 root root 781 Apr 30 15:24 sonar-scanner.properties [root@linux-node2 conf] # vim sonar-scanner.properties |

保存

现在只做到源码管理,没构建

这里面就有所有你能干的活


Analysis properties这里填的是下面配置文件的内容,复制进去 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [root@linux-node1 ~] # cd sonar-scanning-examples-master [root@linux-node1 sonar-scanning-examples-master] # cd sonarqube-scanner [root@linux-node1 sonarqube-scanner] # ll total 8 drwxr-xr-x 2 root root 75 Mar 23 18:26 copybooks drwxr-xr-x 2 root root 39 Mar 23 18:26 coverage-report -rw-r--r-- 1 root root 647 Mar 23 18:26 sonar-project.properties drwxr-xr-x 21 root root 4096 Mar 23 18:26 src [root@linux-node1 sonarqube-scanner] # cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 sonar.sources=src,copybooks sonar.sourceEncoding=UTF-8 ## Cobol Specific Properties # comma-separated paths to directories with copybooks sonar.cobol.copy.directories=copybooks # comma-separated list of suffixes sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy ## Flex Specific Properties # retrieve code coverage data from the Cobertura report sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml # PL/I Specific Properties sonar.pli.marginLeft=2 sonar.pli.marginRight=0 [root@linux-node1 sonarqube-scanner] # |
把下面 ?
1 2 3 4 5 6 7 8 9 10 11 | sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 sonar.sources=src,copybooks sonar.sourceEncoding=UTF-8 sonar.cobol.copy.directories=copybooks sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml sonar.pli.marginLeft=2 sonar.pli.marginRight=0 |
改成 ?
1 2 3 4 5 6 7 8 9 10 11 | sonar.projectKey=demo sonar.projectName=demo sonar.projectVersion=1.0 sonar.sources=./ sonar.sourceEncoding=UTF-8 sonar.cobol.copy.directories=copybooks sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml sonar.pli.marginLeft=2 sonar.pli.marginRight=0 |
复制进去

其它区域都不填写,先保存

右边多了个快捷方式

点击立即构建


查看控制台输出

一些输出

这里配置文件就以参数的形式传输给它了 ?
1 2 3 4 5 | [auto-deploy] $ /usr/local/sonar-scanner/bin/sonar-scanner -e -Dsonar.host.url=http: //192 .168.58.11:9000 -Dsonar.sourceEncoding=UTF-8 -Dsonar.cobol.copy.directories=copybooks -Dsonar.sources=./ -Dsonar.pli.marginRight=0 -Dsonar.projectVersion=1.0 -Dsonar.projectKey=demo -Dsonar.cobol. file .suffixes=cbl,cpy -Dsonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml -Dsonar.cobol.copy.suffixes=cpy -Dsonar.pli.marginLeft=2 -Dsonar.projectName=demo -Dsonar.projectBaseDir= /var/lib/jenkins/workspace/auto-deploy |
返回到工程,点击进去



你可以改仪表盘显示

配置控件

你可以添加代码覆盖率的控件 选一个项目demo

保存

你也可以把欢迎删除了


返回仪表盘

jenkins继续编辑配置

还可以设置构建后操作

把项目名改成demo-sonar

再次立即构建

Jenkins实战-Job关联和Pipeline 新建一个项目


构建,这里选择执行shell

想先看看jenkins是以什么用户执行的

保存之后,点击立即构建

查看控制台,看到它以jenkins用户执行的

这就有问题了。部署机是192.168.58.11 是node1,使用的是www用户 jenkins在node2上 生产中,jenkins很多情况下和部署机器也不在一台 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@linux-node1 src] # su - www Last login: Sat Apr 29 14:00:45 CST 2017 on pts /0 [www@linux-node1 ~]$ cd /scripts/ [www@linux-node1 scripts]$ . /deploy_all .sh Usage: . /deploy_all .sh { deploy | rollback [ list |version ]} [www@linux-node1 scripts]$ . /deploy_all .sh deploy Already up-to- date . code_build web-demo_798366_2017-04-30-17-09-00. tar .gz 100% 9059 8.9KB /s 00:00 web-demo_798366_2017-04-30-17-09-00. tar .gz 100% 9059 8.9KB /s 00:00 test error [www@linux-node1 scripts]$ vim deploy_all.sh [www@linux-node1 scripts]$ . /deploy_all .sh deploy Already up-to- date . code_build web-demo_798366_2017-04-30-17-09-49. tar .gz 100% 9055 8.8KB /s 00:00 web-demo_798366_2017-04-30-17-09-49. tar .gz 100% 9055 8.8KB /s 00:00 192.168.58.12. crontab .xml 100% 21 0.0KB /s 00:00 HTTP /1 .1 200 OK add to cluster [www@linux-node1 scripts]$ |

但是需要配置密钥认证 拷贝公钥过去 ?
1 2 3 4 5 6 7 8 9 10 | [root@linux-node2 ~] # cat .ssh/id_rsa.pub ssh -rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz0QBxB9kIv8H6Uxe+clLvM99 /7QOYTCVZzQKb0Q9Song1Yy9oryCcYWHogN30j80Ctbybu +3PAh4VIfz4SjY1tNd /Dv2Il3caz +5chvX0OL DJCNl2w+Du7HjwdyszlzVc1rP4QTL /yYvrUz1fgBonRKa0/Y5CK7jEzQZ1 +ZMuN /E/ubrDX5FIp0jfPPnbtTryWW8BMloOb +Q8EFKnaI6Wbyx9JzslOIawCkM77LL9RhxMbQ6A3QoU HozmlFnhfu7AXnhfCdPb02xx4RCOAFoY9pEtOH+eHzT7 /SQVf8VzEYO1vIe4TDP6sNN40/1pjUdBPcGiBMidM6pLk/j6CaxV root@linux-node2.nmap.com [root@linux-node2 ~] # [www@linux-node1 scripts]$ cd [www@linux-node1 ~]$ vim . ssh /authorized_keys [www@linux-node1 ~]$ |

测试 ?
1 2 3 | [root@linux-node2 ~] # ssh www@192.168.58.11 Last login: Sun Apr 30 17:08:50 2017 [www@linux-node1 ~]$ |

点击立即构建,它会失败


给它sudo权限

构建这里加上sudo

保存之后执行还是不可以

把这里加上注释,不让它需要tty

再次构建

执行成功

ls -lrt 检查 ?
1 2 3 4 | drwxrwxr-x 3 www www 69 Apr 30 17:09 web-demo_798366_2017-04-30-17-09-49 -rw-rw-r-- 1 www www 9063 Apr 30 20:06 web-demo_798366_2017-04-30-20-06-29. tar .gz drwxrwxr-x 3 www www 69 Apr 30 20:06 web-demo_798366_2017-04-30-20-06-29 [root@linux-node2 webroot] # |
现在我们有2个,一个是代码质量的,一个是自动部署脚本的 显示阴天的是因为之前执行失败过。有提示

开始学钩子脚本 这边一提交(push),这边自动构建一遍 jenkins安装此插件 它是一个触发器插件

直接安装

在demo-sonar项目选择下面插件


这里选择下一个任务(项目)的名称

搜索即可,出现demo-deploy

没有参数,勾上 保存

现在的话,你要是构建demo-sonar,它就会构建demo-deploy

验证下看看 demo-sonar项目,立即构建

看到项目这里也自动执行了。11秒前

安装学习piplines插件 代码流水线插件,它会执行一系列操作 安装构建插件

使用它,点击选中的加号,新建一个视图

点击OK之后,弹出如下视图

选择初始化的job 显示最近5次的构建

点击保存

点击保存之后显示的

点击run

刷新下也看到右边执行了

点下面按钮可以看到控制台输出的快捷方式

Jenkins实战-Jenkins与gitlab集成(Webhook) 现在想实现一个功能,代码提交,push到gitlab上 jenkins自动帮你构建 ?
1 2 3 4 5 6 | [www@linux-node1 ~]$ cd web-demo/ [www@linux-node1 web-demo]$ ll total 8 -rw-rw-r-- 1 www www 26 Apr 28 22:20 index.html -rw-rw-r-- 1 www www 9 Apr 28 21:53 README.md [www@linux-node1 web-demo]$ |
先安装gitlab的钩子脚本 钩子插件已经安装了

配置钩子脚本 先在这里配置

勾选这2个 第二个是当gitlab进行发布的时候,对它进行build

它告诉你,你要在gitlab上填上下面这个url http://192.168.58.12:8080/project/demo-sonar

有一个身份认证的东西 需要再装一个插件才能实现认证 先保存了 安装下面插件

只有装了它才能使用身份验证令牌的方式,让双方验证 生成一个token ?
1 2 3 | [root@linux-node2 ~] # openssl rand -hex 10 dc8f07cefb4a25e8c198 [root@linux-node2 ~] # |
保存 在gitlab上添加这个钩子脚本 http://192.168.58.12:8080/project/demo-sonar
登录gitlab 在这个项目下找到钩子配置的地方

打开它的wiki

https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin

下面告诉你了使用办法

格式如下 buildByToken/build?job=RevolutionTest&token=TacoTuesday
http://192.168.58.12:8080/project/demo-sonar
整合下
http://192.168.58.12:8080/buildByToken/build?job=demo-sonar&token=dc8f07cefb4a25e8c198
job的名称不能错,token也不能错

点击add



页面上test一下

成功调用

日志也刷出来了

打开jenkins的pipeline页面,也能看到成功调用

模拟生产环境,写个代码,git push之后看看他能否执行 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [www@linux-node1 web-demo]$ pwd /home/www/web-demo [www@linux-node1 web-demo]$ ll total 8 -rw-rw-r-- 1 www www 26 Apr 28 22:20 index.html -rw-rw-r-- 1 www www 9 Apr 28 21:53 README.md [www@linux-node1 web-demo]$ cat index.html hehehehhehehe version 2.0 [www@linux-node1 web-demo]$ echo "test jenkins" >>index.html [www@linux-node1 web-demo]$ cat index.html hehehehhehehe version 2.0 test jenkins [www@linux-node1 web-demo]$ |
提交到gitlab上 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [www@linux-node1 web-demo]$ git add * [www@linux-node1 web-demo]$ git commit -m "jenkins hook test" [master bd2b985] jenkins hook test 1 file changed, 1 insertion(+) [www@linux-node1 web-demo]$ git push warning: push.default is unset ; its implicit value is changing in Git 2.0 from 'matching' to 'simple' . To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git) Counting objects: 5, done . Delta compression using up to 4 threads. Compressing objects: 100% (2 /2 ), done . Writing objects: 100% (3 /3 ), 304 bytes | 0 bytes /s , done . Total 3 (delta 0), reused 0 (delta 0) To git@192.168.58.11:web /web-demo .git 7983661..bd2b985 master -> master [www@linux-node1 web-demo]$ |
在jenkins页面上看到执行了

打开node2页面,看到页面是最新的了


这里面缺少自动化测试 你可以在部署后面放一个测试的脚本 比如添加一个demo-test ,持续集成之Jenkins安装部署 1、安装JDK Jenkins是Java编写的,所以需要先安装JDK,这里采用yum安装,如果对版本有需求,可以直接在Oracle官网下载JDK。 ?
1 2 3 4 5 6 7 8 9 | [root@linux-node2 ~] # yum install -y java-1.8.0 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.cn99.com Package 1:java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 already installed and latest version Nothing to do [root@linux-node2 ~] # |
2、安装Jekins ?
1 2 3 4 5 | cd /etc/yum .repos.d/ wget http: //pkg .jenkins.io /redhat/jenkins .repo rpm -- import http: //pkg .jenkins.io /redhat/jenkins .io.key yum install -y jenkins systemctl start jenkins |
3、检查启动情况。看到占用端口默认是8080 ?
1 2 3 4 5 6 7 8 9 10 11 12 | [root@linux-node2 yum.repos.d] # netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1 /systemd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1151 /sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1244 /master tcp6 0 0 :::3306 :::* LISTEN 5755 /mysqld tcp6 0 0 :::111 :::* LISTEN 1 /systemd tcp6 0 0 :::8080 :::* LISTEN 8727 /java tcp6 0 0 :::22 :::* LISTEN 1151 /sshd tcp6 0 0 ::1:25 :::* LISTEN 1244 /master [root@linux-node2 yum.repos.d] # |
4、访问Jenkins 在浏览器输入http://192.168.58.12:8080来访问jenkins。 为了安全考虑,首先需要解锁Jenkins,请在/var/lib/jenkins/secrets/initialAdminPassword中查看文件。 5、密码 [root@linux-node2 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword 4687aa33576b4b748888bb60a88b2e4c [root@linux-node2 ~]# 页面上需要把上面的密码输入进去

复制密码进去

Jenkins只是个平台,真正干活的是插件 啥插件都有 选择它推荐的

安装过程
由于网络原因,有一些插件会安装失败 可以点击retry。很高概率会成功 下面的Subversion Plug-in 是svn插件

OWASP插件,它每年出一个top 10 的安全漏洞
开放式Web应用程序安全项目(OWASP)是一个非营利组织,不附属于任何企业或财团。
因此,由OWASP提供和开发的所有设施和文件都不受商业因素的影响。
OWASP支持商业安全技术的合理使用,它有一个论坛,在论坛里信息技术专业人员可以发表和传授专业知识和技能。

设置Admin用户和密码

设置用户名和密码

登陆Jenkins


修复之后,重启jenkins,打开页面不报错了

Hudson是Jenkins的前身,是基于Java开发的一种持续集成工具,用于监控程序重复的工作,
包括:
1、持续的软件版本发布/测试项目。
2、监控外部调用执行的工作。
HUDSON被收购了。,然后写这个的人又搞了个Jenkins
装gitlab插件
目前已经部署好了gitlab,准备和gitlab集成。要安装gitlab插件
右边搜索gitlab
勾选这2个

下载中



创建任务

构建一个自由风格的软件项目


源码管理这里可以看到它支持git

系统管理--系统设置

看到url等

邮件相关

已经安装的插件

高级这里可以看到
更新插件的时候可以使用代理
也可以本地上传插件

或者你放在这个目录下,可以把别人的整个目录拷贝过来,重启Jenkins即可生效 ?
1 2 3 4 | [root@linux-node2 plugins] # cd /var/lib/jenkins/plugins/ [root@linux-node2 plugins] # pwd /var/lib/jenkins/plugins [root@linux-node2 plugins] # |
jenkins官网和官方文档 jenkins官网 https://jenkins.io/index.html

一些文档 https://jenkins.io/doc/developer/

插件列表 http://javadoc.jenkins.io/plugin/

它还有salt插件呢



点击add弹出下面界面

类型里可以选择




点击下面
和刚才一样
配置deploy key
我们可以配置deploy key
如下配置
node2生成密钥对 ?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | [root@linux-node2 ~] # ssh-keygen -t rsa Generating public /private rsa key pair. Enter file in which to save the key ( /root/ . ssh /id_rsa ): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/ . ssh /id_rsa . Your public key has been saved in /root/ . ssh /id_rsa .pub. The key fingerprint is: 23:21:be:de:78:f7:82:40:b8:38:fa:6f:05:39:06:86 root@linux-node2.nmap.com The key's randomart image is: +--[ RSA 2048]----+ | . | |E o | | . o... | | ..*. . | | . +.o. S | |o . .... . | |.. .o . | |. ..oo o | | ..o+... o. | +-----------------+ [root@linux-node2 ~] # [root@linux-node2 ~] # cd .ssh/ [root@linux-node2 . ssh ] # cat id_rsa.pub ssh -rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz0QBxB9kIv8H6Uxe+clLvM99 /7QOYTCVZzQKb0Q9Song1Yy9oryCcYWHogN30j80Ctbybu +3PAh4VIfz4SjY1tNd /Dv2Il3caz +5chvX0OL DJCNl2w+Du7HjwdyszlzVc1rP4QTL /yYvrUz1fgBonRKa0/Y5CK7jEzQZ1 +ZMuN /E/ubrDX5FIp0jfPPnbtTryWW8BMloOb +Q8EFKnaI6Wbyx9JzslOIawCkM77LL9RhxMbQ6A3QoU HozmlFnhfu7AXnhfCdPb02xx4RCOAFoY9pEtOH+eHzT7 /SQVf8VzEYO1vIe4TDP6sNN40/1pjUdBPcGiBMidM6pLk/j6CaxV root@linux-node2.nmap.com [root@linux-node2 . ssh ] # |
下面这里此项目的private deploy key设置的地方。

把key复制进去,同时title起一个简单明了的名字

添加成功(public deploy key要删除,防止冲突)

这里是public deploy key设置的地方

如下位置

node2拉取下看看,需要有git工具 ?
1 2 3 4 5 6 7 8 9 | [root@linux-node2 ~] # yum install git -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.cn99.com Package git-1.8.3.1-6.el7_2.1.x86_64 already installed and latest version Nothing to do [root@linux-node2 ~] # |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@linux-node2 ~] # git clone git@192.168.58.11:web/web-demo.git Cloning into 'web-demo' ... remote: Counting objects: 13, done . remote: Compressing objects: 100% (8 /8 ), done . remote: Total 13 (delta 0), reused 0 (delta 0) Receiving objects: 100% (13 /13 ), done . [root@linux-node2 ~] # ls anaconda-ks.cfg tools web-demo [root@linux-node2 ~] # cd web-demo/ [root@linux-node2 web-demo] # ls index.html README.md [root@linux-node2 web-demo] # cat index.html hehehehhehehe version 2.0 [root@linux-node2 web-demo] # |
node2上已经没问题,开始配置jenkins的认证
这里应该输入私钥
复制私钥
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [root@linux-node2 ~] # cat .ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAs9EAcQfZCL /B +lMXvnJS7zPff+0DmEwlWc0Cm9EPUqJ4NWMv aK8gnGFh6IDd9I /NArW8m7vtzwIeFSH8 +Eo2NbTXfw79iJd3Gs /uXIb19DiwyQjZ dsPg7ux48HcrM5c1XNaz+EEy /8mL61M9X4AaJ0SmtP2OQiu4xM0GdfmTLjfxP7m6 w1+RSKdI3zz527U68llvATJaDm /kPBBSp2iOlm8sfSc7JTiGsApDO +yy /UYcTG0O gN0KFB6M5pRZ4X7uwF54XwnT29NsceEQjgBaGPaRLTh /nh80 + /0kFX/FcxGDtbyH uEwz+rDTeNP9aY1HQT3BogTInTOqS5P4+gmsVQIDAQABAoIBAQCKQGMqhHIl++lU YZab4qJJL4TjgoOFEOaWyLz8uBwj2K67vWqx /hQAqA7zCEGeywFA2CIPSspooqQm c19gNw6+9C /LjOb5ohr9Dmmh0lJAV2X060znt2q1yDKkD7uzisqIerscYGSprXha kkFZr9Br6KiT927tCViiV1BkXGMA4NAyFHQQBa493kYZ9tRM89nfzQXK8Ttjulp3 1qD3bPebsMWp6JuCtnFsRwfoYRnsYHHG3FsD5QOIV1tcKiT8v6fV5CklVejOn5O3 +992oNz0UqXuHD0GhFRGRo7Xro2XAsSpyxH0NPv6Aclz9eU6t3hXF+lg33l+tKYJ HUrHvt9xAoGBANnNXmu72pyEhIMbWTw6qR2WAy5UL+hdggDilGbsTX9x33eZiaQR bNT7qcX2CRq7pmXiH4o6LmljkkVYTGS /ZIkp6QZZp9CnE2Ywb0oo +UwfKPAXLMpd 2xBjg03YSzD6ElsPKeVgx78 //UGbcWZWMbw7i9k//ho2j3XMKwHIIABPAoGBANNa MK6VGJDX /PPvre +ktF50bKj4VDWNdT5nEvjuEG98zIEIh5geAgPVYMXeBELlKmvw Az /eto5puvtPF2bXk/Y75xJ/pYha5KIhQYhbt +GLk71pWywu9wvZopkEViVc2Tf9 vIo+SFJEjxgQeotJG0S24kCJfNfoh9oIwGSGDxwbAoGALel7yOwNlkbdBCIkumyH L8RqoT5 /5tSnTfZ1YAjaAU6I09ZazGob7f2q +R73Jlp+JiX3jhTbqb2inNmTKP2r fLqH8528oWBG15jILPLcMm6zG4FXIMWOIzNG /4bjusxMSEO3CjJPiQI9KbJRwkfT LiHDRiWWNAJiVWuXeUWtcUMCgYEAwgmjeCTaEOC7ExmRDtqf3bf1qouShDgC0++9 s1ydKj3hYvV5x2aFYw+bcQ2Ly /6pG7 +WvCS0ZbscN209huWUqhQE6S7Xwn /wOAlg KNhL0X5bfSIk2kOj+ /UrGmCo1lSP +J+zYRgdPapD1UwLtaTArC1bOvT+8RpC3yH5 wFBzvtECgYEAyYLpBV8vNzY7ilCmGctXtj5gsAvxOkYMVdaPkCXDr50qHBYYsHj4 22FdOq75zWgrIQo6Poke3IQcz4NML/+Z08r2qa7+4z5Jx5BnwXAnWEB9omcbANgD m+h33eEXWr12L363c+ /RVdZPINkmhaS69u4/Njh +dI1lL8Ta+8Ihkow= -----END RSA PRIVATE KEY----- [root@linux-node2 ~] # |
复制进去
添加描述
点击OK

继续编辑配置

这样就不报错了 下面还可以指定从哪个分支拉取,默认从master拉取,有些公司使用release分支

源码浏览器

gitlab的url
去gitlab上找到http的url

找出gitlab的版本 ?
1 2 3 | [root@linux-node1 ~] # rpm -qa | grep gitlab gitlab-ce-9.1.0-ce.0.el7.x86_64 [root@linux-node1 ~] # |

版本只支持2位

拉到最下面保存

点击立即构建

查看控制台


从控制台可以看到它的工作路径 ?
1 2 3 4 | [root@linux-node2 ~] # cd /var/lib/jenkins/workspace/auto-deploy [root@linux-node2 auto-deploy] # ls index.html README.md [root@linux-node2 auto-deploy] # |
git show查看版本
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@linux-node2 auto-deploy] # git show commit 7983661ca0d2be489b4442c3d49d137d2a1026df Author: nmap <nmap@ test .com> Date: Fri Apr 28 22:20:59 2017 +0800 2.0 diff --git a /index .html b /index .html index f95f8e7..d77c60b 100644 --- a /index .html +++ b /index .html @@ -1,2 +1,2 @@ hehehehhehehe -version 2.1 +version 2.0 [root@linux-node2 auto-deploy] # |
有个.get的目录 ?
1 2 3 | [root@linux-node2 auto-deploy] # ls -a . .. .git index.html README.md [root@linux-node2 auto-deploy] # |
持续代码质量管理-Sonar部署
能检查代码里有没有bug,比如你代码里有个死循环的bug,不用运行,就能检测出来
它还会给你代码打分(代码质量)
Sonar 是一个用于代码质量管理的开放平台。
通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具。与持续集成工具(例如 Hudson/Jenkins 等)不同,
Sonar 并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,
而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。
在对其他工具的支持方面,Sonar 不仅提供了对 IDE 的支持,可以在 Eclipse 和 IntelliJ IDEA 这些工具里联机查看结果;
同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。
Sonar部署
Sonar的相关下载和文档可以在下面的链接中找到:http://www.sonarqube.org/downloads/。需要注意最新版的Sonar需要至少JDK 1.8及以上版本。
1 2 3 4 5 6 7 8 9 10 | [root@linux-node1 ~] # yum install -y java-1.8.0 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.163.com * updates: mirrors.zju.edu.cn Package 1:java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64 already installed and latest version Nothing to do [root@linux-node1 ~] # |
下载sonar包并安装 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | wget https: //sonarsource .bintray.com /Distribution/sonarqube/sonarqube-5 .6.zip [root@linux-node1 src] # pwd /usr/local/src [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonarqube-5.6.zip [root@linux-node1 src] # [root@linux-node1 src] # yum install unzip -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.163.com * updates: mirrors.cn99.com Package unzip-6.0-16.el7.x86_64 already installed and latest version Nothing to do [root@linux-node1 src] # [root@linux-node1 src] # unzip sonarqube-5.6.zip [root@linux-node1 src] # mv sonarqube-5.6 /usr/local/ [root@linux-node1 src] # ln -s /usr/local/sonarqube-5.6/ /usr/local/sonarqube [root@linux-node1 src] # |
准备Sonar数据库 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@linux-node1 src] # /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf & [root@linux-node1 src] # /usr/local/mysql/bin/mysql -S /usr/local/mysql/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.30-log MySQL Community Server (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> GRANT ALL ON sonar.* TO 'sonar' @ 'localhost' IDENTIFIED BY 'sonar@pw' ; mysql> GRANT ALL ON sonar.* TO 'sonar' @ '%' IDENTIFIED BY 'sonar@pw' ; mysql> FLUSH PRIVILEGES; |
配置Sonar ?
1 2 3 4 | [root@linux-node1 src] # cd /usr/local/sonarqube/conf/ [root@linux-node1 conf] # ls sonar.properties wrapper.conf [root@linux-node1 conf] # |
1 2 3 4 | [root@linux-node1 conf] # vim sonar.properties sonar.jdbc.username=sonar sonar.jdbc.password=sonae@pw sonar.jdbc.url=jdbc:mysql: //localhost :3306 /sonar ?useUnicode= true &characterEncoding=utf8&rewriteBatchedStatements= true &useConfigs=maxPerformance |
修改之后如下
它默认监听的地址和端口 这里不用改,知道就行了

启动Sonar 你可以在Sonar的配置文件来配置Sonar Web监听的IP地址和端口,默认是9000端口。 ?
1 2 3 4 | [root@linux-node1 conf] # /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start Starting SonarQube... Started SonarQube. [root@linux-node1 conf] # |
第一次启动慢,因为它要创建表 打开页面

关于sonar的目录结构 ?
1 2 3 4 5 6 7 | [root@linux-node1 conf] # cd /usr/local/sonarqube [root@linux-node1 sonarqube] # ls bin conf COPYING data extensions lib logs temp web [root@linux-node1 sonarqube] # cd logs/ [root@linux-node1 logs] # ls access.log sonar.log [root@linux-node1 logs] # |
插件存放位置 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@linux-node1 logs] # cd /usr/local/sonarqube [root@linux-node1 sonarqube] # ls bin conf COPYING data extensions lib logs temp web [root@linux-node1 sonarqube] # cd extensions/ [root@linux-node1 extensions] # ll total 4 drwxr-xr-x 2 root root 6 Apr 29 16:34 deprecated drwxr-xr-x 2 root root 6 Apr 29 16:35 downloads drwxr-xr-x 3 root root 19 Jan 11 2016 jdbc-driver drwxr-xr-x 2 root root 4096 Apr 29 16:34 plugins [root@linux-node1 extensions] # cd plugins/ [root@linux-node1 plugins] # pwd /usr/local/sonarqube/extensions/plugins [root@linux-node1 plugins] # [root@linux-node1 plugins] # ll total 21952 -rw-r--r-- 1 root root 128 Apr 11 2016 README.txt -rw-r--r-- 1 root root 7797781 Apr 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 3191477 Apr 28 2016 sonar-java-plugin-3.13.1.jar -rw-r--r-- 1 root root 1678073 Apr 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 3233128 Apr 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 Apr 7 2016 sonar-scm-svn-plugin-1.3.jar [root@linux-node1 plugins] # |
安装中文插件


用户名和密码默认都是admin

配置的地方

可以选择升级一些东西,也可以搜索

点击右边的安装,实际上它是wget那个jar包,然后放在响应目录下

提示需要重启

提示重启才生效

restart

另外一种下载插件的途径 从gitlab上下载 https://github.com/SonarQubeCommunity

搜索


wget下载jar即可

查看已经安装的 默认情况下它只能分析java和JS的,因为它只安装了java和JS的语言插件 如果想让它分析python,和php的。需要让它安装对应的插件

安装php和python的语言插件

安装css插件

,
安装sonar Scanner
打开 http://www.sonarqube.org/官网 找到下面扫描器 通过这个扫描器可以分析代码分析

因此你也的安装这个扫描器

上传sonar-scanner-2.6.1.zip ?
1 2 3 4 5 6 7 8 9 | [root@linux-node1 plugins] # cd /usr/local/src/ [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonarqube-5.6.zip [root@linux-node1 src] # rz -E rz waiting to receive. [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonar-scanner-2.6.1.zip sonarqube-5.6.zip [root@linux-node1 src] # |
下载中文插件包失败,可以通过迅雷下载

手动下载中文插件并上传到此目录下 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [root@linux-node1 plugins] # pwd /usr/local/sonarqube/extensions/plugins [root@linux-node1 plugins] # ll total 29180 -rw-r--r-- 1 root root 128 Apr 11 2016 README.txt -rw-r--r-- 1 root root 7797781 Apr 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 3191477 Apr 28 2016 sonar-java-plugin-3.13.1.jar -rw-r--r-- 1 root root 1678073 Apr 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 3733262 Apr 29 17:26 sonar-php-plugin-2.10.0.2087.jar -rw-r--r-- 1 root root 3663678 Apr 29 17:11 sonar-python-plugin-1.7.0.1195.jar -rw-r--r-- 1 root root 3233128 Apr 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 Apr 7 2016 sonar-scm-svn-plugin-1.3.jar [root@linux-node1 plugins] # rz -E rz waiting to receive. [root@linux-node1 plugins] # ll total 29216 -rw-r--r-- 1 root root 128 Apr 11 2016 README.txt -rw-r--r-- 1 root root 7797781 Apr 7 2016 sonar-csharp-plugin-5.0.jar -rw-r--r-- 1 root root 3191477 Apr 28 2016 sonar-java-plugin-3.13.1.jar -rw-r--r-- 1 root root 1678073 Apr 7 2016 sonar-javascript-plugin-2.11.jar -rw-r--r-- 1 root root 36538 Apr 29 17:34 sonar-l10n-zh-plugin-1.11.jar -rw-r--r-- 1 root root 3733262 Apr 29 17:26 sonar-php-plugin-2.10.0.2087.jar -rw-r--r-- 1 root root 3663678 Apr 29 17:11 sonar-python-plugin-1.7.0.1195.jar -rw-r--r-- 1 root root 3233128 Apr 7 2016 sonar-scm-git-plugin-1.2.jar -rw-r--r-- 1 root root 6564535 Apr 7 2016 sonar-scm-svn-plugin-1.3.jar [root@linux-node1 plugins] # |
重启sonar,让插件生效 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@linux-node1 plugins] # /usr/local/sonarqube/bin/linux-x86-64/sonar.sh stop Stopping SonarQube... Waiting for SonarQube to exit ... Stopped SonarQube. [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start Starting SonarQube... Started SonarQube. [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # lsof -i:9000 [root@linux-node1 plugins] # lsof -i:9000 COMMAND PID USER FD TYPE DEVICE SIZE /OFF NODE NAME java 43957 root 77u IPv4 195191 0t0 TCP *:cslistener (LISTEN) [root@linux-node1 plugins] # |
重启后刷新页面,看到变成中文的了

代码规则这里

一些规则,都是些公共的用法,它会告诉你什么叫好,什么叫不好 装了语言插件才会出现对应代码规则

什么情况下算bug

sonar-scanner才是真正扫描代码的,它连接sonar之后,扫描代码,然后把扫描结果存入数据库,sonar界面进行展示 ?
1 2 3 4 5 | [root@linux-node1 plugins] # cd /usr/local/src/ [root@linux-node1 src] # ls mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz sonar-scanner-2.6.1.zip sonarqube-5.6.zip [root@linux-node1 src] # |
解压和安装,并创建软链接 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@linux-node1 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@linux-node1 src] # ll total 422960 -rw-r--r-- 1 root root 314592758 Apr 9 13:38 mysql-5.6.30-linux-glibc2.5-x86_64. tar .gz -rw-r--r-- 1 root root 118012667 Apr 29 15:57 sonarqube-5.6.zip drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 -rw-r--r-- 1 root root 501750 Apr 29 17:16 sonar-scanner-2.6.1.zip [root@linux-node1 src] # mv sonar-scanner-2.6.1 /usr/local/ [root@linux-node1 src] # ln -s /usr/local/sonar-scanner-2.6.1/ /usr/local/sonar-scanner [root@linux-node1 src] # |
查看 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@linux-node1 local ] # cd /usr/local/ [root@linux-node1 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 46 Apr 4 14:15 mysql -> /usr/local/mysql-5 .6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 29 16:01 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share lrwxrwxrwx 1 root root 25 Apr 29 16:00 sonarqube -> /usr/local/sonarqube-5 .6/ drwxr-xr-x 10 root root 111 Jun 1 2016 sonarqube-5.6 lrwxrwxrwx 1 root root 31 Apr 29 17:53 sonar-scanner -> /usr/local/sonar-scanner-2 .6.1/ drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 108 Apr 29 17:52 src [root@linux-node1 local ] # |
把sonar-scanner和sonarqube关联起来 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@linux-node1 local ] # cd sonar-scanner [root@linux-node1 sonar-scanner] # ll total 0 drwxr-xr-x 2 root root 92 May 12 2016 bin drwxr-xr-x 2 root root 37 May 12 2016 conf drwxr-xr-x 2 root root 40 May 12 2016 lib [root@linux-node1 sonar-scanner] # cd conf/ [root@linux-node1 conf] # ll total 4 -rw-r--r-- 1 root root 775 May 12 2016 sonar-scanner.properties [root@linux-node1 conf] # pwd /usr/local/sonar-scanner/conf [root@linux-node1 conf] # |
取消下面注释

保存,退出
找一些代码 对它进行分析
sonar官方提供了个样板包,有各种源的代码
打开下面地址
https://github.com/SonarSource
找到下面东西
https://github.com/SonarSource/sonar-scanning-examples

下载

上传并解压sonar-scanning-examples-master.zip 里面有很多语言 ?
1 2 3 4 5 6 7 8 9 10 11 | [root@linux-node1 ~] # ls anaconda-ks.cfg sonar-scanning-examples-master ks.cfg sonar-scanning-examples-master.zip [root@linux-node1 ~] # cd sonar-scanning-examples-master [root@linux-node1 sonar-scanning-examples-master] # ls objc-llvm-coverage sonarqube-scanner-build-wrapper-linux swift-coverage sonarqube-scanner sonarqube-scanner-maven sonarqube-scanner-ant sonarqube-scanner-msbuild [root@linux-node1 sonar-scanning-examples-master] # cd sonarqube-scanner [root@linux-node1 sonarqube-scanner] # ls copybooks coverage-report sonar-project.properties src |
下面可以看到很多源码 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@linux-node1 src] # pwd /root/sonar-scanning-examples-master/sonarqube-scanner/src [root@linux-node1 src] # ll total 0 drwxr-xr-x 2 root root 46 Mar 23 18:26 abap drwxr-xr-x 2 root root 25 Mar 23 18:26 cobol drwxr-xr-x 2 root root 39 Mar 23 18:26 css drwxr-xr-x 2 root root 33 Mar 23 18:26 directory drwxr-xr-x 2 root root 22 Mar 23 18:26 erlang drwxr-xr-x 2 root root 47 Mar 23 18:26 flex drwxr-xr-x 2 root root 53 Mar 23 18:26 java drwxr-xr-x 2 root root 22 Mar 23 18:26 javascript drwxr-xr-x 2 root root 49 Mar 23 18:26 package drwxr-xr-x 2 root root 21 Mar 23 18:26 php drwxr-xr-x 2 root root 73 Mar 23 18:26 pli drwxr-xr-x 2 root root 44 Mar 23 18:26 python drwxr-xr-x 2 root root 26 Mar 23 18:26 rpg drwxr-xr-x 2 root root 75 Mar 23 18:26 samples drwxr-xr-x 2 root root 40 Mar 23 18:26 sql drwxr-xr-x 2 root root 26 Mar 23 18:26 swift drwxr-xr-x 2 root root 59 Mar 23 18:26 vb6 drwxr-xr-x 2 root root 25 Mar 23 18:26 web drwxr-xr-x 2 root root 29 Mar 23 18:26 xml [root@linux-node1 src] # |
查看配置文件 ?
1 2 3 4 5 6 | [root@linux-node1 sonarqube-scanner] # ll total 8 drwxr-xr-x 2 root root 75 Mar 23 18:26 copybooks drwxr-xr-x 2 root root 39 Mar 23 18:26 coverage-report -rw-r--r-- 1 root root 647 Mar 23 18:26 sonar-project.properties drwxr-xr-x 21 root root 4096 Mar 23 18:26 src |
下面配置文件
项目里必须有上面这个配置文件,没有这个配置文件,无法对其进行扫描 ?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [root@linux-node1 sonarqube-scanner] # cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 sonar.sources=src,copybooks sonar.sourceEncoding=UTF-8 ## Cobol Specific Properties # comma-separated paths to directories with copybooks sonar.cobol.copy.directories=copybooks # comma-separated list of suffixes sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy ## Flex Specific Properties # retrieve code coverage data from the Cobertura report sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml # PL/I Specific Properties sonar.pli.marginLeft=2 sonar.pli.marginRight=0 [root@linux-node1 sonarqube-scanner] # |
样例最新的地址 https://github.com/SonarSource/sonar-scanning-examples
进行扫描
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | [root@linux-node1 sonarqube-scanner] # /usr/local/sonar-scanner/bin/sonar-scanner INFO: Scanner configuration file : /usr/local/sonar-scanner/conf/sonar-scanner .properties INFO: Project root configuration file : /root/sonar-scanning-examples-master/sonarqube-scanner/sonar-project .properties INFO: SonarQube Scanner 2.6.1 INFO: Java 1.8.0_131 Oracle Corporation (64-bit) INFO: Linux 3.10.0-229.el7.x86_64 amd64 INFO: User cache: /root/ .sonar /cache INFO: Load global repositories INFO: Load global repositories ( done ) | time =153ms 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 =32ms 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 5.6 INFO: Default locale: "en_US" , source code encoding: "UTF-8" INFO: Process project properties INFO: Load project repositories INFO: Load project repositories ( done ) | time =33ms INFO: Load quality profiles INFO: Load quality profiles ( done ) | time =49ms INFO: Load active rules INFO: Load active rules ( done ) | time =582ms 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: ------------- Scan Example of SonarQube Scanner Usage INFO: Load server rules INFO: Load server rules ( done ) | time =124ms INFO: Base dir : /root/sonar-scanning-examples-master/sonarqube-scanner INFO: Working dir : /root/sonar-scanning-examples-master/sonarqube-scanner/ .sonar INFO: Source paths: src, copybooks INFO: Source encoding: UTF-8, default locale: en_US INFO: Index files INFO: 13 files indexed INFO: Quality profile for java: Sonar way INFO: Quality profile for js: Sonar way INFO: Quality profile for php: Sonar way INFO: Quality profile for py: Sonar way INFO: JaCoCoSensor: JaCoCo report not found : /root/sonar-scanning-examples-master/sonarqube-scanner/target/jacoco . exec INFO: JaCoCoItSensor: JaCoCo IT report not found: /root/sonar-scanning-examples-master/sonarqube-scanner/target/jacoco-it . exec INFO: Sensor JavaSquidSensor INFO: Configured Java source version (sonar.java. source ): none INFO: JavaClasspath initialization... WARN: Bytecode of dependencies was not provided for analysis of source files, you might end up with less precise results. Bytecode can be provided using sonar.java.libraries property INFO: JavaClasspath initialization done : 1 ms INFO: JavaTestClasspath initialization... WARN: Bytecode of dependencies was not provided for analysis of test files, you might end up with less precise results. Bytecode can be provided using sonar.java. test .libraries property INFO: JavaTestClasspath initialization done : 1 ms INFO: Java Main Files AST scan... INFO: 2 source files to be analyzed INFO: Java Main Files AST scan done : 639 ms WARN: Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@26e0d39c, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@56382bc9 are disabled. INFO: Java Test Files AST scan... INFO: 2 /2 source files have been analyzed INFO: 0 source files to be analyzed INFO: Java Test Files AST scan done : 5 ms INFO: Sensor JavaSquidSensor ( done ) | time =1306ms INFO: Sensor Lines Sensor INFO: 0 /0 source files have been analyzed INFO: Sensor Lines Sensor ( done ) | time =6ms INFO: Sensor PythonXUnitSensor INFO: Sensor PythonXUnitSensor ( done ) | time =24ms INFO: Sensor SurefireSensor INFO: parsing /root/sonar-scanning-examples-master/sonarqube-scanner/target/surefire-reports INFO: Sensor SurefireSensor ( done ) | time =7ms INFO: Sensor JavaScriptSquidSensor INFO: 1 source files to be analyzed INFO: Sensor JavaScriptSquidSensor ( done ) | time =486ms INFO: Sensor SCM Sensor INFO: No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it. INFO: Sensor SCM Sensor ( done ) | time =0ms INFO: Sensor Python Squid Sensor INFO: 1 /1 source files have been analyzed INFO: Python unit test coverage INFO: Python integration test coverage INFO: Python overall test coverage INFO: Sensor Python Squid Sensor ( done ) | time =384ms INFO: Sensor PHP sensor INFO: 1 source files to be analyzed INFO: 1 /1 source files have been analyzed INFO: No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property) INFO: No PHPUnit unit test coverage report provided (see 'sonar.php.coverage.reportPath' property) INFO: No PHPUnit integration test coverage report provided (see 'sonar.php.coverage.itReportPath' property) INFO: No PHPUnit overall coverage report provided (see 'sonar.php.coverage.overallReportPath' property) INFO: Sensor PHP sensor ( done ) | time =717ms INFO: Sensor Analyzer for "php.ini" files INFO: Sensor Analyzer for "php.ini" files ( done ) | time =3ms INFO: Sensor org.sonar.plugins.javascript.lcov.UTCoverageSensor INFO: Sensor org.sonar.plugins.javascript.lcov.UTCoverageSensor ( done ) | time =0ms INFO: Sensor org.sonar.plugins.javascript.lcov.ITCoverageSensor INFO: Sensor org.sonar.plugins.javascript.lcov.ITCoverageSensor ( done ) | time =0ms INFO: Sensor Zero Coverage Sensor INFO: Sensor Zero Coverage Sensor ( done ) | time =36ms INFO: Sensor Code Colorizer Sensor INFO: Sensor Code Colorizer Sensor ( done ) | time =1ms INFO: Sensor CPD Block Indexer INFO: JavaCpdBlockIndexer is used for java INFO: DefaultCpdBlockIndexer is used for js INFO: DefaultCpdBlockIndexer is used for php INFO: DefaultCpdBlockIndexer is used for py INFO: Sensor CPD Block Indexer ( done ) | time =81ms INFO: Calculating CPD for 7 files INFO: CPD calculation finished INFO: Analysis report generated in 108ms, dir size=64 KB INFO: Analysis reports compressed in 40ms, zip size=38 KB INFO: Analysis report uploaded in 298ms INFO: ANALYSIS SUCCESSFUL, you can browse http: //localhost :9000 /dashboard/index/org .sonarqube:sonarqube-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 =AVu6Wsaeyj9VSYmN4Z_C INFO: ------------------------------------------------------------------------ INFO: EXECUTION SUCCESS INFO: ------------------------------------------------------------------------ INFO: Total time : 9.136s INFO: Final Memory: 51M /252M INFO: ------------------------------------------------------------------------ |
网页上查看扫描报告

网页上显示的名称和配置文件配置的名称一样 ?
1 2 3 4 | [root@linux-node1 sonarqube-scanner] # cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 |
点击进去

右边是自己已经下载的插件,然后才会匹配代码

点开重复的

继续点开可以看到重复的块

它会把重复的部分标识出来

jenkins要集成sonar,要装插件

它会把依赖安装上

Jenkins实战Sonar与Jenkins结合
这里可以看到sonar的配置

点击下面
相关配置项

先填这些信息,保存


找到扫描器的配置

取消自动安装,到这里意识到sonar Scanner要和jenkins装一个机器上

之前安装在了node1上,需要把它迁移到node2上,和jenkins在一个机器上
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@linux-node1 local ] # cd /usr/local/ [root@linux-node1 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 46 Apr 4 14:15 mysql -> /usr/local/mysql-5 .6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 30 14:55 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share lrwxrwxrwx 1 root root 25 Apr 29 16:00 sonarqube -> /usr/local/sonarqube-5 .6/ drwxr-xr-x 10 root root 111 Jun 1 2016 sonarqube-5.6 lrwxrwxrwx 1 root root 31 Apr 29 17:53 sonar-scanner -> /usr/local/sonar-scanner-2 .6.1/ drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 108 Apr 29 17:52 src [root@linux-node1 local ] # |
把整个目录拷贝过去 ?
1 2 3 | [root@linux-node1 local ] # rsync -az sonar-scanner-2.6.1 192.168.58.12:/usr/local/ root@192.168.58.12's password: [root@linux-node1 local ] # |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [root@linux-node2 ~] # cd /usr/local/ [root@linux-node2 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 35 Apr 9 16:50 mysql -> mysql-5.6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 9 20:56 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 54 Apr 9 16:50 src [root@linux-node2 local ] # ln -s sonar-scanner-2.6.1 sonar-scanner [root@linux-node2 local ] # ll total 4 drwxr-xr-x. 2 root root 6 Jun 10 2014 bin drwxr-xr-x. 2 root root 6 Jun 10 2014 etc drwxr-xr-x. 2 root root 6 Jun 10 2014 games drwxr-xr-x. 2 root root 6 Jun 10 2014 include drwxr-xr-x. 2 root root 6 Jun 10 2014 lib drwxr-xr-x. 2 root root 6 Jun 10 2014 lib64 drwxr-xr-x. 2 root root 6 Jun 10 2014 libexec lrwxrwxrwx 1 mysql mysql 35 Apr 9 16:50 mysql -> mysql-5.6.30-linux-glibc2.5-x86_64/ drwxr-xr-x 13 mysql mysql 4096 Apr 9 20:56 mysql-5.6.30-linux-glibc2.5-x86_64 drwxr-xr-x. 2 root root 6 Jun 10 2014 sbin drwxr-xr-x. 5 root root 46 Feb 6 01:26 share lrwxrwxrwx 1 root root 19 Apr 30 15:16 sonar-scanner -> sonar-scanner-2.6.1 drwxr-xr-x 5 root root 37 Apr 29 17:52 sonar-scanner-2.6.1 drwxr-xr-x. 2 root root 54 Apr 9 16:50 src [root@linux-node2 local ] # |
1 2 3 4 5 6 | [root@linux-node2 conf] # pwd /usr/local/sonar-scanner/conf [root@linux-node2 conf] # ll total 4 -rw-r--r-- 1 root root 781 Apr 30 15:24 sonar-scanner.properties [root@linux-node2 conf] # vim sonar-scanner.properties |

保存

现在只做到源码管理,没构建

这里面就有所有你能干的活


Analysis properties这里填的是下面配置文件的内容,复制进去 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [root@linux-node1 ~] # cd sonar-scanning-examples-master [root@linux-node1 sonar-scanning-examples-master] # cd sonarqube-scanner [root@linux-node1 sonarqube-scanner] # ll total 8 drwxr-xr-x 2 root root 75 Mar 23 18:26 copybooks drwxr-xr-x 2 root root 39 Mar 23 18:26 coverage-report -rw-r--r-- 1 root root 647 Mar 23 18:26 sonar-project.properties drwxr-xr-x 21 root root 4096 Mar 23 18:26 src [root@linux-node1 sonarqube-scanner] # cat sonar-project.properties sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 sonar.sources=src,copybooks sonar.sourceEncoding=UTF-8 ## Cobol Specific Properties # comma-separated paths to directories with copybooks sonar.cobol.copy.directories=copybooks # comma-separated list of suffixes sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy ## Flex Specific Properties # retrieve code coverage data from the Cobertura report sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml # PL/I Specific Properties sonar.pli.marginLeft=2 sonar.pli.marginRight=0 [root@linux-node1 sonarqube-scanner] # |
把下面 ?
1 2 3 4 5 6 7 8 9 10 11 | sonar.projectKey=org.sonarqube:sonarqube-scanner sonar.projectName=Example of SonarQube Scanner Usage sonar.projectVersion=1.0 sonar.sources=src,copybooks sonar.sourceEncoding=UTF-8 sonar.cobol.copy.directories=copybooks sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml sonar.pli.marginLeft=2 sonar.pli.marginRight=0 |
改成 ?
1 2 3 4 5 6 7 8 9 10 11 | sonar.projectKey=demo sonar.projectName=demo sonar.projectVersion=1.0 sonar.sources=./ sonar.sourceEncoding=UTF-8 sonar.cobol.copy.directories=copybooks sonar.cobol. file .suffixes=cbl,cpy sonar.cobol.copy.suffixes=cpy sonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml sonar.pli.marginLeft=2 sonar.pli.marginRight=0 |
复制进去

其它区域都不填写,先保存

右边多了个快捷方式

点击立即构建


查看控制台输出

一些输出

这里配置文件就以参数的形式传输给它了 ?
1 2 3 4 5 | [auto-deploy] $ /usr/local/sonar-scanner/bin/sonar-scanner -e -Dsonar.host.url=http: //192 .168.58.11:9000 -Dsonar.sourceEncoding=UTF-8 -Dsonar.cobol.copy.directories=copybooks -Dsonar.sources=./ -Dsonar.pli.marginRight=0 -Dsonar.projectVersion=1.0 -Dsonar.projectKey=demo -Dsonar.cobol. file .suffixes=cbl,cpy -Dsonar.flex.cobertura.reportPath=coverage-report /coverage-cobertua-flex .xml -Dsonar.cobol.copy.suffixes=cpy -Dsonar.pli.marginLeft=2 -Dsonar.projectName=demo -Dsonar.projectBaseDir= /var/lib/jenkins/workspace/auto-deploy |
返回到工程,点击进去



你可以改仪表盘显示

配置控件

你可以添加代码覆盖率的控件 选一个项目demo

保存

你也可以把欢迎删除了


返回仪表盘

jenkins继续编辑配置

还可以设置构建后操作

把项目名改成demo-sonar

再次立即构建

Jenkins实战-Job关联和Pipeline 新建一个项目


构建,这里选择执行shell

想先看看jenkins是以什么用户执行的

保存之后,点击立即构建

查看控制台,看到它以jenkins用户执行的

这就有问题了。部署机是192.168.58.11 是node1,使用的是www用户 jenkins在node2上 生产中,jenkins很多情况下和部署机器也不在一台 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@linux-node1 src] # su - www Last login: Sat Apr 29 14:00:45 CST 2017 on pts /0 [www@linux-node1 ~]$ cd /scripts/ [www@linux-node1 scripts]$ . /deploy_all .sh Usage: . /deploy_all .sh { deploy | rollback [ list |version ]} [www@linux-node1 scripts]$ . /deploy_all .sh deploy Already up-to- date . code_build web-demo_798366_2017-04-30-17-09-00. tar .gz 100% 9059 8.9KB /s 00:00 web-demo_798366_2017-04-30-17-09-00. tar .gz 100% 9059 8.9KB /s 00:00 test error [www@linux-node1 scripts]$ vim deploy_all.sh [www@linux-node1 scripts]$ . /deploy_all .sh deploy Already up-to- date . code_build web-demo_798366_2017-04-30-17-09-49. tar .gz 100% 9055 8.8KB /s 00:00 web-demo_798366_2017-04-30-17-09-49. tar .gz 100% 9055 8.8KB /s 00:00 192.168.58.12. crontab .xml 100% 21 0.0KB /s 00:00 HTTP /1 .1 200 OK add to cluster [www@linux-node1 scripts]$ |

但是需要配置密钥认证 拷贝公钥过去 ?
1 2 3 4 5 6 7 8 9 10 | [root@linux-node2 ~] # cat .ssh/id_rsa.pub ssh -rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCz0QBxB9kIv8H6Uxe+clLvM99 /7QOYTCVZzQKb0Q9Song1Yy9oryCcYWHogN30j80Ctbybu +3PAh4VIfz4SjY1tNd /Dv2Il3caz +5chvX0OL DJCNl2w+Du7HjwdyszlzVc1rP4QTL /yYvrUz1fgBonRKa0/Y5CK7jEzQZ1 +ZMuN /E/ubrDX5FIp0jfPPnbtTryWW8BMloOb +Q8EFKnaI6Wbyx9JzslOIawCkM77LL9RhxMbQ6A3QoU HozmlFnhfu7AXnhfCdPb02xx4RCOAFoY9pEtOH+eHzT7 /SQVf8VzEYO1vIe4TDP6sNN40/1pjUdBPcGiBMidM6pLk/j6CaxV root@linux-node2.nmap.com [root@linux-node2 ~] # [www@linux-node1 scripts]$ cd [www@linux-node1 ~]$ vim . ssh /authorized_keys [www@linux-node1 ~]$ |

测试 ?
1 2 3 | [root@linux-node2 ~] # ssh www@192.168.58.11 Last login: Sun Apr 30 17:08:50 2017 [www@linux-node1 ~]$ |

点击立即构建,它会失败


给它sudo权限

构建这里加上sudo

保存之后执行还是不可以

把这里加上注释,不让它需要tty

再次构建

执行成功

ls -lrt 检查 ?
1 2 3 4 | drwxrwxr-x 3 www www 69 Apr 30 17:09 web-demo_798366_2017-04-30-17-09-49 -rw-rw-r-- 1 www www 9063 Apr 30 20:06 web-demo_798366_2017-04-30-20-06-29. tar .gz drwxrwxr-x 3 www www 69 Apr 30 20:06 web-demo_798366_2017-04-30-20-06-29 [root@linux-node2 webroot] # |
现在我们有2个,一个是代码质量的,一个是自动部署脚本的 显示阴天的是因为之前执行失败过。有提示

开始学钩子脚本 这边一提交(push),这边自动构建一遍 jenkins安装此插件 它是一个触发器插件

直接安装

在demo-sonar项目选择下面插件


这里选择下一个任务(项目)的名称

搜索即可,出现demo-deploy

没有参数,勾上 保存

现在的话,你要是构建demo-sonar,它就会构建demo-deploy

验证下看看 demo-sonar项目,立即构建

看到项目这里也自动执行了。11秒前

安装学习piplines插件 代码流水线插件,它会执行一系列操作 安装构建插件

使用它,点击选中的加号,新建一个视图

点击OK之后,弹出如下视图

选择初始化的job 显示最近5次的构建

点击保存

点击保存之后显示的

点击run

刷新下也看到右边执行了

点下面按钮可以看到控制台输出的快捷方式

Jenkins实战-Jenkins与gitlab集成(Webhook) 现在想实现一个功能,代码提交,push到gitlab上 jenkins自动帮你构建 ?
1 2 3 4 5 6 | [www@linux-node1 ~]$ cd web-demo/ [www@linux-node1 web-demo]$ ll total 8 -rw-rw-r-- 1 www www 26 Apr 28 22:20 index.html -rw-rw-r-- 1 www www 9 Apr 28 21:53 README.md [www@linux-node1 web-demo]$ |
先安装gitlab的钩子脚本 钩子插件已经安装了

配置钩子脚本 先在这里配置

勾选这2个 第二个是当gitlab进行发布的时候,对它进行build

它告诉你,你要在gitlab上填上下面这个url http://192.168.58.12:8080/project/demo-sonar

有一个身份认证的东西 需要再装一个插件才能实现认证 先保存了 安装下面插件

只有装了它才能使用身份验证令牌的方式,让双方验证 生成一个token ?
1 2 3 | [root@linux-node2 ~] # openssl rand -hex 10 dc8f07cefb4a25e8c198 [root@linux-node2 ~] # |
保存 在gitlab上添加这个钩子脚本 http://192.168.58.12:8080/project/demo-sonar
登录gitlab 在这个项目下找到钩子配置的地方

打开它的wiki

https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin

下面告诉你了使用办法

格式如下 buildByToken/build?job=RevolutionTest&token=TacoTuesday
http://192.168.58.12:8080/project/demo-sonar
整合下
http://192.168.58.12:8080/buildByToken/build?job=demo-sonar&token=dc8f07cefb4a25e8c198
job的名称不能错,token也不能错

点击add



页面上test一下

成功调用

日志也刷出来了

打开jenkins的pipeline页面,也能看到成功调用

模拟生产环境,写个代码,git push之后看看他能否执行 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [www@linux-node1 web-demo]$ pwd /home/www/web-demo [www@linux-node1 web-demo]$ ll total 8 -rw-rw-r-- 1 www www 26 Apr 28 22:20 index.html -rw-rw-r-- 1 www www 9 Apr 28 21:53 README.md [www@linux-node1 web-demo]$ cat index.html hehehehhehehe version 2.0 [www@linux-node1 web-demo]$ echo "test jenkins" >>index.html [www@linux-node1 web-demo]$ cat index.html hehehehhehehe version 2.0 test jenkins [www@linux-node1 web-demo]$ |
提交到gitlab上 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [www@linux-node1 web-demo]$ git add * [www@linux-node1 web-demo]$ git commit -m "jenkins hook test" [master bd2b985] jenkins hook test 1 file changed, 1 insertion(+) [www@linux-node1 web-demo]$ git push warning: push.default is unset ; its implicit value is changing in Git 2.0 from 'matching' to 'simple' . To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git) Counting objects: 5, done . Delta compression using up to 4 threads. Compressing objects: 100% (2 /2 ), done . Writing objects: 100% (3 /3 ), 304 bytes | 0 bytes /s , done . Total 3 (delta 0), reused 0 (delta 0) To git@192.168.58.11:web /web-demo .git 7983661..bd2b985 master -> master [www@linux-node1 web-demo]$ |
在jenkins页面上看到执行了

打开node2页面,看到页面是最新的了


这里面缺少自动化测试 你可以在部署后面放一个测试的脚本 比如添加一个demo-test
