forked from dotnet/dotnet-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipeline.groovy
437 lines (381 loc) · 17.7 KB
/
Pipeline.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package org.dotnet.ci.pipelines;
import hudson.Util;
import org.apache.commons.lang.StringUtils;
import jobs.generation.Utilities
import jobs.generation.GenerationSettings
import org.dotnet.ci.triggers.GithubTriggerBuilder
import org.dotnet.ci.triggers.VSTSTriggerBuilder
import org.dotnet.ci.triggers.GenericTriggerBuilder
import org.dotnet.ci.triggers.TriggerBuilder
import org.dotnet.ci.pipelines.scm.PipelineScm
import org.dotnet.ci.pipelines.scm.GithubPipelineScm
import org.dotnet.ci.pipelines.scm.VSTSPipelineScm
// Contains functionality to deal with Jenkins pipelines.
// This class enables us to inform Jenkins about pipelines and set up triggers for those pipeline/parameter combos
// as needed.
class Pipeline {
private String _pipelineFile
private String _baseJobName
PipelineScm _scm
// Context of the Job DSL to use for creating jobs
private def _context
private Pipeline(def context, String baseJobName, String pipelineFile) {
_pipelineFile = pipelineFile
_baseJobName = baseJobName
_context = context
}
public setSourceControl(PipelineScm scm) {
_scm = scm
}
private static String getDefaultPipelineJobBaseName(String pipelineFile) {
String baseName = pipelineFile
// Strip off any path prefix
int lastSlash = baseName.lastIndexOf('/')
if (lastSlash != -1) {
baseName = baseName.substring(lastSlash + 1)
}
// Strip off anything after a .
int lastDot = baseName.indexOf('.')
if (lastDot != -1) {
// Has extension
assert lastDot != 0
baseName = baseName.substring(0, lastDot)
}
return baseName
}
// Replace all the unsafe characters in the input string
// with _
// See Jenkins.java's checkGoodName for source of the bad characters
private static String getValidJobNameString(String input) {
String finalString = ''
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i)
// Temporary. '=' and ',' are not invalid paths, but causes problems with CLI
if(',=?*/\\%!@#$^&|<>[]:;'.indexOf("${ch}")!=-1) {
finalString += '_'
}
else {
finalString += ch
}
}
final int maxElementLength = 64
return shortenString(finalString, maxElementLength)
}
private static String shortenString(String input, int max) {
if (input.length() < max) {
return input
}
String abbreviatedInput = StringUtils.abbreviate(input, 0, 16)
String digest = Util.getDigestOf(input.substring(0, 8))
// Don't abbreviate if the name would be longer than the original
if (input.length() < abbreviatedInput.length() + digest.length()) {
return input
}
else {
return abbreviatedInput + digest
}
}
// Determines a full job name for a pipeline job from the base job and parameter set
//
public static String getPipelineJobName(String _baseJobName, Map<String,Object> parameters = [:]) {
// Take the base job name and append '-'' if there are any parameters
// If parameters, walk the parameter list. Append X=Y forms, replacing any
// invalid characters with _, separated by comma
String finalJobName = _baseJobName
if (parameters.size() != 0) {
finalJobName += '-'
boolean needsComma = false
parameters.each { k,v ->
if (needsComma) {
finalJobName += '+'
}
String paramName = getValidJobNameString(k)
// This could be a boolean or string
assert v instanceof String || v instanceof Boolean : "Unknown type of value ${v} for parameter ${k} used. Please use string or boolean (currently ${v.getClass()}"
String paramValue = getValidJobNameString(v.toString())
// Temporary - Don't use an equals sign. This causes issues with the CLI
// finalJobName += "${paramName}=${paramValue}"
finalJobName += "${paramName}_${paramValue}"
needsComma = true
}
}
// Shorten the entire job name
final int maxElementLength = 256
return shortenString(finalJobName, maxElementLength)
}
// Creates a new pipeline given the pipeline groovy script that
// will be invoked. A base job name is derived from the pipeline file name
// Parameters:
// context - Context used to construct new pipelines. Pass 'this' from groovy file.
// project - GitHub project that the pipeline lives in.
// branch - Branch that the project lives in
// pipelineFile - File name relative to root of the repo
public static Pipeline createPipelineForGithub(def context, String project, String branch, String pipelineFile) {
String baseJobName = getDefaultPipelineJobBaseName(pipelineFile)
def newPipeline = new Pipeline(context, baseJobName, pipelineFile)
// Create a new source control for the basic setup here.
// By default today we're using the r/o PAT that identifies the cloner as dotnet-bot
// to avoid API rate limit issues when cloning the pipeline script, which happens on the master.
def scm = new GithubPipelineScm(project, branch, 'dotnet-bot-readonly-public-clone-token')
newPipeline.setSourceControl(scm)
return newPipeline
}
/**
* Creates a new pipeline for VSTS
*
* @param context Context used to construct new pipelines. Pass 'this' from groovy file.
* @param project Qualified name of the project/repo combo (VSTS)
* @param branch Branch where the pipeline lives
* @param pipelineFile Pipeline path relative to root of the repo.
*
* @return Newly created pipeline
*/
private static Pipeline createPipelineForVSTS(def context, String project, String branch, String pipelineFile) {
String collectionName = context.getBinding().getVariables()['VSTSCollectionName']
String credentialsId = context.getBinding().getVariables()['VSTSCredentialsId']
String baseJobName = getDefaultPipelineJobBaseName(pipelineFile)
def newPipeline = new Pipeline(context, baseJobName, pipelineFile)
// Create a new source control for the basic setup here
def scm = new VSTSPipelineScm(project, branch, credentialsId, collectionName)
newPipeline.setSourceControl(scm)
return newPipeline
}
/**
* Creates a new generic pipeline for the given source control location
*
* @param context Context used to construct new pipelines. Pass 'this' from groovy file.
* @param scmType Where the SCM lives. Use either 'VSTS' or 'GitHub'. Typically passed in from VersionControlLocation parameter
* @param project Qualified name of the project/repo combo (VSTS) or org/repo combo (GitHub)
* @param branch Branch where the pipeline lives
* @param pipelineFile Pipeline path relative to root of the repo.
*
* @return Newly created pipeline
*/
public static Pipeline createPipeline(def context, String pipelineFile) {
// From the context, we can get the incoming parameters These incoming parameters
// will tell us things like the credentials (VSTS), project, branch, collection (VSTS), etc.
String scmType = context.getBinding().getVariables()['VersionControlLocation']
String project = context.getBinding().getVariables()['QualifiedRepoName']
String branch = context.getBinding().getVariables()['BranchName']
if (scmType == 'VSTS') {
return createPipelineForVSTS(context, project, branch, pipelineFile)
}
else if (scmType == 'GitHub') {
return createPipelineForGithub(context, project, branch, pipelineFile)
}
else {
assert false : "NYI, unknown scm type"
}
}
/**
* Triggers a pipeline on every PR.
*
* @param context Context of the status check that should be presented in the GitHub/VSTS UI.
* @param parameters Parameters to pass to the pipeline
*
* @return Newly created job
*/
public def triggerPipelineOnEveryPR(String context, Map<String,Object> parameters = [:], String jobName = null) {
if (this._scm.getScmType() == 'VSTS') {
return triggerPipelineOnEveryVSTSPR(context, parameters, jobName)
}
else if (this._scm.getScmType() == 'GitHub') {
return triggerPipelineOnEveryGithubPR(context, parameters, jobName)
}
else {
assert false : "NYI, unknown scm type"
}
}
// Triggers a puipeline on every Github PR.
// Parameters:
// context - The context that appears for the status check in the Github UI
// parameter - Optional set of key/value pairs of string parameters that will be passed to the pipeline
public def triggerPipelineOnEveryGithubPR(String context, Map<String,Object> parameters = [:], String jobName = null) {
// Create the default trigger phrase based on the context
return triggerPipelineOnEveryGithubPR(context, null, parameters, jobName)
}
// Triggers a puipeline on every Github PR, with a custom trigger phrase.
// Parameters:
// context - The context that appears for the status check in the Github UI
// triggerPhrase - The trigger phrase that can relaunch the pipeline
// parameters - Optional set of key/value pairs of string parameters that will be passed to the pipeline
public def triggerPipelineOnEveryGithubPR(String context, String triggerPhrase, Map<String,Object> parameters = [:], String jobName = null) {
// Create a trigger builder and pass it to the generic triggerPipelineOnEvent
GithubTriggerBuilder builder = GithubTriggerBuilder.triggerOnPullRequest()
builder.setGithubContext(context)
// If the trigger phrase is non-null, specify it
if (triggerPhrase != null) {
builder.setCustomTriggerPhrase(triggerPhrase)
}
// Ensure it's always run
builder.triggerByDefault()
// Set the target branch
builder.triggerForBranch(this._scm.getBranch())
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
// Triggers a pipeline on a Github PR when the specified phrase is commented.
// Parameters:
// context - The context that appears for the status check in the Github UI
// triggerPhrase - The trigger phrase that can relaunch the pipeline
// parameters - Optional set of key/value pairs of string parameters that will be passed to the pipeline
public def triggerPipelineOnGithubPRComment(String context, String triggerPhrase, Map<String,Object> parameters = [:], String jobName = null) {
// Create the trigger event and call the helper API
GithubTriggerBuilder builder = GithubTriggerBuilder.triggerOnPullRequest()
builder.setGithubContext(context)
if (triggerPhrase != null) {
builder.setCustomTriggerPhrase(triggerPhrase)
}
builder.triggerOnlyOnComment()
builder.triggerForBranch(this._scm.getBranch())
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
// Triggers a pipeline on a Github PR, using the context as the trigger phrase
// Parameters:
// context - The context to show on GitHub + trigger phrase that will launch the job
// Returns:
// Newly created pipeline job
public def triggerPipelineOnGithubPRComment(String context, Map<String,Object> parameters = [:], String jobName = null) {
// Create the default trigger phrase based on the context
return triggerPipelineOnGithubPRComment(context, null, parameters, jobName)
}
// Triggers a pipeline on every VSTS PR.
// Parameters:
// context - The context that appears for the status check in the VSTS UI
// parameter - Optional set of key/value pairs of string parameters that will be passed to the pipeline
public def triggerPipelineOnEveryVSTSPR(String context, Map<String,Object> parameters = [:], String jobName) {
// Create a trigger builder and pass it to the generic triggerPipelineOnEvent
VSTSTriggerBuilder builder = VSTSTriggerBuilder.triggerOnPullRequest(context)
builder.triggerForBranch(this._scm.getBranch())
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
/**
* Triggers a pipeline on every push.
*
* @param parameters Parameters to pass to the pipeline
*
* @return Newly created job
*/
public def triggerPipelineOnPush(Map<String,Object> parameters = [:], String jobName = null) {
if (this._scm.getScmType() == 'VSTS') {
return triggerPipelineOnVSTSPush(parameters, jobName)
}
else if (this._scm.getScmType() == 'GitHub') {
return triggerPipelineOnGithubPush(parameters, jobName)
}
else {
assert false : "NYI, unknown scm type"
}
}
/**
* Triggers a pipeline on every push.
*
* @param context Context of the pipeline job to be triggered
* @param parameters Parameters to pass to the pipeline
*
* @return Newly created job
*/
public def triggerPipelineOnPush(String context, Map<String,Object> parameters = [:], String jobName = null) {
if (this._scm.getScmType() == 'VSTS') {
return triggerPipelineOnVSTSPush(parameters, context, jobName)
}
else if (this._scm.getScmType() == 'GitHub') {
return triggerPipelineOnGithubPush(parameters, jobName)
}
else {
assert false : "NYI, unknown scm type"
}
}
// Triggers a pipeline on a VSTS Push
// Parameters:
// parameters - Parameters to pass to the pipeline on a push
// Returns:
// Newly created job
public def triggerPipelineOnVSTSPush(Map<String,Object> parameters = [:], String contextString = null, String jobName = null) {
VSTSTriggerBuilder builder = VSTSTriggerBuilder.triggerOnCommit(contextString)
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
// Triggers a pipeline on a Github Push
// Parameters:
// parameters - Parameters to pass to the pipeline on a push
// Returns:
// Newly created job
public def triggerPipelineOnGithubPush(Map<String,Object> parameters = [:], String jobName = null) {
GithubTriggerBuilder builder = GithubTriggerBuilder.triggerOnCommit()
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
// Triggers a pipeline periodically, if changes have been made to the
// source control in question.
public def triggerPipelinePeriodically(String cronString, Map<String,Object> parameters = [:], String jobName = null) {
GenericTriggerBuilder builder = GenericTriggerBuilder.triggerPeriodically(cronString)
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
/* Creates a pipeline that only triggers manually
*
* @param parameters Parameters to pass to the pipeline
*
* @return Newly created job
*/
public def triggerPipelineManually(Map<String,Object> parameters = [:], String jobName = null) {
GenericTriggerBuilder builder = GenericTriggerBuilder.triggerManually()
// Call the generic API
return triggerPipelineOnEvent(builder, parameters, jobName)
}
// Creates a pipeline job for a generic trigger event
// Parameters:
// triggerBuilder - Trigger that the pipeline should run on
// parameter - Parameter set to run the pipeline with
// Returns
// Newly created pipeline job
public def triggerPipelineOnEvent(TriggerBuilder triggerBuilder, Map<String,Object> params = [:], String jobName = null) {
// Determine the job name
// Job name is based off the parameters
def isPR = triggerBuilder.isPRTrigger()
def _jobName = jobName
if(_jobName == null) {
_jobName = Pipeline.getPipelineJobName(_baseJobName, params)
}
def fullJobName = Utilities.getFullJobName(_jobName, isPR)
// Create the standard pipeline job
def newJob = createStandardPipelineJob(fullJobName, isPR, params)
if (isPR) {
// Emit the source control
_scm.emitScmForPR(newJob, this._pipelineFile)
}
else {
_scm.emitScmForNonPR(newJob, this._pipelineFile)
}
newJob.with {
// Emit additional parameters for the input parameters
params.each { k,v ->
parameters {
// The type of the parameter is dependent on the value. If v is a boolean, then
// make a boolean parameter. Otherwise string
if (v instanceof Boolean) {
booleanParam(k,v, '')
}
else {
stringParam(k,v, '')
}
}
}
}
// Emit the trigger
triggerBuilder.emitTrigger(newJob)
return newJob
}
private def createStandardPipelineJob(String fullJobName, boolean isPR, Map<String,Object> parameters) {
// Create the new pipeline job
def newJob = _context.pipelineJob(fullJobName) {}
// Most options are set up in the pipeline itself.
// We really only need to set up the retention policy
Utilities.addStandardOptions(newJob, isPR)
// Return the new job
return newJob
}
}