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