环境准备

  • SonarQube 项目创建
  • jenkins Windows构建节点配置
    • 安装与SonarQube服务端相同版本jdk
    • 安装sonar-scanner 并配置环境变量
    • 安装Visual Studio 并配置环境变量
  • jenkins 任务创建

SonarQube 配置

创建项目



创建令牌



复制令牌内容分析项目需要用到

获取分析项目方法

jenkins Windows构建节点配置

解压相关文件并复制到C:\DevOps_tools\



添加系统环境变量path

C:\DevOps_tools\sonar-scanner-msbuild-5.2.1.31210-net46\

添加vs MSBuild.exe 到系统环境变量path中

sonar-scanner节点配置完成

jenkins 配置

jenkins 任务创建







pipeline

pipeline {
agent any
environment {
gitlab_tokenid = 'a6710614-3bbd-4efb-acc2-63aa8b615290' //gitlab令牌
sonar_tokenid = 'a3e55c8b9b39c05be659b44efef4d632b8764195' //前面创建的令牌 }
triggers{
GenericTrigger(
genericVariables:[
[key:'event_name',value:'$.event_name'], //触发动作 pubat or tag_pubat
[key:'user_email',value:'$.user_email'], //GitLab公共邮箱需要自行配置否则获取不到
[key:'project_name',value:'$.project.name'], //项目名称 DevOps_Test
[key:'git_url',value:'$.project.git_http_url'], //git_url
[key:'ref',value:'$.ref'], //分支或tag信息
[key:'group_name',value:'$.project.namespace'], //GITLAB_GROUP
[key:'commits_id',value:'$.commits[0].id']
],
token:"qazwsx",
causeString:'Triggered on $ref',
printContributedVariables:true,
printPostContent:true
)
} stages {
stage('Clean') {
steps{
cleanWs(
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: true,
cleanWhenSuccess: true,
cleanWhenUnstable: true,
cleanupMatrixParent: true,
disableDeferredWipeout: true,
deleteDirs: true
)
}
} stage('init') {
steps {
git credentialsId: "$gitlab_tokenid", url: "$git_url"
script {
tagname = ref.tokenize('/')[2]
bat "git checkout $tagname"
// 从文件中读取 JSON 字符串
projectsjson = readJSON file: 'projects.json'
}
}
} stage('SonarScanner') {
steps {
script{
for(j in projectsjson.projects){
if (j.sonar == "yes"){
if (j.sln_path != "./"){
// MsBuild.exe /t:Rebuild 需要找到*.sln 所以这里进行CD 切换目录
bat "cd $j.sln_path && SonarScanner.MSBuild.exe begin /k:$project_name /d:sonar.host.url=\"http://SonarQube:9000\" /d:sonar.login=$sonar_tokenid"
bat "cd $j.sln_path && MsBuild.exe /t:Rebuild"
bat "cd $j.sln_path && SonarScanner.MSBuild.exe end /d:sonar.login=$sonar_tokenid"
}else{
bat "SonarScanner.MSBuild.exe begin /k:$project_name /d:sonar.host.url=\"http://SonarQube:9000\" /d:sonar.login=$sonar_tokenid"
bat "MsBuild.exe /t:Rebuild"
bat "SonarScanner.MSBuild.exe end /d:sonar.login=$sonar_tokenid" //引用上面令牌
}
}else{
echo "已跳过$j.name项目sonar代码扫描。"
}
}
}
}
} }
post {
always {
bat ""
echo '构建结束...'
}
success {
echo '恭喜您,构建成功!!!'
mail subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 执行成功",
body: """
<div id="content">
<h1>CI报告</h1>
<div id="sum2">
<h2>Jenkins 运行结果</h2>
<ul>
<li>jenkins的执行结果 : <a>jenkins 执行成功</a></li>
<li>jenkins的Job名称 : <a id="url_1">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></li>
<li>jenkins的URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li>
<li>jenkins项目名称 : <a>${env.JOB_NAME}</a></li>
<li>Job URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li>
<li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
</ul>
</div>
<div id="sum0">
<h2>GIT 信息</h2>
<ul>
<li>GIT项目的地址 : <a>${git_url}</a></li>
<li>GIT项目当前的分支名 : ${ref}</li>
<li>GIT最后一次提交的commitID : ${commits_id}</li>
</ul>
</div>
</div>
""",
charset: 'utf-8',
from: 'IBM_nmc@naura.local',
mimeType: 'text/html',
to: "$user_email"
//to : "${Recipient}"
}
failure {
echo '抱歉,构建失败!!!'
mail subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 执行失败",
body: """
<div id="content">
<h1>CI报告</h1>
<div id="sum2">
<h2>Jenkins 运行结果</h2>
<ul>
<li>jenkins的执行结果 : <a>jenkins 执行失败</a></li>
<li>jenkins的Job名称 : <a id="url_1">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></li>
<li>jenkins的URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li>
<li>jenkins项目名称 : <a>${env.JOB_NAME}</a></li>
<li>Job URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li>
<li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
</ul>
</div>
<div id="sum0">
<h2>GIT 信息</h2>
<ul>
<li>GIT项目的地址 : <a>${git_url}</a></li>
<li>GIT项目当前的分支名 : ${ref}</li>
<li>GIT最后一次提交的commitID : ${commits_id}</li>
</ul>
</div>
</div>
""",
charset: 'utf-8',
from: 'IBM_nmc@naura.local',
mimeType: 'text/html',
to: "$user_email"
}
unstable {
echo '该任务已经被标记为不稳定任务....'
}
changed {
echo ''
}
} }

Jenkins+SonarQube实现C#代码质量检查的更多相关文章

  1. jenkins+sonarqube进行代码质量检测

    JavaNeverGiveUp教程篇 用jenkins+sonarqube去检查代码是非常方便的,它能检查出代码中可能存在的一些问题,比如io流未关闭.空指针异常.死循环.代码不规范等问题. 1. 搭 ...

  2. Jenkins系列——使用SonarQube进行代码质量检查

    1.目标 之前已经写过一篇关于Jenkins和SonarQube的一篇博客<jenkins集成sonar>,本文在参考前文的基础上,做了详细的补充. 使用SonarQube进行代码质量检查 ...

  3. jenkins集成sonarQube实现代码质量检查

    1.sonarQube的简介 SonarQube是一款自动化代码审查工具,用于检测代码中的错误.漏洞和代码异味.它可以与你现有的工作流集成,以支持跨项目分支和拉取请求的连续代码检查. 其工作流程如下: ...

  4. 在 k8s 中的 jenkins 集成 sonarqube 实现代码质量检查

    不乱于心,不困于情,不畏将来,不念过往,如此安好 --<不宠无惊过一生>丰子恺 概述 关于在 k8s 中安装 jenkins 和 sornarqube 可以查看下面的文章: 在 k8s 中 ...

  5. 自动代码质量分析(GitLab+JenKins+SonarQube)

    自动代码质量分析(GitLab+JenKins+SonarQube) 1.需求场景 开发提交代码自动执行代码质量分析. 2.所需应用 GitLab,JenKins,SonarQube 3.架构图 4. ...

  6. git jenkins SonarQube手动代码质检

    SonarQube代码质检:1.提交代码-->gitlab-->jenkins抓取-->sonarqube质量检测-->maven编译-->shell-->web集 ...

  7. 持续集成工具之jenkins+sonarqube做代码扫描

    上一篇我们主要聊了下代码质量管理平台sonarqube的安装部署以及它的工作方式做了简单的描述和代码扫描演示:回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13 ...

  8. 手把手教你用SonarQube+Jenkins搭建--前端项目--代码质量管理平台 (Window系统)

    前言 网上教程大多介绍的是Linux系统下SonarQube+Jenkins如何使用,这是因为这两款软件一般都是部署在服务器上,而大多数服务器,采用的都是Linux系统.大多数服务器用Linux的原因 ...

  9. gitlab-ci集成SonarQube代码质量检查

    SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误. docker安装 1.拉取 postgres:docker pull postgres:10 2.拉取sona ...

随机推荐

  1. ES6中的箭头函数的语法、指向、不定参数

    箭头函数的语法 function fn1() { console.log(1); } let fn2 = () => { console.log(2); } fn1()//1 fn2()//2 ...

  2. jolokia配置Java监控

    wget http://search.maven.org/remotecontent?filepath=org/jolokia/jolokia-jvm/1.3.6/jolokia-jvm-1.3.6- ...

  3. Zabbix agent端 配置

    Zabbix agent端 配置 agent端环境 zabbix-client:RHEL8 IP:192.168.121.11 一.安装 Zabbix 源 [root@zabbix-client ~] ...

  4. Linux基本原则

    Bash特性 Shell shell(外壳),广义的shell可以理解为是用户的工作环境,在windows看来桌面就是一个shell,在linux看来终端就是shell 常见的shell有两种,一种是 ...

  5. linux 服务开机自启动systemd方式 (Centos7)

    linux 服务开机自启动systemd方式 (Centos7) 1.编写一个 /opt/hello.sh 脚本 [root@jws-ftp prometheus]# cat /opt/hello.s ...

  6. Linux中级之ansible概念及hoc命令行调用模式

    一.Ansible简介 ansible是新出现的开源的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统 ...

  7. 僵尸进程和孤儿进程-(转自Anker's Blog)

    2.基本概念 我们知道在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程.子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束. 当 ...

  8. linux(centos 7)下安装JDK,Tomcat,mysql 运行Maven 项目

    一.在Linux中安装JDK 1. 将JDK上传到root下(任何位置均可以). 如图: 2. 用解压命令解压JDK tar -xvf (此处为jdk文件名) 如果是rpm包,执行rpm -i jdk ...

  9. GPU微观物理结构框架

     GPU微观物理结构框架 一.CPU 和 GPU 在物理结构和设计上有何区别 首先需要解释CPU(Central Processing Unit)和GPU(Graphics Processing Un ...

  10. Nucleus SE RTOS初始化和启动

    Nucleus SE RTOS初始化和启动 Nucleus SE RTOS initialization and start-up 对于任何类型的操作系统,都有某种类型的启动机制.具体的工作方式因系统 ...