Jenkins+SonarQube实现C#代码质量检查

环境准备

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

SonarQube 配置

创建项目

image
image

创建令牌

image
image

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

获取分析项目方法

image

jenkins Windows构建节点配置

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

image
image

添加系统环境变量path

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

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

image

sonar-scanner节点配置完成

jenkins 配置

jenkins 任务创建

image
image
image
image

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@lanxiang.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@lanxiang.local',
            mimeType: 'text/html',
            to: "$user_email"
        }
        unstable {
            echo '该任务已经被标记为不稳定任务....'
        }
        changed {
            echo ''
        }
    }

}
posted @ 2021-07-02 17:30  老头还我葵花宝典  阅读(410)  评论(0)    收藏  举报