mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-04 04:07:16 +02:00
refactor(sonar): reuse piperExecuteBin code (#1880)
* reuse piperExecuteBin code * add correlation id * reuse credential loading * reuse dockerWrapper * rearrange code * add influx wrapper * correct writeToDisk position
This commit is contained in:
parent
1fe94680df
commit
896519a77f
@ -79,12 +79,14 @@ void call(Map parameters = [:], String stepName, String metadataFile, List crede
|
||||
}
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
static void prepareExecution(Script script, Utils utils, Map parameters = [:]) {
|
||||
def piperGoUtils = parameters.piperGoUtils ?: new PiperGoUtils(script, utils)
|
||||
piperGoUtils.unstashPiperBin()
|
||||
utils.unstash('pipelineConfigAndTests')
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
static Map prepareStepParameters(Map parameters) {
|
||||
Map stepParameters = [:].plus(parameters)
|
||||
|
||||
@ -99,10 +101,12 @@ static Map prepareStepParameters(Map parameters) {
|
||||
return MapUtils.pruneNulls(stepParameters)
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
static void prepareMetadataResource(Script script, String metadataFile) {
|
||||
script.writeFile(file: ".pipeline/tmp/${metadataFile}", text: script.libraryResource(metadataFile))
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
static Map getStepContextConfig(Script script, String piperGoPath, String metadataFile, String defaultConfigArgs, String customConfigArg) {
|
||||
return script.readJSON(text: script.sh(returnStdout: true, script: "${piperGoPath} getConfig --contextConfig --stepMetadata '.pipeline/tmp/${metadataFile}'${defaultConfigArgs}${customConfigArg}"))
|
||||
}
|
||||
@ -117,6 +121,7 @@ static String getCustomDefaultConfigs() {
|
||||
return customDefaults.join(',')
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
static String getCustomDefaultConfigsArg() {
|
||||
String customDefaults = getCustomDefaultConfigs()
|
||||
if (customDefaults) {
|
||||
@ -125,6 +130,7 @@ static String getCustomDefaultConfigsArg() {
|
||||
return ''
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
static String getCustomConfigArg(def script) {
|
||||
if (script?.commonPipelineEnvironment?.configurationFile
|
||||
&& script.commonPipelineEnvironment.configurationFile != '.pipeline/config.yml'
|
||||
@ -134,6 +140,7 @@ static String getCustomConfigArg(def script) {
|
||||
return ''
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
void dockerWrapper(script, config, body) {
|
||||
if (config.dockerImage) {
|
||||
Map dockerExecuteParameters = [:].plus(config)
|
||||
@ -146,6 +153,7 @@ void dockerWrapper(script, config, body) {
|
||||
}
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
void credentialWrapper(config, List credentialInfo, body) {
|
||||
if (credentialInfo.size() > 0) {
|
||||
def creds = []
|
||||
@ -185,6 +193,7 @@ void credentialWrapper(config, List credentialInfo, body) {
|
||||
}
|
||||
}
|
||||
|
||||
// reused in sonarExecuteScan
|
||||
void handleErrorDetails(String stepName, Closure body) {
|
||||
try {
|
||||
body()
|
||||
|
@ -11,47 +11,44 @@ import java.nio.charset.StandardCharsets
|
||||
|
||||
void call(Map parameters = [:]) {
|
||||
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
|
||||
def stepParameters = [:].plus(parameters)
|
||||
|
||||
def script = checkScript(this, parameters) ?: this
|
||||
stepParameters.remove('script')
|
||||
|
||||
def utils = parameters.juStabUtils ?: new Utils()
|
||||
stepParameters.remove('juStabUtils')
|
||||
|
||||
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
|
||||
stepParameters.remove('jenkinsUtilsStub')
|
||||
String piperGoPath = parameters.piperGoPath ?: './piper'
|
||||
|
||||
new PiperGoUtils(this, utils).unstashPiperBin()
|
||||
utils.unstash('pipelineConfigAndTests')
|
||||
script.commonPipelineEnvironment.writeToDisk(script)
|
||||
piperExecuteBin.prepareExecution(this, utils, parameters)
|
||||
piperExecuteBin.prepareMetadataResource(script, METADATA_FILE)
|
||||
Map stepParameters = piperExecuteBin.prepareStepParameters(parameters)
|
||||
|
||||
writeFile(file: ".pipeline/tmp/${METADATA_FILE}", text: libraryResource(METADATA_FILE))
|
||||
List credentialInfo = [
|
||||
[type: 'token', id: 'sonarTokenCredentialsId', env: ['PIPER_token']],
|
||||
[type: 'token', id: 'githubTokenCredentialsId', env: ['PIPER_githubToken']],
|
||||
]
|
||||
|
||||
withEnv([
|
||||
"PIPER_parametersJSON=${groovy.json.JsonOutput.toJson(stepParameters)}",
|
||||
"PIPER_correlationID=${env.BUILD_URL}",
|
||||
]) {
|
||||
String customDefaultConfig = piperExecuteBin.getCustomDefaultConfigsArg()
|
||||
String customConfigArg = piperExecuteBin.getCustomConfigArg(script)
|
||||
// get context configuration
|
||||
Map config = readJSON(text: sh(returnStdout: true, script: "./piper getConfig --contextConfig --stepMetadata '.pipeline/tmp/${METADATA_FILE}'${customDefaultConfig}${customConfigArg}"))
|
||||
echo "Config: ${config}"
|
||||
Map config
|
||||
piperExecuteBin.handleErrorDetails(STEP_NAME) {
|
||||
config = piperExecuteBin.getStepContextConfig(script, piperGoPath, METADATA_FILE, customDefaultConfig, customConfigArg)
|
||||
echo "Context Config: ${config}"
|
||||
}
|
||||
// get step configuration to access `instance` & `customTlsCertificateLinks` & `owner` & `repository` & `legacyPRHandling`
|
||||
Map stepConfig = readJSON(text: sh(returnStdout: true, script: "./piper getConfig --stepMetadata '.pipeline/tmp/${METADATA_FILE}'${customDefaultConfig}${customConfigArg}"))
|
||||
echo "StepConfig: ${stepConfig}"
|
||||
// writeToDisk needs to be called here as owner and repository may come from the pipeline environment
|
||||
script.commonPipelineEnvironment.writeToDisk(script)
|
||||
Map stepConfig = readJSON(text: sh(returnStdout: true, script: "${piperGoPath} getConfig --stepMetadata '.pipeline/tmp/${METADATA_FILE}'${customDefaultConfig}${customConfigArg}"))
|
||||
echo "Step Config: ${stepConfig}"
|
||||
|
||||
// determine credentials to load
|
||||
List credentials = []
|
||||
List environment = []
|
||||
if (config.sonarTokenCredentialsId)
|
||||
credentials.add(string(credentialsId: config.sonarTokenCredentialsId, variable: 'PIPER_token'))
|
||||
if(isPullRequest()){
|
||||
checkMandatoryParameter(stepConfig, "owner")
|
||||
checkMandatoryParameter(stepConfig, "repository")
|
||||
if(stepConfig.legacyPRHandling) {
|
||||
checkMandatoryParameter(config, "githubTokenCredentialsId")
|
||||
if (config.githubTokenCredentialsId)
|
||||
credentials.add(string(credentialsId: config.githubTokenCredentialsId, variable: 'PIPER_githubToken'))
|
||||
}
|
||||
environment.add("PIPER_changeId=${env.CHANGE_ID}")
|
||||
environment.add("PIPER_changeBranch=${env.CHANGE_BRANCH}")
|
||||
@ -61,26 +58,20 @@ void call(Map parameters = [:]) {
|
||||
// load certificates into cacerts file
|
||||
loadCertificates(customTlsCertificateLinks: stepConfig.customTlsCertificateLinks, verbose: stepConfig.verbose)
|
||||
// execute step
|
||||
dockerExecute(
|
||||
script: script,
|
||||
dockerImage: config.dockerImage,
|
||||
dockerWorkspace: config.dockerWorkspace,
|
||||
dockerOptions: config.dockerOptions
|
||||
) {
|
||||
piperExecuteBin.dockerWrapper(script, config){
|
||||
if(!fileExists('.git')) utils.unstash('git')
|
||||
piperExecuteBin.handleErrorDetails(STEP_NAME) {
|
||||
withSonarQubeEnv(stepConfig.instance) {
|
||||
withCredentials(credentials) {
|
||||
withEnv(environment){
|
||||
try {
|
||||
sh "./piper ${STEP_NAME}${customDefaultConfig}${customConfigArg}"
|
||||
} finally {
|
||||
InfluxData.readFromDisk(script)
|
||||
withEnv(environment){
|
||||
influxWrapper(script){
|
||||
piperExecuteBin.credentialWrapper(config, credentialInfo){
|
||||
sh "${piperGoPath} ${STEP_NAME}${customDefaultConfig}${customConfigArg}"
|
||||
}
|
||||
jenkinsUtils.handleStepResults(STEP_NAME, false, false)
|
||||
script.commonPipelineEnvironment.readFromDisk(script)
|
||||
}
|
||||
}
|
||||
}
|
||||
jenkinsUtils.handleStepResults(STEP_NAME, false, false)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@ -90,6 +81,14 @@ void call(Map parameters = [:]) {
|
||||
}
|
||||
}
|
||||
|
||||
private void influxWrapper(Script script, body){
|
||||
try {
|
||||
body()
|
||||
} finally {
|
||||
InfluxData.readFromDisk(script)
|
||||
}
|
||||
}
|
||||
|
||||
private void checkMandatoryParameter(config, key){
|
||||
if (!config[key]) {
|
||||
throw new IllegalArgumentException( "ERROR - NO VALUE AVAILABLE FOR ${key}")
|
||||
|
Loading…
Reference in New Issue
Block a user