前提:
Jenkins
JDK
目录:
1、安装sonar插件:SonarQube Scanner for Jenkins
2、安装SonarQube
3、安装sonar-scanner
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1.安装sonar插件:SonarQube Scanner for Jenkins
2、安装SonarQube
2.1 下载
下载地址:https://www.sonarqube.org/downloads/
2.2 解压
下载后放在/usr/local目录下,并解压
unzip sonarqube-7.4.zip
2.3 配置环境变量
vi /etc/profile
里面写如下
#set sonarqube export SONAR_HOME=/usr/local/sonarqube-7.4 PATH=$PATH:$SONAR_HOME/bin
使配置生效
source /etc/profile
2.4 启动并测试
进入到启动目录
/usr/local/sonarqube-7.4/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启动会报错,请参考我的另一篇文章:https://www.cnblogs.com/gcgc/p/10239590.html
所有问题解决完后启动,并测试,访问ip+port,OK了
我这里是因为默认的9000端口被占用了,所以修改了默认端口 vi /usr/local/sonarqube-7.4/conf/sonar.properties
3、安装sonar-scanner
3.1 下载
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
3.2 解压
下载后放在/usr/local目录下,并解压
unzip sonar-scanner-cli-3.2.0.1227-linux.zip
3.3 配置环境变量
和上面一样修改/etc/profile文件,修改完记得 source /etc/profile
#set sonarqube export SONAR_HOME=/usr/local/sonarqube-7.4 export SONAR_SCANNER_HOME=/usr/local/sonar-scanner-3.2.0.1227-linux PATH=$PATH:$SONAR_HOME/bin:$SONAR_SCANNER_HOME/bin
修改完记得 source /etc/profile
3.4 测试
进入/usr/local/sonar-scanner-3.2.0.1227-linux/bin执行:
./sonar-scanner -h
如果出现如下就说明安装好了
[root@iZbp1bb2egi7w0ueys548qZ bin]# ./sonar-scanner -h INFO: INFO: usage: sonar-scanner [options] INFO: INFO: Options: INFO: -D,--define <arg> Define property INFO: -h,--help Display help information INFO: -v,--version Display version information INFO: -X,--debug Produce execution debug output
4、在Jenkins中配置sonar
SonarScanner和SonarQube的关系类似于客户端与服务端,由于SonarScanner工具需要把扫描的代码及结果发送到SonarQube服务器上,所以需要配置SonarQube服务地址。
在Jenkins系统配置>>系统设置中配置如下:
5、在Jenkins中配置sonar-scanner
在全局工具配置中配置如下:
6、增加构建步骤
内容如下
sonar.projectKey=e-auth sonar.projectName=电商用户服务-auth sonar.projectVersion=1.0 sonar.sources=./ sonar.language=java sonar.sourceEncoding=UTF-8 sonar.java.binaries=./ sonar.login=admin sonar.password=admin
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、构建一下