ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

7. jenkins的代码审查

2022-07-08 17:03:56  阅读:146  来源: 互联网

标签:opt 审查 sonarqube 代码 SonarQube sonar jenkins


sonar基本使用

1,sonar安装和配置

SonarQube是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前 支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检 测。
官网:https://www.sonarqube.org/

安装SonarQube

1)安装MySQL(已完成)

2)安装SonarQube
在MySQL创建sonar数据库

下载sonar压缩包:
https://www.sonarqube.org/downloads/
解压sonar,并设置权限

yum install unzip
unzip sonarqube-6.7.4.zip 解压
mkdir /opt/sonar 创建目录
mv sonarqube-6.7.4/* /opt/sonar 移动文件
useradd sonar 创建sonar用户,必须sonar用于启动,否则报错
chown -R sonar. /opt/sonar 更改sonar目录及文件权限修改sonar配置文件

vi /opt/sonarqube-6.7.4/conf/sonar.properties
内容如下:

sonar.jdbc.username=root
sonar.jdbc.password=Root@123
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs= maxPerformance&useSSL=false

注意:sonar默认监听9000端口,如果9000端口被占用,需要更改。 启动sonar
cd /opt/sonarqube-6.7.4

su sonar ./bin/linux-x86-64/sonar.sh start 启动
su sonar ./bin/linux-x86-64/sonar.sh status 查看状态
su sonar ./bin/linux-x86-64/sonar.sh stop 停止

tail -f logs/sonar.logs 查看日志访问sonar http://192.168.66.101:9000

image-20211212165547756

默认账户:admin/admin 创建token

image-20211212165622706

2. jenkins配置sonar

安装SonarQube Scanner插件

image-20211212165852279

添加SonarQube凭证

image-20211212165921164

Jenkins进行SonarQube配置
Manage Jenkins->Configure System->SonarQube servers

image-20211212170009841

Manage Jenkins->Global Tool Configuration

image-20211212170113851

SonaQube关闭审查结果上传到SCM功能

image-20211212170146989

3. Jenkins+SonarQube代码审查(2) - 实现代码审查

image-20211212165830204

非流水线项目

项目风格为freestyle或者maven项目,在增加构建步骤选择sonar Scanner,添加脚本

# must be unique in a given SonarQube instance
sonar.projectKey=web_demo_freestyle
# this is the name and version displayed in theSonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=web_demo_freestyle
sonar.projectVersion=1.0

# 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.8 
sonar.java.target=1.8

# Encoding of the source code. Default is default system encoding 
sonar.sourceEncoding=UTF-8

image-20211212164426162

执行构建,后访问sonar webui

image-20211212164748492

流水线项目

区别在于,构建脚本在项目中,采用pipeline语法构建的。

image-20211212164911064

在项目添加SonaQube代码审查(流水线项目)

1)项目根目录下,创建sonar-project.properties文件

2)修改Jenkinsfile,加入SonarQube代码审查阶段

pipeline { agent any


stages {
stage('拉取代码') { steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '68f2087f-a034-4d39-a9ff-1f776dd3dfa8', url: 'git@192.168.66.100:itheima_group/web_demo.git']]])
}
}
stage('编译构建') { steps {
sh label: '', script: 'mvn clean package'
}
}
stage('SonarQube代码审查') { steps{
script {
scannerHome = tool 'sonarqube-scanner'
}
withSonarQubeEnv('sonarqube6.7.4') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('项目部署') { steps {
deploy adapters: [tomcat8(credentialsId: 'afc43e5e-4a4e-4de6-984f- b1d5a254e434', path: '', url: 'http://192.168.66.102:8080')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext(
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -
${BUILD_STATUS}!',
body: '${FILE,path="email.html"}', to: '1014671449@qq.com'
)
}
}
}

1) 到SonarQube的UI界面查看审查结果

解决新版sonar-java插件需要配置sonar.java.binaries参数的问题

https://blog.csdn.net/u013111003/article/details/79927668

标签:opt,审查,sonarqube,代码,SonarQube,sonar,jenkins
来源: https://www.cnblogs.com/nwnusun/p/16458920.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有