Analyzing with SonarQube Scanner for Jenkins
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 ์ด์์ ์ฌ์ฉํ์ฌ ๋ถ์ํด์ผ ํฉ๋๋ค
์ค์นํ๊ธฐ
- Jenkins ์ ๋ฐ์ดํธ ์ผํฐ์์ SonarQube Scanner for Jenkins ํ๋ฌ๊ทธ์ธ์ ์ค์นํฉ๋๋ค.
- SonarQube ์๋ฒ๋ฅผ ์๋์ ๊ฐ์ด ์ค์นํฉ๋๋ค:
- ๊ด๋ฆฌ์๋ก ๋ก๊ทธ์ธ ํ Manage Jenkins > Configure System ํญ๋ชฉ์ผ๋ก ์ด๋ํฉ๋๋ค.
- SonarQube configuration ์น์ ์์ Add SonarQube๋ฅผ ํด๋ฆญํ ํ, ํ์ํ ์ค์ ๊ฐ์ ์ ๋ ฅํฉ๋๋ค.
์ฌ์ฉํ๊ธฐ
SonarQube ์ค์บ๋๋ก ๋ถ์ํ๊ธฐ
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
- Job > Configure > Build ์น์ ์ผ๋ก ์ด๋ํฉ๋๋ค.
- ๋น๋ ์คํ ์ SonarQube Scanner๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- SonarQube ๋ถ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํฉ๋๋ค. ๊ธฐ์กด
sonar-project.properties
ํ์ผ์ ์ง์ ํ๊ฑฐ๋, Analysis properties ํ๋์ ๋ถ์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ง์ ์ ๋ ฅํ ์ ์์ต๋๋ค.
SonarQube Scanner for MSBuild๋ก ๋ถ์ํ๊ธฐ
Global Configuration
SonarQube Scanner for MSBuild๋ฅผ ์ฌ์ฉํด SonarQube ๋ถ์์ ์ํํ๋ ค๋ฉด, ์ด ๋จ๊ณ๋ฅผ ๋ฐ๋์ ์ํํด์ผ ํฉ๋๋ค. ์ํ๋ ๋งํผ์ ์ค์บ๋๋ฅผ ์ค์ ํ ๋ค, ๊ฐ๋ณ Jenkins job์์ ์ํ๋ ๋ฐ์ฒ๋ฅผ ์ ํํด SonarQube ๋ถ์์ ์ํํ ์ ์์ต๋๋ค.
Jenkins์ administrator ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ ํ ๋ค, Manage Jenkins > Global Tool Configuration ํ์ด์ง๋ก ์ด๋ํฉ๋๋ค.
Add SonarQube Scanner for MSBuild๋ฅผ ํด๋ฆญํฉ๋๋ค.
์ฌ์ฉ ๊ฐ๋ฅํ ์ค์น ๋ฒ์ ์ ์ถ๊ฐ์ฝ๋๋ค. Install automatically์ ์ฒดํฌํ๋ฉด Jenkins executor๊ฐ SonarQube Scanner for MSBuild์ ํด๋น ๋ฒ์ ์ ๋ค์ด๋ก๋ ํฉ๋๋ค. Install from GitHub์์ ์ฌ์ฉ๊ฐ๋ฅํ ๋ฒ์ ์ ํ์ธํ ์ ์๋ ๊ฒฝ์ฐ, Manage Jenkins > Manage Plugins > Advanced๋ก ์ด๋ํด Check now๋ฅผ ๋จผ์ ํด๋ฆญํ์ญ์์ค.
Job Configuration
- Job > Configure > Build ์น์ ์ผ๋ก ์ด๋ํฉ๋๋ค.
- ๋น๋ ์คํ ์ SonarQube for MSBuild - Begin Analysis ๋ฐ SonarQube for MSBuild - End Analysis๋ฅผ ์ถ๊ฐํฉ๋๋ค.
- SonarQube Scanner for MSBuild - Begin Analysis ์คํ ์์ SonarQube Project Key, Name ๋ฐ Version ์ ๋ณด๋ฅผ ์ ๋ ฅํฉ๋๋ค.
Begin Analysis์ End Analysis ์ฌ์ด์์ MSBuild ์คํ ํน์ MSBuild 14(ํธํ์ฑ ์ฐธ์กฐ)์ ๊ฒฝ์ฐ Execute Windows batch command ์คํ ์ ์ฌ์ฉํด ๋ถ์์ ์ํํ ์ ์์ต๋๋ค.
SonarQube Scanner for Maven์ผ๋ก ๋ถ์ํ๊ธฐ
Global Configuration
- Jenkins์ administrator ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธ ํ ๋ค, Manage Jenkins > Configure System ํ์ด์ง๋ก ์ด๋ํฉ๋๋ค.
- SonarQube servers ์น์ ์์ Enable injection of SonarQue server configuration as build environment variables ํญ๋ชฉ์ ์ฒดํฌํฉ๋๋ค
Job Configuration
- Job > Configure > Build Environment ์น์ ์ผ๋ก ์ด๋ํฉ๋๋ค.
Prepare SonarQube Scanner environment ์ต์ ์ ํ์ฑํํด์ SonarQube ์๋ฒ์ ํ๊ฒฝ ๋ณ์๋ฅผ ํด๋น Job์์ ํ์ฉํ ์ ์๋๋ก ํฉ๋๋ค. ๋ณต์์ SonarQube ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ์ฌ์ฉํ ์ธ์คํด์ค๋ฅผ ์ง์ ์ง์ ํ ์ ์์ต๋๋ค.
์ฌ์ฉ ๊ฐ๋ฅํ ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํ๋ ค๋ฉด help ๋ฒํผ์ ํด๋ฆญํฉ๋๋ค. SonarQube ์๋ฒ ์ค์ ์ ๋ฐ๋ผ ์ผ๋ถ ํ๊ฒฝ ๋ณ์๋ ๋น ๊ฐ์ ๊ฐ์ง๊ณ ์์ ์ ์์ต๋๋ค.
ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ฉด ์๋์ ๊ฐ์ด Maven goal ์ค์ ์ธ์๋ก ํ์ฉํ ์ ์์ต๋๋ค:
$SONAR_MAVEN_GOAL -Dsonar.host.url=$SONAR_HOST_URL
Post-build Action for Maven analysis ์คํ ์ ์ฌ์ฉํ ์ ์์ผ๋, ํด๋น ๊ธฐ๋ฅ์ ๋์ด์ ์ ์ง๋ณด์๋์ง ์์ต๋๋ค.
Jenkins pipeline์์ ๋ถ์ํ๊ธฐ
SonarQjube Scanner for Jenkins ํ๋ฌ๊ทธ์ธ v2.5๋ถํฐ ๊ณต์์ ์ผ๋ก Jenkins Pipeline์ ์ง์ํฉ๋๋ค. 'withSonarQubeEnv
' ๋ธ๋ก์ ์ฌ์ฉํด ์ฐ๋ํ SonarQube ์๋ฒ๋ฅผ ์ ํํ ์ ์์ต๋๋ค. Jenkins global configuration์ ์ ์ฅ๋ ์ฐ๊ฒฐ ์ค์ ์ ๋ณด๋ฅผ ์๋์ผ๋ก scanner์ ์ ๋ฌํฉ๋๋ค.
Pipeline์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ SonarQube v5.2 ์ด์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค,
์๋ ์ํ์ Unix ์ฌ๋ ์ด๋ธ๋ฅผ ์ฌ์ฉํ๊ณ , SonarQube ์๋ฒ์ ์ด๋ฆ์ 'My SonarQube Server'๋ผ๊ณ ์ค์ ํ ๊ฒฝ์ฐ์ ์์ ๋๋ค. Windows ์์คํ ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ 'sh'๋ฅผ '.bat' ์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค:
node { stage('SCM') { git 'https://github.com/foo/bar.git' } stage('SonarQube analysis') { // requires SonarQube Scanner 2.8+ def scannerHome = tool 'SonarQube Scanner 2.8'; withSonarQubeEnv('My SonarQube Server') { sh "${scannerHome}/bin/sonar-scanner" } } }
node { stage('SCM') { git 'https://github.com/foo/bar.git' } 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' } } }
node { stage('SCM') { git 'https://github.com/foo/bar.git' } 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' } } }
node { stage('SCM') { git 'https://github.com/foo/bar.git' } 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" } } }
ํ์ง ๊ฒ์ดํธ ๊ณ์ฐ์ด ์๋ฃ๋ ๋๊น์ง ํ์ดํ๋ผ์ธ ์ค๋จํ๊ธฐ
waitForQualityGate
๋จ๊ฒ๋ฅผ ์ฌ์ฉํด SaonrQube ์๋ฒ๊ฐ ๋ถ์์ ์๋ฃํ๊ณ ํ์ง ๊ฒ์ดํธ ์ํ๋ฅผ ๋ฐํํ ๋๊น์ง ํ์ดํ๋ผ์ธ์ ์ค๋จํ ์ ์์ต๋๋ค.
์ฌ์ ์ค๋น ์ฌํญ:
- SonarQube v6.2 ์ด์ ์ค์น (webhook ๊ธฐ๋ฅ ํ์)
- SonarQube ์๋ฒ๊ฐ
<your Jenkins instance>/sonarqube-webhook/
์ ๊ฐ๋ฆฌํค๋๋ก webhook์ ์ค์ ํฉ๋๋ค. SonarQube v6.2 ๋ฐ v6.3์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ ๊ฐ์ฅ ๋ง์ง๋ง์ ์ฌ๋์('/')๋ฅผ ๋ฐ๋์ ์ ๋ ฅํด์ผ ํฉ๋๋ค. - ํ์ดํ๋ผ์ธ์์
withSonarQubeEnv
์คํ ์ค์ (SionarQube taskId๊ฐ ํ์ดํ๋ผ์ธ ์ปจํ ์คํธ์ ์ ์์ ์ผ๋ก ํ์๋์ด์ผ ํจ)
์:
node { stage('SCM') { git 'https://github.com/foo/bar.git' } 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}" } } }
webhook์ ์ฌ์ฉํด ์ด ๋จ๊ณ๋ฅผ ๋งค์ฐ ๊ฐ๋จํ๊ฒ ์ค์ ํ ์ ์์ต๋๋ค. ๋ณ๋๋ก node๋ฅผ ์ง์ ํ๊ฑฐ๋, Jenkins๋ฅผ ์ฌ์์ํ์ง ์์๋ ๋ฉ๋๋ค(์ฌ์์ ํ์ ์คํ ์ด ๋ณต์๋ฉ๋๋ค).
Related content
ยฉ 2017-2018 Moses Kim.
๋ณ๋์ ์ธ๊ธ์ด ์๋ ํ, ์ด ์คํ์ด์ค์ ์ปจํ
์ธ ๋ ํฌ๋ฆฌ์์ดํฐ๋ธ ์ปค๋จผ์ฆ ์ ์์ํ์-๋น์๋ฆฌ-๋์ผ์กฐ๊ฑด๋ณ๊ฒฝํ๋ฝ 4.0 ๊ตญ์ ๋ผ์ด์ ์ค์ ๋ฐ๋ผ ์ด์ฉํ ์ ์์ต๋๋ค.
SONARQUBE๋ SonarSource SA์ ํธ๋ ์ด๋ ๋งํฌ์
๋๋ค. ๋ชจ๋ ํธ๋ ์ดํธ ๋งํฌ ๋ฐ ์ ์๊ถ์ ๊ฐ ์์ ์์ ์์ ๋ฌผ์
๋๋ค.
::: SonarQube ๊ด๋ จ ๋ฌธ์ : ์ด๋ฉ์ผ :::