1
0
mirror of https://github.com/firstBitMarksistskaya/jenkins-lib.git synced 2025-03-17 21:17:50 +02:00

Попытка выноса sonar-scanner в отдельный класс

This commit is contained in:
Nikita Gryzlov 2020-04-15 18:23:59 +03:00
parent f65a2b2d59
commit 2d29485f7c
No known key found for this signature in database
GPG Key ID: C1EAE411FEF0BF2F
3 changed files with 62 additions and 28 deletions

View File

@ -0,0 +1,55 @@
package ru.pulsar.jenkins.library.steps
import ru.pulsar.jenkins.library.IStepExecutor
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.utils.VersionParser
class SonarScanner implements Serializable {
private final JobConfiguration config;
private final String rootFile
SonarScanner(JobConfiguration config, String rootFile = 'src/cf/Configuration.xml') {
this.config = config
this.rootFile = rootFile
}
def run() {
IStepExecutor steps = ContextRegistry.getContext().getStepExecutor()
Logger.printLocation()
if (!config.sonarQubeOptions.enabled) {
steps.echo("SonarQube step is disabled")
return
}
def env = System.getenv();
def sonarScannerBinary
if (config.sonarQubeOptions.useSonarScannerFromPath) {
sonarScannerBinary = "sonar-scanner"
} else {
String scannerHome = steps.tool(config.sonarQubeOptions.sonarScannerToolName)
sonarScannerBinary = "$scannerHome/bin/sonar-scanner"
}
def sonarCommand = "$sonarScannerBinary -Dsonar.branch.name=$env.BRANCH_NAME"
String configurationVersion = VersionParser.configuration(rootFile)
if (configurationVersion) {
sonarCommand += " -Dsonar.projectVersion=$configurationVersion"
}
def sonarQubeInstallation = config.sonarQubeOptions.sonarQubeInstallation
if (sonarQubeInstallation == '') {
sonarQubeInstallation = null
}
steps.withSonarQubeEnv(sonarQubeInstallation) {
steps.cmd(sonarCommand)
}
}
}

View File

@ -40,9 +40,7 @@ void call() {
label 'sonar'
}
steps {
printLocation()
sonarScanner()
sonarScanner config
}
}

View File

@ -1,29 +1,10 @@
import com.cloudbees.groovy.cps.NonCPS
import groovy.transform.Field
import ru.pulsar.jenkins.library.configuration.JobConfiguration
import ru.pulsar.jenkins.library.utils.VersionParser
import ru.pulsar.jenkins.library.ioc.ContextRegistry
import ru.pulsar.jenkins.library.steps.SonarScanner
@Field def sonarCommand
def call(JobConfiguration config, String rootFile = 'src/cf/Configuration.xml') {
ContextRegistry.registerDefaultContext(this)
def call(String rootFile = 'src/cf/Configuration.xml') {
def config = jobConfiguration() as JobConfiguration
String scannerHome = tool config.sonarScannerToolName
sonarCommand = "$scannerHome/bin/sonar-scanner -Dsonar.branch.name=$env.BRANCH_NAME"
String configurationVersion = VersionParser.configuration(rootFile)
if (configurationVersion) {
sonarCommand += " -Dsonar.projectVersion=$configurationVersion"
}
withSonarQubeEnv('qa.dev.pulsar.ru') {
cmd sonarCommand
}
def sonarScanner = new SonarScanner(config, rootFile)
sonarScanner.run()
}
@NonCPS
private static String version(String text) {
def matcher = text =~ /<Version>(.*)<\/Version>/
return matcher ? matcher.group(1) : ""
}