赞
踩
SonarQube是基于java开发的代码质量检测工具,一般开发会在上面检查自己的代码问题,而我们运维需要搭建出来,确保他们正常使用,并且需要集成jenkins实现由jenkins将gitlab的内容推送给SonarQube检测。
目录
- 1. SonarQube基础java开发,需安装open JDK8版本
- 2. SonarQube需要依赖MySQL数据库,至少5.6版本以上
- 3. SonarQube的小型实例至少4G内存,如果大型实例需要16G内存
- [root@Sonar ~]# rpm -ivh jdk-8u181-linux-x64.rpm
-
- https://dev.mysql.com/downloads/repo/yum/ #该网址找到mysql下载链接
- [root@Sonar ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
- [root@Sonar ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
- [root@Sonar ~]# cat /etc/yum.repos.d/mysql-community.repo
- ......
- [mysql56-community]
- name=MySQL 5.6 Community Server
- baseurl=http://repo.mysql.com/yum/mysql-5.6-community/e
- l/7/$basearch/
- enabled=1 #更改5.6的enabled为0
- gpgcheck=1
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- ......
- [mysql80-community]
- name=MySQL 8.0 Community Server
- baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
- enabled=0 #更改8.0的enabled为1
- gpgcheck=1
- gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- ......
- [root@Sonar ~]# yum -y install mysql-community-server #安装mysql
- [root@Sonar ~]# service mysql start #启动mysql
- Redirecting to /bin/systemctl start mysql.service
某些情况下可能会出现MySQL监听了TCP6端口而无法正常访问数据库的问题
- #如果端口监听到tcp6,增加一条配置重启即可
- [root@sonar ~]# grep bind /etc/my.cnf
- bind-address=0.0.0.0 #在my.cnf文件中配置bind-address参数来指定MySQL进程的绑定IP地址;
-
- #或者关闭系统的tcp6
- vim /etc/sysctl.conf #加入以下三条配置,在sysctl.conf文件中设置这三个内核参数,以禁用所有的IPv6连接,这样即时MySQL配置错误地监听了TCP6端口,也不会阻塞TCP4端口。
- net.ipv6.conf.all.disable_ipv6 = 1
- net.ipv6.conf.default.disable_ipv6 = 1
- net.ipv6.conf.lo.disable_ipv6 = 1
- sysctl -p #使用sysctl -p命令来重新加载sysctl.conf,使刚刚设置的内核参数生效。
配置数据库密码,创建数据库
- [root@Sonar ~]# mysqladmin -uroot password 123
- Warning: Using a password on the command line interface can be insecure.
- [root@Sonar ~]# mysqladmin -uroot password 123
- Warning: Using a password on the command line interface can be insecure.
- [root@Sonar ~]# mysql -uroot -p123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
- Warning: Using a password on the command line interface can be insecure.
- [root@Sonar ~]# mysql -u root -p123 -e "show databases"
-
- Warning: Using a password on the command line interface can be insecure.
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sonar |
- +--------------------+
SonarQube服务下载安装,并使用sonar用户启动
- #官网链接,可以在里面找需要下载的版本,这里我选择7.0,下载链接放到文末了
- https://www.sonarsource.com/products/sonarqube/downloads/
-
- [root@Sonar ~]# unzip sonarqube-7.0.zip
- [root@Sonar ~]# mv sonarqube-7.0 /usr/local/
- [root@Sonar ~]# ln -s /usr/local/sonarqube-7.0/ /usr/local/sonarqube
- [root@Sonar ~]# useradd sonar
- [root@Sonar ~]# chown -R sonar.sonar /usr/local/sonarqube-7.0/
- [root@Sonar ~]# cat /usr/local/sonarqube/conf/sonar.properties
- ......
- sonar.jdbc.username=root
- sonar.jdbc.password=123
- sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewrieBatchedSta
- tements=true&useConfigs=maxPerformance&useSSL=false
- ......
使用普通用户启动sonarqube
- [root@Sonar ~]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
- Starting SonarQube...
- Started SonarQube.
启动后的端口为9000,使用浏览器直接访问http://10.0.0.203:9000/about,用户名admin 密码admin,生成一个token,只显示一次,记录好
点击下载链接进入文档选择要下载的客户端版本,并配置服务器地址
我这边选择使用4.2.0,下载链接放在文末
保存好生成的内容
将客户端配置到jenkins
- [root@Jenkins ~]# unzip sonar-scanner-cli-4.2.0.1873-linux.zip
- [root@Jenkins ~]# mv sonar-scanner-4.2.0.1873-linux/ /usr/local/
- [root@Jenkins ~]# cd /usr/local/
- [root@Jenkins local]# ln -s sonar-scanner-4.2.0.1873-linux/ sonar-scanner
- [root@Jenkins local]# cat sonar-scanner/conf/sonar-scanner.properties
- ......
- sonar.host.url=http://10.0.0.203:9000
- sonar.login=8d006485eddea1483554f993f5ae384be96e12e8 #提前保存的密钥
- #----- Default source code encoding
- sonar.sourceEncoding=UTF-8
配置好后点击跳过
1、安装中文汉化插件,依次点击找到,install后点击restart生效
2、默认已经安装C、Java、Python、PHP、JS等代码质量分析工具,如果一个项目使用了JAVA、HTML、JS、HTML,默认情况下只会检查JAVA,JS等代码的漏洞和bug,如果安装了HTML、CSS等插件,则会检测该项目代码中JAVA、JS、HTML、CSS代码的漏洞和bug
我提前将下载好的模块打包好了,重新导入进去解压即可,下载链接放到文末了
- [root@Sonar ~]# mv /usr/local/sonarqube/extensions/plugins /usr/local/sonarqube/extensions/plugins_back
- [root@Sonar ~]# tar xf sonar_plugins.tar.gz -C /usr/local/sonarqube/extensions/
- [root@Sonar ~]# chown -R sonar.sonar /usr/local/sonarqube/extensions/plugins
- [root@Sonar ~]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
- Stopping SonarQube...
- Waiting for SonarQube to exit...
- Stopped SonarQube.
- Starting SonarQube...
- Started SonarQube.
刷新页面显示了中文
在项目目录下进行代码推送分析,先打开认证
在Jenkins执行下面代码,推送html代码
- [root@Jenkins local]# cd /var/lib/jenkins/workspace/freestyle-job
- [root@Jenkins freestyle-job]# /usr/local/sonar-scanner/bin/sonar-scanner \
- -Dsonar.projectKey=html \
- -Dsonar.sources=. \
执行后在页面查看
推送java代码进行分析
- # 方法1
- cd /var/lib/jenkins/workspace/my-maven-job
- /usr/local/sonar-scanner/bin/sonar-scanner \
- -Dsonar.projectKey=java \
- -Dsonar.sources=.
- # 方法2
- cd /var/lib/jenkins/workspace/my-maven-job
- mvn sonar:sonar \
- -Dsonar.host.url=http://10.0.0.203:9000 \
- -Dsonar.login=8d006485eddea1483554f993f5ae384be96e12e8
1、首先需要在Jenkins安装Sonar的插件
2、配置sonar的服务端信息
在系统管理,系统设置,找到sonarqube的字样
点击添加后填写好sonar的信息和url
添加sonar的凭据
选择好类型,填写好sonar的密钥,点击提交
再选择好刚刚提交的凭据,之后点保存即可
3、配置sonar的命令路径
要使jenkins能找到sonar-scanner命令的目录,在系统设置,全局工具配置里面设置
在工程配置里,增加sonar构建项
增加后调整与发布项目的位置,实现先测试再发布项目。
- sonar.projectName=${JOB_NAME} # 项目在sonarqube上的显示名称
- sonar.projectKey=html # 项目的唯一表示,不能重复
- sonar.sources=. # 扫描那个项目的源码
将上面代码内容添加进Analysis properties配置项
此时构建项目,就会实现先去sonar检测再去发布代码,点立即构建,发现构建成功。
此处还显示了构建状态,点击就会跳转到sonar界面
此时sonar中的项目更新
如果是配置Java项目的话,Analysis properties参数项填写如下。前者是根据代码位置分析代码,后者是先把代码构建再进行分析
- sonar.projectName=${JOB_NAME}
- sonar.projectKey=java
- sonar.sources=.
- sonar.java.binaries=jeesns-service/target/sonar
-
- 或者
-
- clean
- verify
- sonar:sonar
- -Dsonar.host.url=http://10.0.0.203:9000
- -Dsonar.login=0807eba6dcceabe5bce653a9c4efe95ce6a8ac99
Sonarqube7.0服务端下载链接:https://pan.baidu.com/s/1e6Te3sg3PaKqY66iz_qkKA?pwd=d9jx
Sonarqube4.2客户端下载链接:https://pan.baidu.com/s/1DGkAXDlLq_rueQZfIo9kxg?pwd=gerw
Sonarqube模块下载链接:https://pan.baidu.com/s/1t4G3sNpJXqnFzYMPdYQZUQ?pwd=wbcl
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。