在你搭建好 SonarQube 实例后,需要将其与项目集成。
由于我使用的是 Community Edition,它不支持 C/C++ 项目,所以这里只演示 Maven、Gradle 及其他类型项目的集成方法。
假设在 SonarQube 中的项目名称和 ID 都为 test-demo
,并通过 Jenkins 进行构建。
Maven 项目集成#
在
pom.xml
中添加:<properties> <sonar.projectKey>test-demo</sonar.projectKey> </properties>
在
Jenkinsfile
中添加:stage('SonarQube Analysis') { def mvn = tool 'Default Maven' withSonarQubeEnv() { sh "${mvn}/bin/mvn sonar:sonar" } }
Gradle 项目集成#
在
build.gradle
中添加:plugins { id "org.sonarqube" version "3.3" } sonarqube { properties { property "sonar.projectKey", "test-demo" } }
在
Jenkinsfile
中添加:stage('SonarQube Analysis') { withSonarQubeEnv() { sh "./gradlew sonarqube" } }
其他类型项目(JS、TS、Python 等)#
在代码仓库根目录创建
sonar-project.properties
文件:sonar.projectKey=test-demo
在
Jenkinsfile
中添加:stage('SonarQube Analysis') { def scannerHome = tool 'SonarScanner' withSonarQubeEnv() { sh "${scannerHome}/bin/sonar-scanner" } }
更多关于 SonarQube 集成的方法,可以访问你本地的 SonarQube 文档页面:
http://localhost:9000/documentation
转载本文请注明作者与出处,禁止商业用途。欢迎关注公众号「DevOps攻城狮」。