SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。
目前支持 java,C,C++,python,SQL,Java 等二十几种编程语言的代码质量管理与检测。
官网:https://www.sonarqube.org/
软件 | 服务器 | 版本 |
---|---|---|
JDK | 192.168.168.13 | 1.8 |
MySQL | 192.168.168.13 | 5.7.29 |
SonarQube | 192.168.168.13 | 7.8(必须是7.8版本) |
[root@localhost ~]# tar -zxvf jdk-8u201-linux-x64.tar.gz -C /usr/src
[root@localhost ~]# mv /usr/src/jdk1.8.0_201/ /usr/local/java
[root@localhost ~]# echo 'export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin' >> /etc/profile
[root@localhost ~]# rm -rf /usr/bin/java
[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
安装步骤省略…
mysql> create database sonar;
mysql> grant all on *.* to 'sonar'@'192.168.168.%' identified by '123.com';
下载地址:https://www.sonarqube.org/success-download-community-edition/
[root@jenkins ~]# unzip sonarqube-8.4.1.35646.zip
[root@jenkins ~]# useradd sonar
[root@jenkins ~]# chown -R sonar:sonar /usr/local/sonar
[root@jenkins ~]# cd /usr/local/sonar/
vim /usr/local/sonar/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=123.com
sonar.jdbc.url=jdbc:mysql://192.168.168.13:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
#sonar.web.port=9000 指定 sonar-web 页面访问端口
ES 要求系统文件描述符 > 65535
#软限制文件数 soft nofile
[root@node1 ~]# echo '* soft nofile 655360' >> /etc/security/limits.conf
#硬限制文件数
[root@node1 ~]# echo '* hard nofile 655360' >> /etc/security/limits.conf
#软限制用户可用进程数 soft nproc
[root@node1 ~]# echo '* soft nproc 2048' >> /etc/security/limits.conf
#硬限制用户可用进程数 hard nproc
[root@node1 ~]# echo '* hard nproc 4096' >> /etc/security/limits.conf
#JVM能够使用的最大线程数
[root@node1 ~]# echo 'vm.max_map_count=655360' >> /etc/sysctl.conf
[root@node1 ~]# sysctl -p
vm.max_map_count = 655360
#root 用户下直接生效 ulimit
echo 'root soft nofile 655360
root hard nofile 655360
root soft nproc 655360
root hard nproc 655360 ' >> /etc/security/limits.conf
[sonar@jenkins ~]# useradd sonar
[sonar@jenkins ~]# passwd sonar
[sonar@jenkins ~]# su sonar
[sonar@jenkins sonar]$ cd /usr/local/sonar/
[sonar@jenkins sonar]$ ./bin/linux-x86-64/sonar.sh start
[sonar@jenkins sonar]$ ./bin/linux-x86-64/sonar.sh status
[sonar@jenkins sonar]$ netstat -anpt
tcp6 0 0 :::9000 :::* LISTEN 7775/java
tcp6 0 0 127.0.0.1:9001 :::* LISTEN 7683/java
http://192.168.168.13:9000/默认用户名 admin,密码 admin
8147d9493f2950690dc5fdcee0c79c47fa94a8fe
设置全局配置
全局工具名:sonar-Scanner
sonar服务名:soarqube
这两个配置的 名字非常重要,需要在 sonar 扫描脚本中引用。
构建:
审查脚本
### 注意大小写
sonar.projectKey=web_demo_freestyle
sonar.projectName=web_demo_freestyle
sonar.projectVersion=1.0
#扫描路径 “.”代表全部内容
sonar.sources=/src/main/*
#排除扫描路径
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
sonar.sourceEncoding=UTF-8
构建成功信息如下:
命名为:sonar-project.properties(名称固定)
### 注意大小写
sonar.projectKey=web_demo_pipeline
sonar.projectName=web_demo_pipeline
sonar.projectVersion=1.0
#扫描路径 “.”代表全部内容
sonar.sources=.
#排除扫描路径
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
sonar.sourceEncoding=UTF-8
代码扫描片段:
stage('code checking') { // 检索代码步骤
steps {
script { //引入 sonar-Scanner 工具,全局配置下设置的名字
scannerHome = tool 'sonar-Scanner'
}
withSonarQubeEnv('sonarqube') { //引入 sonarqube 服务名字,jenkins服务配置
sh "${scannerHome}/bin/sonar-scanner" //sonar-scanner 触发代码扫描命令
}
}
}
完整脚本:
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-manven', url: '[email protected]:cheliang/test2.git']]])
}
}
stage('code checking') {
steps {
script {
scannerHome = tool 'sonar-Scanner'
}
withSonarQubeEnv('sonarqube') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('build project') {
steps {
sh label: '', script: '''echo "======= 开始打包 ========"
source /etc/profile
mvn clean package
echo "======= 打包结束、开始部署 ======="'''
}
}
stage('publish project') {
steps {
deploy adapters: [tomcat9(credentialsId: 'b73170b6-fa08-4350-9d35-530b1eb19b75', path: '', url: 'http://192.168.168.5:8080/')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext (
subject: '\'构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}\'',
body: '${FILE,path="email.html"}',
to: '[email protected]'
)
}
}
}