8. 在Jenkins中集成Sonar Qube和Sonar Scanner

1 SonarQube和SonarScan的安装配置

1.1 Windows环境下的安装和使用

1.1.1 启动sonarqube服务

image.png
image.png
image.png
image.png

1.1.2 在mysql里面创建一个存储扫描结果的库(比如sonarDB)

image.png

1.1.3 配置sonar qube的sonar.properties

image.png
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarDB?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

sonar.jdbc.username=root  

sonar.jdbc.password=root

sonar.sorceEncoding=UTF-8

1.1.4 重启sonar qube(使用ctrl + c)

停止sonar qube(使用ctrl + c)

image.png

然后重新启动sonar qube...

1.1.5 Sonar Scanner插件配置

image.png
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarDB?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.jdbc.username=root

sonar.jdbc.password=root

# 如果测试项目与服务器不在同一台机子,则需要添加SonarQube服务器的IP:
sonar.host.url=http://localhost:9000

1.1.6 运行Sonar scanner

注意:需要提前把sonar qube运行起来

1.1.6.1 下载代码到本地

1.1.6.2 创建一个sonar-project.properties


sonar.projectKey=yay:project

# this is the name displayed in the SonarQube UI

sonar.projectName=whatproject.0project

sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.

# Since SonarQube 4.2, this property is optional if sonar.modules is set.

# If not set, SonarQube starts looking for source code from the directory containing

# the sonar-project.properties file.

sonar.sources=personrecord

sonar.java.binaries=newtarget

sonar.language=java

# Encoding of the source code. Default is default system encoding

sonar.sourceEncoding=UTF-8
image.png

1.1.6.3 运行sonar-scanner执行代码扫描

D:\svncode>D:\softwareback\devops\sonar\sonar-scanner-3.0.3.778-windows\bin\sonar-scanner
image.png

注意,在执行过程中很可能会遇到这种错误:
image.png

这主要是由于生产的报告大于mysql允许的最大包大小(比如默认值只允许4M)。
解决方法为修改mysql根目录里面的my.ini文件,增加一行:max_allowed_packet= 100M
我们可以查看mysql现有的包大小配置:
SHOW VARIABLES LIKE 'max_allowed_packet';


image.png
 [mysqld]
#设置3306端口
port = 3306

# 设置mysql的安装目录
basedir=D:/softwareback/db/mysql-5.7.26

# 设置mysql数据库的数据的存放目录
datadir=D:/softwareback/db/mysql-5.7.26/data


# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8

default-storage-engine=INNODB
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# 解决sonarscanner插件允许时候出错:ERROR: Failed to upload report - 500: An error has occurred. Please contact your administrator
max_allowed_packet= 100M

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8

1.1.7 查看扫描结果

image.png

1.2 Centeros环境下的安装和使用

1.2.1 大多数安装环境和windows差不多

。。。

2. 和Jenkins集成

2.1 Mysql和Sonarqube加入到PATH中

image.png

2.2 SonarScanner Jenkins插件的安装

注意SonarScanner仍然需要在本地环境现安装好


image.png

image.png

image.png

2.3 在Jenkins上某个task上增加这个步骤

2.3.1 直接在Jenkins上配置

image.png

接下来是这个步骤的配置:
image.png
sonar.projectKey=HGARCH1.0.01:project
sonar.projectName=HGARCH1.0.01.0project
sonar.projectVersion=1.0.01
sonar.sources=.
sonar.java.binaries=output
sonar.language=java
sonar.sourceEncoding=UTF-8

然后到sonar系统修改一个地方,否则在运行Jenkins的job时候会出错


image.png

然后重启sonar
image.png

然后运行,注意上一步配置如果不做可能会出错:


image.png

运行成功后可以在Jenkins看到sonar上相关工程执行结果链接:


image.png

image.png

2.3.2 在pipeline脚本里面配置

image.png
pipeline{
    agent any
     tools {
        //工具名称必须在Jenkins 管理Jenkins → 全局工具配置中预配置。
        maven 'maven3'
    }

    environment{     
        def SRCCODE_DIR = "/var/jenkins_home/workspace/${env.JOB_NAME}"
        def DAILYCI_DIR = "/var/ciOutput/dailyCiOutput"
        def SVN_FOLD = "personrecord"  
    }
    

    options { 
        //pipeline保持构建的最大个数
        buildDiscarder(logRotator(numToKeepStr: '28')) 
    }

    triggers { cron('H 0,12 * * *') }

    stages{     
        stage('Checkout'){
             steps{
    
                checkout([
                      $class: 'SubversionSCM', 
                      additionalCredentials: [], 
                      excludedCommitMessages: '', 
                      excludedRegions: '', 
                      excludedRevprop: '', 
                      excludedUsers: '', 
                      filterChangelog: false, 
                      ignoreDirPropChanges: false, 
                      includedRegions: '', 
                      locations: [[credentialsId: '6aab1014-b1c9-4a8a-adba-f1cd0d27a483', 
                                   depthOption: 'infinity', 
                                   ignoreExternalsOption: true,                                    
                                   remote: "https://10.xxx.xxx.xxx/svn/ZYN/xxx/branch/V1.0/code/personrecord"]], 
                      workspaceUpdater: [$class: 'UpdateUpdater']])
            }
        }

        stage('Build'){
            steps{
                compileAllFiles()                                          
            }           
        }

        stage('Backend Unit Test') {
            steps {
                sh 'mvn -f $SVN_FOLD/algorithm/pom.xml test'
                sh 'mvn -f $SVN_FOLD/pom.xml test'

                //junit '**/target/surefire-reports/TEST-*.xml'
                //archive 'target/*.jar'                    
            }
        }


        stage('SonarQube Analysis') {
            steps {
                withSonarQubeEnv('snoar-6.7.7') {
                    script {
                        def sonarScanner = tool name: 'sonarscanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
                        sh "${sonarScanner}/bin/sonar-scanner " +
                        "-Dsonar.projectKey=HGARCHMain:project " +
                        "-Dsonar.projectName=HGARCHMainProject " +
                        "-Dsonar.projectVersion=1 " +
                        "-Dsonar.sources=$SVN_FOLD " +
                        "-Dsonar.java.binaries=$SVN_FOLD/output " +                     
                        "-Dsonar.sourceEncoding=UTF-8"
                    }
                }
                //timeout(time: 15, unit: 'MINUTES') {
                //    script {
                //        def qg = waitForQualityGate()
                //        if (qg.status != 'OK') {
                            error "代码不满足Pipelin中Sonar设定的质量门限: ${qg.status}"
                //        }
                //    }
               // }             
            }
        }


        stage('UploadToRepository'){
            steps{                  
                    rmDailyCIOutputFiles() 
                    copyJarsToDailyCIOutoutDir()               
            }   
        }
        
        stage('copy to 118'){
            steps{              
            sshPublisher(publishers: [sshPublisherDesc(configName: 'fornanjing118', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'dailyOutput', remoteDirectorySDF: false, removePrefix: 'personrecord/output', sourceFiles: 'personrecord/output/**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }

        stage('Deploy to 117'){
            steps{
                //调用Publish Over SSH插件,上传docker-compose.yaml文件并且执行deploy脚本
            sshPublisher(publishers: [sshPublisherDesc(configName: 'forchongqing117', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'sh /root/hgarchDevops/redeploy.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'dailyOutput', remoteDirectorySDF: false, removePrefix: 'personrecord/output', sourceFiles: 'personrecord/output/**/*')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

            //sh /root/hgarchDevops/redeploy.sh
            //remoteDirectory: '/root/hgarchDevops/dailyOutput'
            //flatten: true会避免copy过来的jar包在personrecord/output中
            //personrecord/output/*.jar

            //. ./hgarchDevops/dailyOutput/redeploy.sh
            }
        }       
    }

    post {      
        failure
            {
                sh '$mail'
            }       
    }
}
def compileAllFiles()
{
    //执行shell命令
    sh 'mvn -f $SVN_FOLD/algorithm/pom.xml clean scala:compile compile package -DskipTests=true'
    sh 'mvn -f $SVN_FOLD/pom.xml clean package' 
}

def rmDailyCIOutputFiles()
{   
    sh 'rm -rf $DAILYCI_DIR.'
}
def copyJarsToDailyCIOutoutDir()
{
    if (isUnix())
    {
        sh 'cp -r $SVN_FOLD/output/* $DAILYCI_DIR'              
    }
    else
    {       
        bat 'cp -r $SVN_FOLD/output/* $DAILYCI_DIR'
    }   
}

def mail=
{
    emailext body: emailBody(),//"${env.DEFAULT_CONTENT}",
    recipientProviders: [ //[$class: 'CulpritsRecipientProvider'],
    //[$class: 'DevelopersRecipientProvider'],
    //[$class: 'RequesterRecipientProvider'],
    //[$class: 'FailingTestSuspectsRecipientProvider'],
    [$class: 'FirstFailingBuildSuspectsRecipientProvider'],
    [$class: 'UpstreamComitterRecipientProvider']
    ],
    subject: '构建失败',
    mimeType: "text/html"
}

def emailBody()
{
    return '''<!DOCTYPE html>    
    <html>    
    <head>    
    <meta charset="UTF-8">    
    <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>    
    </head>    
        
    <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"    
        offset="0">    
        <table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">    
            <tr>    
                本邮件由系统自动发出,无需回复!<br/>            
                各位同事,大家好,以下为${PROJECT_NAME }项目构建信息</br> 
                <td><font color="#CC0000">构建结果 - ${BUILD_STATUS}</font></td>   
            </tr>    
            <tr>    
                <td><br />    
                <b><font color="#0B610B">构建信息</font></b>    
                <hr size="2" width="100%" align="center" /></td>    
            </tr>    
            <tr>    
                <td>    
                    <ul>    
                        <li>项目名称 : ${PROJECT_NAME}</li>    
                        <li>构建编号 : 第${BUILD_NUMBER}次构建</li>    
                        <li>触发原因: ${CAUSE}</li>    
                        <li>构建状态: ${BUILD_STATUS}</li>    
                        <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>    
                        <li>构建  Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>    
                        <li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>    
                        <li>项目  Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>    
                    </ul>    

    <h4><font color="#0B610B">失败用例</font></h4>
    <hr size="2" width="100%" />
    $FAILED_TESTS<br/>

    <h4><font color="#0B610B">最近提交(#$SVN_REVISION)</font></h4>
    <hr size="2" width="100%" />
    <ul>
    ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"}
    </ul>
    详细提交: <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a><br/>

                </td>    
            </tr>    
        </table>    
    </body>    
    </html>'''
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,137评论 6 511
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,824评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,465评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,131评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,140评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,895评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,535评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,435评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,952评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,081评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,210评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,896评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,552评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,089评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,198评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,531评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,209评论 2 357