当前位置: 首页 > news >正文

Jenkins集成SonarQube实现代码质量检查

文章目录

    • 一、前提配置
      • 1.1 安装及配置SonarQube Scanner插件
      • 1.2 配置SonarQube servers
    • 二、非流水线集成SonarQube
      • 1.1 配置非流水线任务
    • 三、流水线集成SonarQube

一、前提配置

1.1 安装及配置SonarQube Scanner插件

(1) 点击【系统管理】>【插件管理】>【可选插件】搜索SonarQube Scanne点击安装

(2)点击【系统管理】>【全局工具配置】搜索SonarQube Scanner新建一个自动安装

1.2 配置SonarQube servers

(1)使用admin用户登入sonar平台,点击【右上角用户头像】>【我的账号】>【安全】生成一个新的令牌,复制令牌内容后面会使用到

(2)新建凭证:点击【系统管理】>【Manager files】>【全局】创建sonar凭证

(3)点击【系统管理】>【系统配置】搜索到 SonarQube servers 进行配置

(4)SonarQube关闭审查上传SCM功能

二、非流水线集成SonarQube

1.1 配置非流水线任务

# 项目唯一标记
sonar.projectKey=web_JAR
# 项目名字
sonar.projectName=web_JAR
# 版本号
sonar.projectVersion=1.0
# 指定扫描目录 点表示项目根目录
sonar.sources=.
# 对test、target目录不进行扫描
sonar.exclusions=**/test/**,**/target/**
# JDK设置
sonar.java.source=1.8
sonar.java.target=1.8
# 字符集
sonar.sourceEncoding=UTF-8

三、流水线集成SonarQube

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

# must be unique in a given SonarQube instance
sonar.projectKey=web_demo
# this is the name and version displayed in the SonarQube UI. Was mandatory
prior to SonarQube 6.1.
sonar.projectName=web_demo
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

流水线代码:

pipeline {agent anystages {stage('拉取代码') {steps {checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'git@152.136.184.149:gitlab-instance-0d7d20f4/cicd.git']]])}}stage('打JAR包') {steps {sh 'mvn clean package'}}stage('SonarQube代码审查') {steps{script {//引入Jenkins SonarQube-Scanner全局工具 "全局配置中以SonarQube-Scanner命名的工具"scannerHome = tool 'SonarQube-Scanner'}//引用SonarQube环境 "系统配置中配置的SonarQube servers的name值 "withSonarQubeEnv('sonar') {//执行sonar-scanner命令sh "${scannerHome}/bin/sonar-scanner"}}}stage('部署前操作') {steps {sshPublisher(publishers: [sshPublisherDesc(configName: '120.48.34.146', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile
bash /home/qinzt/jar/helloword/run.sh stop helloword-*.jar
mv /home/qinzt/jar/helloword/helloword-*.jar{,.$(date +%F-%S)}''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}}stage('远程部署') {steps {sshPublisher(publishers: [sshPublisherDesc(configName: '120.48.34.146', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile
cd /home/qinzt/jar/helloword && ./run.sh start helloword-*.jar''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'jar/helloword', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: '**/helloword-*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}   }}post {//构建失败发邮件通知unsuccessful  {emailext attachLog: true, body: '${FILE,path="email.html"}', subject: '【构建通知】:${PROJECT_NAME}', to: '1790168505@qq.com'}}
}
http://www.lryc.cn/news/66505.html

相关文章:

  • 2023 谷歌I/O发布会新AI,PALM 2模型要反超GPT-4,一雪前耻!
  • MySQL和Redis如何保证数据一致性?
  • Markdown使用(超详细)
  • yolov5实现扑克牌识别的产品化过程
  • 第07讲:Java High Level Client,读写 ES 利器
  • dockerfile暴力处理配置文件外提
  • 如何快速给出解释——正交矩阵子矩阵的特征值的模必然不大于1
  • c语言-位运算
  • 【Android学习专题】安卓样式学习(学习内容记录)
  • 普罗米修斯统计信息上报结构设计
  • 两个系统之间的传值
  • PostgreSQL(五)JDBC连接串常用参数
  • 如何修改浏览器中导航栏的背景色和字体
  • 如何选择合适的智能氮气柜?
  • 双向链表(数据结构)(C语言)
  • 离线安装Percona
  • 界面控件Telerik UI for WinForms使用指南 - 数据绑定 填充(二)
  • 通过栈/队列/优先级队列/了解容器适配器,仿函数和反向迭代器
  • leetcode 704. 二分查找
  • 蓝牙耳机什么牌子好?500内好用的蓝牙耳机推荐
  • 设计模式 -- 中介者模式
  • 人工智能的未来之路:语音识别的应用与挑战
  • c++ 友元介绍
  • 四维轻云地理空间数据在线管理软件能够在线管理哪些数据?
  • 学习 GitHub 对我们有什么好处?
  • java记录-反射
  • 这次彻底不需要账号了,无需魔法永久白嫖GPT
  • 远程桌面连接是什么?如何开启远程桌面连接详细教程
  • lua实战(2)
  • UI自动化测试案例——简单的Google搜索测试