2018-09-21 16:55:31 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
|
2018-08-15 11:53:28 +02:00
|
|
|
import com.sap.piper.ConfigurationHelper
|
2018-01-24 10:55:38 +02:00
|
|
|
import com.sap.piper.JsonUtils
|
2018-08-15 11:53:28 +02:00
|
|
|
import com.sap.piper.Utils
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-08-15 11:53:28 +02:00
|
|
|
import groovy.transform.Field
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-11-29 10:54:05 +02:00
|
|
|
@Field def STEP_NAME = getClass().getName()
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-08-15 11:53:28 +02:00
|
|
|
@Field Set GENERAL_CONFIG_KEYS = []
|
2019-01-17 16:42:03 +02:00
|
|
|
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
|
|
|
|
'artifactVersion',
|
|
|
|
'customData',
|
|
|
|
'customDataTags',
|
|
|
|
'customDataMap',
|
|
|
|
'customDataMapTags',
|
2018-08-15 11:53:28 +02:00
|
|
|
'influxServer',
|
2018-12-12 18:33:36 +02:00
|
|
|
'influxPrefix',
|
|
|
|
'wrapInNode'
|
2018-08-15 11:53:28 +02:00
|
|
|
])
|
2019-01-17 16:42:03 +02:00
|
|
|
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-08-30 16:33:07 +02:00
|
|
|
void call(Map parameters = [:]) {
|
2018-08-15 11:53:28 +02:00
|
|
|
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters, allowBuildFailure: true) {
|
2018-09-21 16:55:31 +02:00
|
|
|
|
|
|
|
def script = checkScript(this, parameters)
|
2018-01-24 10:55:38 +02:00
|
|
|
if (script == null)
|
2018-11-05 13:26:48 +02:00
|
|
|
script = this
|
2018-08-15 11:53:28 +02:00
|
|
|
|
|
|
|
// load default & individual configuration
|
2018-12-12 18:33:36 +02:00
|
|
|
Map config = ConfigurationHelper.newInstance(this)
|
2018-09-07 10:08:16 +02:00
|
|
|
.loadStepDefaults()
|
2018-08-15 11:53:28 +02:00
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
|
|
|
|
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
|
|
|
|
.mixinStageConfig(script.commonPipelineEnvironment, parameters.stageName?:env.STAGE_NAME, STEP_CONFIG_KEYS)
|
|
|
|
.mixin([
|
2019-02-28 09:45:44 +02:00
|
|
|
artifactVersion: script.commonPipelineEnvironment.getArtifactVersion(),
|
|
|
|
influxPrefix: script.commonPipelineEnvironment.getGithubOrg() && script.commonPipelineEnvironment.getGithubRepo()
|
|
|
|
? "${script.commonPipelineEnvironment.getGithubOrg()}_${script.commonPipelineEnvironment.getGithubRepo()}"
|
|
|
|
: null
|
2018-08-15 11:53:28 +02:00
|
|
|
])
|
|
|
|
.mixin(parameters, PARAMETER_KEYS)
|
2019-01-17 16:42:03 +02:00
|
|
|
.addIfNull('customData', script.commonPipelineEnvironment.getInfluxCustomData())
|
|
|
|
.addIfNull('customDataTags', script.commonPipelineEnvironment.getInfluxCustomDataTags())
|
|
|
|
.addIfNull('customDataMap', script.commonPipelineEnvironment.getInfluxCustomDataMap())
|
|
|
|
.addIfNull('customDataMapTags', script.commonPipelineEnvironment.getInfluxCustomDataMapTags())
|
2018-08-15 11:53:28 +02:00
|
|
|
.use()
|
|
|
|
|
2019-01-21 09:47:34 +02:00
|
|
|
new Utils().pushToSWA([
|
|
|
|
step: STEP_NAME,
|
|
|
|
stepParamKey1: 'scriptMissing',
|
|
|
|
stepParam1: parameters?.script == null
|
|
|
|
], config)
|
2018-08-15 11:53:28 +02:00
|
|
|
|
2018-12-12 18:33:36 +02:00
|
|
|
if (!config.artifactVersion) {
|
2018-01-24 10:55:38 +02:00
|
|
|
//this takes care that terminated builds due to milestone-locking do not cause an error
|
2018-08-15 11:53:28 +02:00
|
|
|
echo "[${STEP_NAME}] no artifact version available -> exiting writeInflux without writing data"
|
2018-01-24 10:55:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:53:28 +02:00
|
|
|
echo """[${STEP_NAME}]----------------------------------------------------------
|
2018-12-12 18:33:36 +02:00
|
|
|
Artifact version: ${config.artifactVersion}
|
|
|
|
Influx server: ${config.influxServer}
|
|
|
|
Influx prefix: ${config.influxPrefix}
|
2019-01-17 16:42:03 +02:00
|
|
|
InfluxDB data: ${config.customData}
|
|
|
|
InfluxDB data tags: ${config.customDataTags}
|
|
|
|
InfluxDB data map: ${config.customDataMap}
|
|
|
|
InfluxDB data map tags: ${config.customDataMapTags}
|
2018-08-15 11:53:28 +02:00
|
|
|
[${STEP_NAME}]----------------------------------------------------------"""
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-12-12 18:33:36 +02:00
|
|
|
if(config.wrapInNode){
|
|
|
|
node(''){
|
|
|
|
try{
|
|
|
|
writeToInflux(config, script)
|
|
|
|
}finally{
|
|
|
|
deleteDir()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writeToInflux(config, script)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2018-12-12 18:33:36 +02:00
|
|
|
private void writeToInflux(config, script){
|
|
|
|
if (config.influxServer) {
|
2019-01-21 15:12:05 +02:00
|
|
|
try {
|
|
|
|
step([
|
|
|
|
$class: 'InfluxDbPublisher',
|
|
|
|
selectedTarget: config.influxServer,
|
|
|
|
customPrefix: config.influxPrefix,
|
|
|
|
customData: config.customData.size()>0 ? config.customData : null,
|
|
|
|
customDataTags: config.customDataTags.size()>0 ? config.customDataTags : null,
|
|
|
|
customDataMap: config.customDataMap.size()>0 ? config.customDataMap : null,
|
|
|
|
customDataMapTags: config.customDataMapTags.size()>0 ? config.customDataMapTags : null
|
|
|
|
])
|
|
|
|
} catch (NullPointerException e){
|
|
|
|
if(!e.getMessage()){
|
|
|
|
//TODO: catch NPEs as long as https://issues.jenkins-ci.org/browse/JENKINS-55594 is not fixed & released
|
|
|
|
error "[$STEP_NAME] NullPointerException occured, is the correct target defined?"
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
2018-01-24 10:55:38 +02:00
|
|
|
}
|
2018-12-12 18:33:36 +02:00
|
|
|
|
|
|
|
//write results into json file for archiving - also benefitial when no InfluxDB is available yet
|
|
|
|
def jsonUtils = new JsonUtils()
|
2019-01-17 16:42:03 +02:00
|
|
|
writeFile file: 'jenkins_data.json', text: jsonUtils.getPrettyJsonString(config.customData)
|
|
|
|
writeFile file: 'influx_data.json', text: jsonUtils.getPrettyJsonString(config.customDataMap)
|
|
|
|
writeFile file: 'jenkins_data_tags.json', text: jsonUtils.getPrettyJsonString(config.customDataTags)
|
|
|
|
writeFile file: 'influx_data_tags.json', text: jsonUtils.getPrettyJsonString(config.customDataMapTags)
|
2018-12-12 18:33:36 +02:00
|
|
|
archiveArtifacts artifacts: '*data.json', allowEmptyArchive: true
|
2018-01-24 10:55:38 +02:00
|
|
|
}
|