Jenkins集成SonarScan

1.SonarScan安装

1)进入目录local

# cd /usr/local

2)使用wget下载sonar-scanner

# wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.4.0.2170-linux.zip
# unzip sonar-scanner-cli-4.4.0.2170-linux.zip

3)编辑/etc/profile设置环境变量

# vi /etc/profile
export SONAR_HOME=/usr/local/sonar-scanner-4.4.0.2170-linux
export $PATH:$SONAR_HOME/bin

# source /etc/profile

2.SoarScan扫描项目

注:需在和src同级目录下执行

# cd /var/lib/jenkins/workspace/demo-mavn-service_PUSH
# sonar-scanner -Dsonar.host.url=http://192.168.164.158:9000 \   #sonarqube地址
-Dsonar.projectKey=demo-maven-service \
-Dsonar.projectName=demo-maven-service \
-Dsonar.projectVersion=1.0 \
-Dsonar.login=admin \ # sonarqube用户名
-Dsonar.password=admin \ # sonarqube密码
-Dsonar.ws.timeout=30 \
-Dsonar.projectDescription="my first project!" \
-Dsonar.links.homepage=http://www.baidu.com \
-Dsonar.sources=src \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=target/classes \
-Dsonar.java.test.binaries=target/test-classes \
-Dsonar.java.surefire.report=target/surefire-reports

 扫描结果展示

 

3.Jenkins流水线自动扫描

1)安装SonarQube Scanner插件

2)sonarqube.groovy

package org.devops


//scan
def SonarScan(projectName,projectDesc,projectPath){
    
    def scannerHome = "/usr/local/sonar-scanner-4.4.0.2170-linux/"
    def sonarServer = "http://192.168.164.158:9000"
    def sonarDate = sh  returnStdout: true, script: 'date  +%Y%m%d%H%M%S'
    sonarDate = sonarDate - "\n"

    
    sh """ 
        ${scannerHome}/bin/sonar-scanner -Dsonar.host.url=${sonarServer} \
        -Dsonar.projectKey=${projectName} \
        -Dsonar.projectName=${projectName} \
        -Dsonar.projectVersion=${sonarDate} \
        -Dsonar.login=admin \
        -Dsonar.password=admin \
        -Dsonar.ws.timeout=30 \
        -Dsonar.projectDescription=${projectDesc} \
        -Dsonar.links.homepage=http://www.baidu.com \
        -Dsonar.sources=${projectPath} \
        -Dsonar.sourceEncoding=UTF-8 \
        -Dsonar.java.binaries=target/classes \
        -Dsonar.java.test.binaries=target/test-classes \
        -Dsonar.java.surefire.report=target/surefire-reports

       """
}

 

3)jenkinsfile调用

def sonar = new org.devops.sonarqube()

stage("SonarScan"){
            steps{
                script{
                   tools.PrintMes("代码扫描","green") 
                   sonar.SonarScan("$JOB_NAME","$JOB_NAME","src")
                   
                }
            }
            
    }

 

4)结果展示

 

4.Jenkins集成Sonarqube Scanner

1)SonarQube生成秘钥

 

 

2)jenkins添加SonarQube秘钥凭证(secreat text类型)

 

 3)系统配置,添加sonarqube服务信息

 

4)构建验证

 

posted @ 2022-10-17 10:38  杰宏唯一  阅读(383)  评论(0编辑  收藏  举报