前提:
Jenkins
JDK
目录:
1、安装sonar插件:SonarQube Scanner for Jenkins
2、安装SonarQube
3、安装sonar-scanner
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1.安装sonar插件:SonarQube Scanner for Jenkins
2、安装SonarQube
2.1 下载
下载地址:
2.2 解压
下载后放在/usr/local目录下,并解压
unzip sonarqube-6.7.6.zip
2.3 配置环境变量
vi /etc/profile
里面写如下
#set sonarqubeexport SONAR_HOME=/usr/local/sonarqube-6.7.6PATH=$PATH:$SONAR_HOME/bin
使配置生效
source /etc/profile
2.4 启动并测试
进入到启动目录
/usr/local/sonarqube-6.7.6/bin/linux-x86-64
执行启动命令
./sonar.sh start
启动时要查看es.log和sonar.log日志
tail -f /usr/local/sonarqube-6.7.6/logs/es.log
tail -f /usr/local/sonarqube-6.7.6/logs/sonar.log
用root启动会报错,请参考我的另一篇文章:
所有问题解决完后启动,并测试,访问ip+port,OK了
我这里是因为默认的9000端口被占用了,所以修改了默认端口 vi /usr/local/sonarqube-6.7.6/conf/sonar.properties
3、安装sonar-scanner
3.1 下载
3.2 解压
下载后放在/usr/local目录下,并解压
unzip sonar-scanner-cli-3.2.0.1227-linux.zip
3.3 配置环境变量
和上面一样修改/etc/profile文件
#set sonarqubeexport SONAR_HOME=/usr/local/sonarqube-6.7.6export SONAR_SCANNER_HOME=/usr/local/sonar-scanner-3.2.0.1227-linuxPATH=$PATH:$SONAR_HOME/bin:$SONAR_SCANNER_HOME/bin
3.4 测试
进入/usr/local/sonar-scanner-3.2.0.1227-linux/bin执行:
./sonar-scanner -h
如果出现如下就说明安装好了
[root@iZbp1bb2egi7w0ueys548qZ bin]# ./sonar-scanner -hINFO: INFO: usage: sonar-scanner [options]INFO: INFO: Options:INFO: -D,--defineDefine propertyINFO: -h,--help Display help informationINFO: -v,--version Display version informationINFO: -X,--debug Produce execution debug output
4、在Jenkins中配置sonar
SonarScanner和SonarQube的关系类似于客户端与服务端,由于SonarScanner工具需要把扫描的代码及结果发送到SonarQube服务器上,所以需要配置SonarQube服务地址。
在Jenkins系统配置>>系统设置中配置如下:
5、在Jenkins中配置sonar-scanner
在全局工具配置中配置如下:
6、在项目根目录下新建sonar-project.properties文件
内容如下
# must be unique in a given SonarQube instancesonar.projectKey=cynomys:0.0.1# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.sonar.projectName=cynomyssonar.projectVersion=0.0.1# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.# This property is optional if sonar.modules is set.sonar.sources=.sonar.exclusions=**/test/**,**/target/**sonar.java.source=1.8sonar.java.target=1.8# Encoding of the source code. Default is default system encodingsonar.sourceEncoding=UTF-8 # 最新版本的sonarQube6.5只能扫描class文件,需要指定classes文件路径sonar.java.binaries=/root/.jenkins/workspace/e-content/e-content-controller/target/classes
7、编写pipeline脚本
node { maven_home='/usr/local/apache-maven-3.5.4' stage('Pull Code'){ git branch: 'master', credentialsId: 'gitlab_account', url: 'http://*****.git' } stage('Code Check') { echo "代码检查!" } stage('SonarQube analysis') { // SonarQubeServer为系统配置的SonarQube servers的名称 withSonarQubeEnv('SonarQubeServer') { sh '/usr/local/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner' } } stage('Unit Test') { echo "单元测试 !" } stage('Compile') { echo '编译开始' // 暂时打包报错 sh "'$maven_home'/bin/mvn install" } stage('Build Images') { echo '敬请期待!' } stage('Backup'){ echo '旧版本软件包已经备份' } stage('Delete Old war'){ echo "删除老的包" } stage('Deploy'){ echo '部署!' sh 'echo hello' // sh 'scp /root/.jenkins/workspace/e-trade/e-trade-mapper/target/e-trade-mapper-1.0-SNAPSHOT.jar root@10.132.131.51:/home/guchen_test' } stage('Integration Testing') { echo "敬请期待!" } stage('UI Automated Testing') { echo "敬请期待!" } stage('Send Testing Report') { echo "敬请期待!" }}
8、构建一下