2020-04-08 12:55:46 +02:00
|
|
|
import com.sap.piper.JenkinsUtils
|
|
|
|
import com.sap.piper.PiperGoUtils
|
2019-05-13 12:29:07 +02:00
|
|
|
import com.sap.piper.Utils
|
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
import groovy.transform.Field
|
2019-08-11 22:42:34 +02:00
|
|
|
import java.nio.charset.StandardCharsets
|
|
|
|
|
2019-05-13 12:29:07 +02:00
|
|
|
@Field String STEP_NAME = getClass().getName()
|
2020-04-08 12:55:46 +02:00
|
|
|
@Field String METADATA_FILE = 'metadata/sonar.yaml'
|
2019-05-13 12:29:07 +02:00
|
|
|
|
|
|
|
void call(Map parameters = [:]) {
|
|
|
|
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
|
2020-04-08 12:55:46 +02:00
|
|
|
def stepParameters = [:].plus(parameters)
|
2019-05-13 12:29:07 +02:00
|
|
|
|
2020-04-08 12:55:46 +02:00
|
|
|
def script = checkScript(this, parameters) ?: this
|
|
|
|
stepParameters.remove('script')
|
2019-05-13 12:29:07 +02:00
|
|
|
|
2020-04-08 12:55:46 +02:00
|
|
|
def utils = parameters.juStabUtils ?: new Utils()
|
|
|
|
stepParameters.remove('juStabUtils')
|
|
|
|
|
|
|
|
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
|
|
|
|
stepParameters.remove('jenkinsUtilsStub')
|
|
|
|
|
|
|
|
new PiperGoUtils(this, utils).unstashPiperBin()
|
|
|
|
utils.unstash('pipelineConfigAndTests')
|
|
|
|
script.commonPipelineEnvironment.writeToDisk(script)
|
|
|
|
|
|
|
|
writeFile(file: ".pipeline/tmp/${METADATA_FILE}", text: libraryResource(METADATA_FILE))
|
|
|
|
|
|
|
|
withEnv([
|
|
|
|
"PIPER_parametersJSON=${groovy.json.JsonOutput.toJson(stepParameters)}",
|
|
|
|
]) {
|
|
|
|
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}"
|
|
|
|
// 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}"
|
|
|
|
|
|
|
|
// 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'))
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
2020-04-08 12:55:46 +02:00
|
|
|
environment.add("PIPER_changeId=${env.CHANGE_ID}")
|
|
|
|
environment.add("PIPER_changeBranch=${env.CHANGE_BRANCH}")
|
|
|
|
environment.add("PIPER_changeTarget=${env.CHANGE_TARGET }")
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
2020-04-08 12:55:46 +02:00
|
|
|
// 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
|
|
|
|
) {
|
|
|
|
if(!fileExists('.git')) utils.unstash('git')
|
|
|
|
withSonarQubeEnv(stepConfig.instance) {
|
|
|
|
withCredentials(credentials) {
|
|
|
|
withEnv(environment){
|
|
|
|
sh "./piper ${STEP_NAME}${customDefaultConfig}${customConfigArg}"
|
|
|
|
}
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 12:55:46 +02:00
|
|
|
jenkinsUtils.handleStepResults(STEP_NAME, false, false)
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 12:55:46 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-13 12:29:07 +02:00
|
|
|
|
2020-04-08 12:55:46 +02:00
|
|
|
private void checkMandatoryParameter(config, key){
|
|
|
|
if (!config[key]) {
|
|
|
|
throw new IllegalArgumentException( "ERROR - NO VALUE AVAILABLE FOR ${key}")
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Boolean isPullRequest(){
|
|
|
|
return env.CHANGE_ID
|
|
|
|
}
|
|
|
|
|
2019-08-11 22:42:34 +02:00
|
|
|
private void loadCertificates(Map config) {
|
|
|
|
String certificateFolder = '.certificates/'
|
|
|
|
List wgetOptions = [
|
|
|
|
"--directory-prefix ${certificateFolder}"
|
|
|
|
]
|
|
|
|
List keytoolOptions = [
|
|
|
|
'-import',
|
|
|
|
'-noprompt',
|
|
|
|
'-storepass changeit',
|
2019-12-20 11:51:33 +02:00
|
|
|
"-keystore ${certificateFolder}cacerts"
|
2019-08-11 22:42:34 +02:00
|
|
|
]
|
|
|
|
if (config.customTlsCertificateLinks){
|
|
|
|
if(config.verbose){
|
|
|
|
wgetOptions.push('--verbose')
|
|
|
|
keytoolOptions.push('-v')
|
|
|
|
}else{
|
|
|
|
wgetOptions.push('--no-verbose')
|
|
|
|
}
|
|
|
|
config.customTlsCertificateLinks.each { url ->
|
|
|
|
def filename = new File(url).getName()
|
|
|
|
filename = URLDecoder.decode(filename, StandardCharsets.UTF_8.name())
|
|
|
|
sh "wget ${wgetOptions.join(' ')} ${url}"
|
|
|
|
sh "keytool ${keytoolOptions.join(' ')} -alias '${filename}' -file '${certificateFolder}${filename}'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|