2020-02-06 12:27:31 +02:00
|
|
|
import com.sap.piper.JenkinsUtils
|
|
|
|
|
2018-09-21 16:55:31 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
|
2019-04-01 09:57:24 +02:00
|
|
|
import com.sap.piper.GenerateDocumentation
|
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
|
2019-04-11 11:39:41 +02:00
|
|
|
import com.sap.piper.analytics.InfluxData
|
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([
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines the version of the current artifact. Defaults to `commonPipelineEnvironment.getArtifactVersion()`
|
|
|
|
*/
|
2019-01-17 16:42:03 +02:00
|
|
|
'artifactVersion',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines custom data (map of key-value pairs) to be written to Influx into measurement `jenkins_custom_data`. Defaults to `commonPipelineEnvironment.getInfluxCustomData()`
|
|
|
|
*/
|
2019-01-17 16:42:03 +02:00
|
|
|
'customData',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines tags (map of key-value pairs) to be written to Influx into measurement `jenkins_custom_data`. Defaults to `commonPipelineEnvironment.getInfluxCustomDataTags()`
|
|
|
|
*/
|
2019-01-17 16:42:03 +02:00
|
|
|
'customDataTags',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines a map of measurement names containing custom data (map of key-value pairs) to be written to Influx. Defaults to `commonPipelineEnvironment.getInfluxCustomDataMap()`
|
|
|
|
*/
|
2019-01-17 16:42:03 +02:00
|
|
|
'customDataMap',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines a map of measurement names containing tags (map of key-value pairs) to be written to Influx. Defaults to `commonPipelineEnvironment.getInfluxCustomDataTags()`
|
|
|
|
*/
|
2019-01-17 16:42:03 +02:00
|
|
|
'customDataMapTags',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines the name of the Influx server as configured in Jenkins global configuration.
|
|
|
|
*/
|
2018-08-15 11:53:28 +02:00
|
|
|
'influxServer',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines a custom prefix.
|
|
|
|
* For example in multi branch pipelines, where every build is named after the branch built and thus you have different builds called 'master' that report different metrics.
|
|
|
|
*/
|
2018-12-12 18:33:36 +02:00
|
|
|
'influxPrefix',
|
2020-06-23 14:16:44 +02:00
|
|
|
'sonarTokenCredentialsId',
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Defines if a dedicated node/executor should be created in the pipeline run.
|
|
|
|
* This is especially relevant when running the step in a declarative `POST` stage where by default no executor is available.
|
|
|
|
*/
|
2018-12-12 18:33:36 +02:00
|
|
|
'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
|
|
|
|
2019-04-01 09:57:24 +02:00
|
|
|
/**
|
|
|
|
* Since your Continuous Delivery Pipeline in Jenkins provides your productive development and delivery infrastructure you should monitor the pipeline to ensure it runs as expected. How to setup this monitoring is described in the following.
|
|
|
|
*
|
|
|
|
* You basically need three components:
|
|
|
|
*
|
|
|
|
* - The [InfluxDB Jenkins plugin](https://wiki.jenkins-ci.org/display/JENKINS/InfluxDB+Plugin) which allows you to send build metrics to InfluxDB servers
|
|
|
|
* - The [InfluxDB](https://www.influxdata.com/time-series-platform/influxdb/) to store this data (Docker available)
|
|
|
|
* - A [Grafana](http://grafana.org/) dashboard to visualize the data stored in InfluxDB (Docker available)
|
|
|
|
*
|
|
|
|
* !!! note "no InfluxDB available?"
|
|
|
|
* If you don't have an InfluxDB available yet this step will still provide you some benefit.
|
|
|
|
*
|
|
|
|
* It will create following files for you and archive them into your build:
|
|
|
|
*
|
|
|
|
* * `jenkins_data.json`: This file gives you build-specific information, like e.g. build result, stage where the build failed
|
|
|
|
* * `influx_data.json`: This file gives you detailed information about your pipeline, e.g. stage durations, steps executed, ...
|
|
|
|
*/
|
|
|
|
@GenerateDocumentation
|
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
|
|
|
|
2020-08-26 15:32:58 +02:00
|
|
|
def script = checkScript(this, parameters) ?: this
|
2020-02-06 12:27:31 +02:00
|
|
|
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
|
2020-08-26 15:32:58 +02:00
|
|
|
String stageName = parameters.stageName ?: env.STAGE_NAME
|
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)
|
2020-08-26 15:32:58 +02:00
|
|
|
.loadStepDefaults([:], stageName)
|
2018-08-15 11:53:28 +02:00
|
|
|
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
|
|
|
|
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
|
2020-08-26 15:32:58 +02:00
|
|
|
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
|
2018-08-15 11:53:28 +02:00
|
|
|
.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-04-11 11:39:41 +02:00
|
|
|
.addIfNull('customData', InfluxData.getInstance().getFields().jenkins_custom_data)
|
|
|
|
.addIfNull('customDataTags', InfluxData.getInstance().getTags().jenkins_custom_data)
|
|
|
|
.addIfNull('customDataMap', InfluxData.getInstance().getFields().findAll({ it.key != 'jenkins_custom_data' }))
|
|
|
|
.addIfNull('customDataMapTags', InfluxData.getInstance().getTags().findAll({ it.key != 'jenkins_custom_data' }))
|
2018-08-15 11:53:28 +02:00
|
|
|
.use()
|
|
|
|
|
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{
|
2020-02-06 12:27:31 +02:00
|
|
|
writeToInflux(config, jenkinsUtils, script)
|
2018-12-12 18:33:36 +02:00
|
|
|
}finally{
|
|
|
|
deleteDir()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-06 12:27:31 +02:00
|
|
|
writeToInflux(config, jenkinsUtils, script)
|
2018-12-12 18:33:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 10:55:38 +02:00
|
|
|
|
2020-02-06 12:27:31 +02:00
|
|
|
private void writeToInflux(config, JenkinsUtils jenkinsUtils, script){
|
2018-12-12 18:33:36 +02:00
|
|
|
if (config.influxServer) {
|
2020-02-06 12:27:31 +02:00
|
|
|
|
|
|
|
def influxPluginVersion = jenkinsUtils.getPluginVersion('influxdb')
|
|
|
|
|
2019-01-21 15:12:05 +02:00
|
|
|
try {
|
2020-06-23 14:16:44 +02:00
|
|
|
def credentialList = []
|
2020-02-06 12:27:31 +02:00
|
|
|
def influxParams = [
|
2019-01-21 15:12:05 +02:00
|
|
|
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
|
2020-02-06 12:27:31 +02:00
|
|
|
]
|
2020-06-23 14:16:44 +02:00
|
|
|
if(config.sonarTokenCredentialsId){
|
|
|
|
credentialList.add(string(
|
|
|
|
credentialsId: config.sonarTokenCredentialsId,
|
|
|
|
variable: 'SONAR_AUTH_TOKEN'
|
|
|
|
))
|
|
|
|
}
|
|
|
|
withCredentials(credentialList){
|
|
|
|
if (!influxPluginVersion || influxPluginVersion.startsWith('1.')) {
|
|
|
|
influxParams['$class'] = 'InfluxDbPublisher'
|
|
|
|
step(influxParams)
|
|
|
|
} else {
|
|
|
|
influxDbPublisher(influxParams)
|
|
|
|
}
|
2020-02-06 12:27:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 15:12:05 +02:00
|
|
|
} catch (NullPointerException e){
|
|
|
|
if(!e.getMessage()){
|
|
|
|
//TODO: catch NPEs as long as https://issues.jenkins-ci.org/browse/JENKINS-55594 is not fixed & released
|
2020-09-24 07:41:06 +02:00
|
|
|
error "[$STEP_NAME] NullPointerException occurred, is the correct target defined?"
|
2019-01-21 15:12:05 +02:00
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
2018-01-24 10:55:38 +02:00
|
|
|
}
|
2018-12-12 18:33:36 +02:00
|
|
|
|
2020-09-24 07:41:06 +02:00
|
|
|
//write results into json file for archiving - also beneficial when no InfluxDB is available yet
|
2018-12-12 18:33:36 +02:00
|
|
|
def jsonUtils = new JsonUtils()
|
2019-03-21 14:25:22 +02:00
|
|
|
writeFile file: 'jenkins_data.json', text: jsonUtils.groovyObjectToPrettyJsonString(config.customData)
|
|
|
|
writeFile file: 'influx_data.json', text: jsonUtils.groovyObjectToPrettyJsonString(config.customDataMap)
|
|
|
|
writeFile file: 'jenkins_data_tags.json', text: jsonUtils.groovyObjectToPrettyJsonString(config.customDataTags)
|
|
|
|
writeFile file: 'influx_data_tags.json', text: jsonUtils.groovyObjectToPrettyJsonString(config.customDataMapTags)
|
2021-03-10 10:23:36 +02:00
|
|
|
archiveArtifacts artifacts: '*data*.json', allowEmptyArchive: true
|
2018-01-24 10:55:38 +02:00
|
|
|
}
|