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

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

注意本文档 SonarQube 版本 9.9.1.69595

社区版本的 SonarQube 不支持扫描多分支,可以利用开源插件实现。插件的主要版本和次要版本与兼容的 SonarQube 版本匹配,例如,插件的 25.4.0 与 SonarQube 25.4.x 兼容。不保证任何较旧的插件版本都能正常工作,也不保证较新的 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}"
        }
    }
}
posted @ 2023-09-04 10:06  tomoncle  阅读(1582)  评论(0)    收藏  举报