github.com/xgoffin/jenkins-library@v1.154.0/vars/commonPipelineEnvironment.groovy (about) 1 import com.sap.piper.ConfigurationLoader 2 import com.sap.piper.ConfigurationMerger 3 import com.sap.piper.DefaultValueCache 4 import com.sap.piper.analytics.InfluxData 5 import groovy.json.JsonOutput 6 7 class commonPipelineEnvironment implements Serializable { 8 9 //Project identifier which might be used to distinguish resources which are available globally, e.g. for locking 10 def projectName 11 12 //stores version of the artifact which is build during pipeline run 13 def artifactVersion 14 def originalArtifactVersion 15 16 // stores additional artifact coordinates 17 def artifactId 18 def groupId 19 def packaging 20 21 //stores the build tools if it inferred automatically, e.g. in the SAP Cloud SDK pipeline 22 String buildTool 23 24 //Stores the current buildResult 25 String buildResult = 'SUCCESS' 26 27 //stores the gitCommitId as well as additional git information for the build during pipeline run 28 String gitCommitId 29 String gitCommitMessage 30 String gitSshUrl 31 String gitHttpsUrl 32 String gitBranch 33 34 String xsDeploymentId 35 36 //GitHub specific information 37 String githubOrg 38 String githubRepo 39 40 //stores properties for a pipeline which build an artifact and then bundles it into a container 41 private Map appContainerProperties = [:] 42 43 Map configuration = [:] 44 Map containerProperties = [:] 45 Map defaultConfiguration = [:] 46 47 // Location of the file from where the configuration was parsed. See setupCommonPipelineEnvironment.groovy 48 // Useful for making sure that the piper binary uses the same file when called from Jenkins. 49 String configurationFile = '' 50 51 String mtarFilePath = null 52 53 String abapAddonDescriptor 54 55 private Map valueMap = [:] 56 57 void setValue(String property, value) { 58 valueMap[property] = value 59 } 60 61 def getValue(String property) { 62 return valueMap.get(property) 63 } 64 65 String changeDocumentId 66 67 def reset() { 68 69 projectName = null 70 71 abapAddonDescriptor = null 72 73 appContainerProperties = [:] 74 artifactVersion = null 75 originalArtifactVersion = null 76 77 artifactId = null 78 groupId = null 79 packaging = null 80 81 buildTool = null 82 83 configuration = [:] 84 containerProperties = [:] 85 86 gitCommitId = null 87 gitCommitMessage = null 88 gitSshUrl = null 89 gitHttpsUrl = null 90 gitBranch = null 91 92 githubOrg = null 93 githubRepo = null 94 95 mtarFilePath = null 96 valueMap = [:] 97 98 changeDocumentId = null 99 100 InfluxData.reset() 101 } 102 103 def setAppContainerProperty(property, value) { 104 appContainerProperties[property] = value 105 } 106 107 def getAppContainerProperty(property) { 108 return appContainerProperties[property] 109 } 110 111 def setContainerProperty(property, value) { 112 containerProperties[property] = value 113 } 114 115 def getContainerProperty(property) { 116 return containerProperties[property] 117 } 118 119 // goes into measurement jenkins_custom_data 120 def setInfluxCustomDataEntry(key, value) { 121 InfluxData.addField('jenkins_custom_data', key, value) 122 } 123 // goes into measurement jenkins_custom_data 124 @Deprecated // not used in library 125 def getInfluxCustomData() { 126 return InfluxData.getInstance().getFields().jenkins_custom_data 127 } 128 129 // goes into measurement jenkins_custom_data 130 def setInfluxCustomDataTagsEntry(key, value) { 131 InfluxData.addTag('jenkins_custom_data', key, value) 132 } 133 // goes into measurement jenkins_custom_data 134 @Deprecated // not used in library 135 def getInfluxCustomDataTags() { 136 return InfluxData.getInstance().getTags().jenkins_custom_data 137 } 138 139 void setInfluxCustomDataMapEntry(measurement, field, value) { 140 InfluxData.addField(measurement, field, value) 141 } 142 @Deprecated // not used in library 143 def getInfluxCustomDataMap() { 144 return InfluxData.getInstance().getFields() 145 } 146 147 def setInfluxCustomDataMapTagsEntry(measurement, tag, value) { 148 InfluxData.addTag(measurement, tag, value) 149 } 150 @Deprecated // not used in library 151 def getInfluxCustomDataMapTags() { 152 return InfluxData.getInstance().getTags() 153 } 154 155 @Deprecated // not used in library 156 def setInfluxStepData(key, value) { 157 InfluxData.addField('step_data', key, value) 158 } 159 @Deprecated // not used in library 160 def getInfluxStepData(key) { 161 return InfluxData.getInstance().getFields()['step_data'][key] 162 } 163 164 @Deprecated // not used in library 165 def setInfluxPipelineData(key, value) { 166 InfluxData.addField('pipeline_data', key, value) 167 } 168 @Deprecated // not used in library 169 def setPipelineMeasurement(key, value){ 170 setInfluxPipelineData(key, value) 171 } 172 @Deprecated // not used in library 173 def getPipelineMeasurement(key) { 174 return InfluxData.getInstance().getFields()['pipeline_data'][key] 175 } 176 177 Map getStepConfiguration(stepName, stageName = env.STAGE_NAME, includeDefaults = true) { 178 Map defaults = [:] 179 if (includeDefaults) { 180 defaults = ConfigurationLoader.defaultGeneralConfiguration() 181 defaults = ConfigurationMerger.merge(ConfigurationLoader.defaultStepConfiguration(null, stepName), null, defaults) 182 defaults = ConfigurationMerger.merge(ConfigurationLoader.defaultStageConfiguration(null, stageName), null, defaults) 183 } 184 Map config = ConfigurationMerger.merge(configuration.get('general') ?: [:] as Map, null, defaults) 185 config = ConfigurationMerger.merge(configuration.get('steps')?.get(stepName) ?: [:], null, config) 186 config = ConfigurationMerger.merge(configuration.get('stages')?.get(stageName) ?: [:], null, config) 187 return config 188 } 189 190 def files = [ 191 [filename: '.pipeline/commonPipelineEnvironment/artifactVersion', property: 'artifactVersion'], 192 [filename: '.pipeline/commonPipelineEnvironment/artifactId', property: 'artifactId'], 193 [filename: '.pipeline/commonPipelineEnvironment/groupId', property: 'groupId'], 194 [filename: '.pipeline/commonPipelineEnvironment/packaging', property: 'packaging'], 195 [filename: '.pipeline/commonPipelineEnvironment/buildTool', property: 'buildTool'], 196 [filename: '.pipeline/commonPipelineEnvironment/originalArtifactVersion', property: 'originalArtifactVersion'], 197 [filename: '.pipeline/commonPipelineEnvironment/github/owner', property: 'githubOrg'], 198 [filename: '.pipeline/commonPipelineEnvironment/github/repository', property: 'githubRepo'], 199 [filename: '.pipeline/commonPipelineEnvironment/git/branch', property: 'gitBranch'], 200 [filename: '.pipeline/commonPipelineEnvironment/git/commitId', property: 'gitCommitId'], 201 [filename: '.pipeline/commonPipelineEnvironment/git/httpsUrl', property: 'gitHttpsUrl'], 202 [filename: '.pipeline/commonPipelineEnvironment/git/commitMessage', property: 'gitCommitMessage'], 203 [filename: '.pipeline/commonPipelineEnvironment/mtarFilePath', property: 'mtarFilePath'], 204 [filename: '.pipeline/commonPipelineEnvironment/abap/addonDescriptor', property: 'abapAddonDescriptor'], 205 ] 206 207 Map getCPEMap(script) { 208 def cpeMap = [:] 209 files.each({f -> 210 createMapEntry(script, cpeMap, f.filename, this[f.property]) 211 }) 212 213 containerProperties.each({key, value -> 214 def filename = ".pipeline/commonPipelineEnvironment/container/${key}" 215 createMapEntry(script, cpeMap, filename, value) 216 }) 217 218 valueMap.each({key, value -> 219 def filename = ".pipeline/commonPipelineEnvironment/custom/${key}" 220 createMapEntry(script, cpeMap, filename, value) 221 }) 222 return cpeMap 223 } 224 225 void createMapEntry(script, Map resMap, String filename, value) { 226 // net.sf.json.JSONNull can come in through readPipelineEnv via readJSON() 227 // leaving them in will create a StackOverflowError further down in writePipelineEnv() 228 // thus removing them from the map for now 229 if (value != null && !(value instanceof net.sf.json.JSONNull)) { 230 // prefix is assumed by step if nothing else is specified 231 def prefix = ~/^.pipeline\/commonPipelineEnvironment\// 232 filename -= prefix 233 resMap[filename] = value 234 } 235 } 236 237 def setCPEMap(script, Map cpeMap) { 238 if (cpeMap == null) return 239 def prefix = ~/^.pipeline\/commonPipelineEnvironment\// 240 files.each({f -> 241 def key = f.filename - prefix 242 if (cpeMap.containsKey(key)) this[f.property] = cpeMap[key] 243 }) 244 245 cpeMap.each { 246 if (it.key.startsWith("custom/")) valueMap[it.key - ~/^custom\//] = it.value 247 if (it.key.startsWith("container/")) containerProperties[it.key - ~/^container\//] = it.value 248 } 249 } 250 251 List getCustomDefaults() { 252 DefaultValueCache.getInstance().getCustomDefaults() 253 } 254 }