Sonarqube(社区版)支持扫描多分支配置

发布时间 2023-09-04 10:15:59作者: tomoncle

Sonarqube(社区版)支持扫描多分支配置

社区版本的sonarqube不支持扫描多分支,可以利用开源插件实现

GitHub:https://github.com/mc1arke/sonarqube-community-branch-plugin


一.Sonarqube配置

1.下载扫描插件到 /opt/sonarqube/extensions/plugins/

$ wget https://github.com/mc1arke/sonarqube-community-branch-plugin/releases/download/1.14.0/sonarqube-community-branch-plugin-1.14.0.jar -P /opt/sonarqube/extensions/plugins/

2.配置sonarqube配置文件:/opt/sonarqube/conf/sonar.properties 添加以下内容

sonar.web.javaAdditionalOpts=-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-1.14.0.jar=web
sonar.ce.javaAdditionalOpts=-javaagent:./extensions/plugins/sonarqube-community-branch-plugin-1.14.0.jar=ce

3.重启soanrqube

二.Jenkins配置

1.安装多分支扫描插件:GitLab Branch Source Plugin

2.配置pipline流水线:配置选项 指定分支(为空时代表any)= */$gitlabSourceBranch

三.配置Jenkinsfile

配置Jenkinsfile在使用Sonarqube时,添加参数 -Dsonar.branch.name=${gitlabSourceBranch}

Python

stage('SonarQube Analysis') {
    environment {
        // http://192.168.61.33:8080/manage/configureTools/
        // Set the scannerHome variable to the SonarScanner tool path
        // 在全局工具配置中配置 SonarScanner, 变量值就是配置的 SonarQube Scanner Name
        scannerHome = tool 'SonarScanner'
    }
    steps {
        // http://192.168.61.33:8080/manage/configure
        // withSonarQubeEnv('sonarqube') 表示在SonarQube环境中运行
        // 在系统管理-系统配置中配置 SonarQube servers, 
        // 1. 选中 environment variables
        // 2. Nmae 输入 sonarqube,表示SonarQube服务器的名称
        withSonarQubeEnv('sonarqube'){
            // 添加参数 -Dsonar.branch.name=${gitlabSourceBranch}
            sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${gitlabSourceBranch}"
        }
    }
}

Java

stage('SonarQube Analysis') {
    environment {
        // 在全局工具配置中配置 maven, 变量值就是配置的 Maven Name
        // Set the maven variable to the maven tool path
        mvn = tool 'maven'
    }
    steps {
        // http://192.168.61.33:8080/manage/configure
        // withSonarQubeEnv('sonarqube') 表示在SonarQube环境中运行
        // 在系统管理-系统配置中配置 SonarQube servers
        // 1. 选中 environment variables
        // 2. Nmae 输入 sonarqube,表示SonarQube服务器的名称
        withSonarQubeEnv('sonarqube'){
            // sh "${mvn}/bin/mvn findbugs:findbugs"
            sh "${mvn}/bin/mvn clean verify sonar:sonar -Dsonar.projectKey=sonar_mxnet-spring-samples_AYnjWl7-6upJ4s3JUirg" -Dsonar.branch.name=${gitlabSourceBranch}"
        }
    }
}