SQ Official Doc Link: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins
By SonarSource – GNU LGPL 3 – Issue Tracker – Sources SonarQube Scanner for Jenkins 2.6.1 |
Table of Contents
기능
이 플러그인을 사용하면 Jenkins 글로벌 환경 설정에서 SonarQube 서버 환경을 설정할 수 있습니다.
이후 표준 Jenkins Build Step으로 SonarQube 분석을 다음 Scanner로 수행할 수 있습니다:
Jenkins의 job이 완료되면, 이 플러그인 SoanrQube 분석 여부를 식별하고 job 페이지에 SonarQube 배지를 표시합니다. 해당 배지를 클릭하면 SonarQube 대시보드로 이동해 quality gate 상태를 확인할 수 있습니다.
SINCE 2.5 : Jenkins Pipeline DSL 도 사용할 수 있습니다(일부 기능은 SonarQube v6.2 이상에서만 동작합니다).
호환성 매트릭스
SonarQube Scanner for Jenkins | 2.1 | 2.2.1 | 2.3 | 2.4 | 2.5 - 2.6 |
---|---|---|---|---|---|
Jenkins | 1.491+ | 1.580.1+ | 1.587+ | 1.587+ | 1.651+ |
SINCE 2.5 JRE8 이상을 사용하여 분석해야 합니다
설치하기
- Install the SonarQube Scanner for Jenkins via the Jenkins Update Center.
- Configure your SonarQube server(s)
- Log into Jenkins as an administrator and go to Manage Jenkins > Configure System:
- Scroll down to the SonarQube configuration section, click on Add SonarQube, and add the values you're prompted for.
사용하기
Analyzing with the SonarQube Scanner
Global Configuration
SonarQube Scanner를 사용해 SonarQube 분석을 수행하려면, 이 단계를 반드시 수행해야 합니다. 원하는 만큼의 스캐너를 설정한 뒤, 개별 Jenkins job에서 원하는 런처를 선택해 SonarQube 분석을 수행할 수 있습니다:
- Jenkins에 administrator 계정으로 로그인 한 뒤, Manage Jenkins > Global Tool Configuration 페이지로 이동합니다.
- SonarQube Scanner 설정 영역으로 이동한 뒤, Add SonarQube Scanner를 클릭합니다. 기본적으로 Jenkins 도구를 자동설치하도록 설정되어 있습니다. 이미 설치한 Scanner를 선택하거나('Install automatically' 체크 박스 해제) Jenkins가 특정 버전을 설치하도록 할 수 있습니다('Install automatically' 체크 박스 선택).
Jenkins가 업데이트 센터 파일을 다운로드하지 않은 경우(기본적으로 1일에 1회 다운로드)에는 사용 가능한 SonarQube Scanner 버전을 리스트에서 확인할 수 없을 수 있습니다. Manage Plugins > Advanced 탭에서 'Check Now' 버튼을 클릭해 강제로 업데이트 센터 파일을 다운로드 할 수 있습니다.
Job Configuration
- Configure the project, and scroll down to the Build section.
- Add the SonarQube Scanner build step to your build.
- Configure the SonarQube analysis properties. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the Analysis properties field
Analyzing with SonarQube Scanner for MSBuild
Global Configuration
This step is mandatory if you want to trigger any of your analyses with the SonarQube Scanner for MSBuild. You can define as many scanner instances as you wish. Then for each Jenkins job, you will be able to choose with which launcher to use to run the SonarQube analysis.
Log into Jenkins as an administrator and go to Manage Jenkins > Global Tool Configuration
Click on Add SonarQube Scanner for MSBuild
Add an installation of the latest available version. Check Install automatically to have the SonarQube Scanner for MSBuild automatically provisioned on your Jenkins executors
If you do not see any available version under Install from GitHub, first go to Manage Jenkins > Manage Plugins > Advanced and click on Check now
Job Configuration
- Configure the project, and scroll down to the Build section.
- Add both the SonarQube for MSBuild - Begin Analysis and SonarQube for MSBuild - End Analysis build steps to your build
- Configure the SonarQube Project Key, Name and Version in the SonarQube Scanner for MSBuild - Begin Analysis build step
Use the MSBuild build step or the Execute Windows batch command to execute the build with MSBuild 14 (see compatibility) between the Begin Analysis and End Analysis steps.
Analyzing with SonarQube Scanner for Maven
Global Configuration
- Log into Jenkins as an administrator and go to Manage Jenkins > Configure System
- Scroll to the SonarQube servers section and check Enable injection of SonarQube server configuration as build environment variables
Job Configuration
- Configure the project, and scroll down to the Build Environment section.
Enable Prepare SonarQube Scanner environment to allow the injection of SonarQube server values into this particular job. If multiple SonarQube instances are configured, you will be able to choose which one to use.
Press the help button to learn which variables you can use in your build. Some values may be blank, depending on what was defined for the server.
Once the environment variables are available, use them in a standard Maven build step by setting the Goals to include
$SONAR_MAVEN_GOAL -Dsonar.host.url=$SONAR_HOST_URL
Analyzing in a Jenkins pipeline
Since version 2.5 of the SonarQube Scanner for Jenkins, there is an official support of Jenkins pipeline. We provide a 'withSonarQubeEnv
' block that allow to select the SonarQube server you want to interact with. Connection details you have configured in Jenkins global configuration will be automatically passed to the scanner.
Support of pipeline only works with SonarQube >= 5.2.
Here are a some examples for every scanner, assuming you run on Unix slaves and you have configured a server named 'My SonarQube Server' as well as required tools. If you run on Windows slaves, just replace 'sh' by 'bat'.
SonarQube Scanner
node { stage( 'SCM' ) { } stage( 'SonarQube analysis' ) { // requires SonarQube Scanner 2.8+ def scannerHome = tool 'SonarQube Scanner 2.8' ; withSonarQubeEnv( 'My SonarQube Server' ) { sh "${scannerHome}/bin/sonar-scanner" } } } |
SonarQube Scanner for Gradle
node { stage( 'SCM' ) { } stage( 'SonarQube analysis' ) { withSonarQubeEnv( 'My SonarQube Server' ) { // requires SonarQube Scanner for Gradle 2.1+ // It's important to add --info because of SONARJNKNS-281 sh './gradlew --info sonarqube' } } } |
SonarQube Scanner for Maven
node { stage( 'SCM' ) { } stage( 'SonarQube analysis' ) { withSonarQubeEnv( 'My SonarQube Server' ) { // requires SonarQube Scanner for Maven 3.2+ sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar' } } } |
SonarQube Scanner for MSBuild
node { stage( 'SCM' ) { } stage( 'Build + SonarQube analysis' ) { def sqScannerMsBuildHome = tool 'Scanner for MSBuild 2.2' withSonarQubeEnv( 'My SonarQube Server' ) { // Due to SONARMSBRU-307 value of sonar.host.url and credentials should be passed on command line bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe begin /k:myKey /n:myName /v:1.0 /d:sonar.host.url=%SONAR_HOST_URL% /d:sonar.login=%SONAR_AUTH_TOKEN%" bat 'MSBuild.exe /t:Rebuild' bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe end" } } } |
Pause pipeline until quality gate is computed
The waitForQualityGate
step will pause the pipeline until SonarQube analysis is completed and returns quality gate status.
Pre-requisites:
- SonarQube server 6.2+ (need webhook feature)
- Configure a webhook in your SonarQube server pointing to
<your Jenkins instance>/sonarqube-webhook/
The trailing slash is mandatory with SonarQube 6.2 and 6.3! Use
withSonarQubeEnv
step in your pipeline (so that SonarQube taskId is correctly attached to the pipeline context).
Example:
SonarQube Scanner for MSBuild
node { stage( 'SCM' ) { } stage( 'SonarQube analysis' ) { withSonarQubeEnv( 'My SonarQube Server' ) { sh 'mvn clean package sonar:sonar' } // SonarQube taskId is automatically attached to the pipeline context } } // No need to occupy a node stage( "Quality Gate" ){ timeout(time: 1 , unit: 'HOURS' ) { // Just in case something goes wrong, pipeline will be killed after a timeout def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv if (qg.status != 'OK' ) { error "Pipeline aborted due to quality gate failure: ${qg.status}" } } } |
Thanks to the webhook, the step is implemented in a very lightweight way: no need to occupy a node, and it doesn't prevent Jenkins to restart (step will be restored after restart).
0 Comments