環境準備
- 安裝 SonarQube Server
- jenknins plugin: SonarQube Scanner
SonarQube
Generate SonarQube Token
My Account -> Security -> Generate Tokens
jenkins 設定
環境
- Manage Jenkins > Manage Credentials
- manage > system > SonarQube servers
- manage > system Tools > SonarQube Scanner
專案
pipline
- 已下為 grails 專案設定
node {
env.SONAR_USER_HOME="${JENKINS_HOME}"
env.JAVA_HOME="${tool 'openjdk-17'}"
env.GRAILS_HOME="/usr/local/lib/grails/grails-5.3.3"
env.PATH="${env.JAVA_HOME}/bin:${env.GRAILS_HOME}/bin:${env.PATH}"
stage ('Checkout') {
git branch: 'master',
credentialsId: 'credentialsId',
url: "<git url>"
}
stage ('Build') {
sh "grails test war ${env.WAR_NAME}.war"
}
stage('Code Quality Check via SonarQube'){
script {
def scannerHome = tool 'sonarqube';
withSonarQubeEnv("sonarqube-server") {
sh "${tool("sonarqube")}/bin/sonar-scanner \
-Dsonar.projectKey=<projectKey> \
-Dsonar.projectVersion=<projectVersion> \
-Dsonar.projectBaseDir=${WORKSPACE} \
-Dsonar.sources=grails-app,src/main \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.language=grvy,java \
-Dsonar.java.binaries=build \
-Dsonar.lang.patterns.grvy='**/*.groovy' \
-Dsonar.lang.patterns.java='**/*.java' \
-Dsonar.groovy.binaries=build \
-Dsonar.exclusions='**/*.xml'"
}
}
}
}