-
Notifications
You must be signed in to change notification settings - Fork 242
/
ModelInterpreter.groovy
835 lines (767 loc) · 35.4 KB
/
ModelInterpreter.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.modeldefinition
import com.cloudbees.groovy.cps.NonCPS
import com.cloudbees.groovy.cps.impl.CpsClosure
import hudson.FilePath
import hudson.Launcher
import hudson.model.Result
import org.jenkinsci.plugins.pipeline.StageStatus
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.AbstractDockerAgent
import org.jenkinsci.plugins.pipeline.modeldefinition.model.*
import org.jenkinsci.plugins.pipeline.modeldefinition.options.DeclarativeOption
import org.jenkinsci.plugins.pipeline.modeldefinition.steps.CredentialWrapper
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional
import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
import javax.annotation.CheckForNull
import javax.annotation.Nonnull
import static java.lang.String.format
import static org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace
/**
* CPS-transformed code for actually performing the build.
*
* @author Andrew Bayer
*/
class ModelInterpreter implements Serializable {
private CpsScript script
ModelInterpreter(CpsScript script) {
this.script = script
}
def call(CpsClosure closure) {
Root root = (Root) closure.call()
Throwable firstError
if (root != null) {
boolean postBuildRun = false
Utils.updateRunAndJobActions(script, root.astUUID)
try {
loadLibraries(root)
executeProperties(root)
String restartedStage = Utils.getRestartedStage(script)
// Entire build, including notifications, runs in the agent.
inDeclarativeAgent(root, root, root.agent) {
withCredentialsBlock(root.environment) {
withEnvBlock(root.getEnvVars(script)) {
inWrappers(root.options?.wrappers) {
toolsBlock(root.tools, root.agent, null) {
firstError = evaluateSequentialStages(root, root.stages, firstError, null, restartedStage, null).call()
// Execute post-build actions now that we've finished all parallel.
try {
postBuildRun = true
executePostBuild(root)
} catch (Throwable e) {
if (firstError == null) {
firstError = e
}
}
}
// Throw any error we might have here to make sure that it gets caught and handled by
// wrappers.
if (firstError != null) {
throw firstError
}
}
}
}
}
} catch (Throwable e) {
// Catch any errors that may have been thrown outside of the parallel proper and make sure we set
// firstError accordingly.
if (firstError == null) {
firstError = e
}
} finally {
// If we hit an exception somewhere *before* we got to parallel, we still need to do post-build tasks.
if (!postBuildRun) {
try {
executePostBuild(root)
} catch (Throwable e) {
if (firstError == null) {
firstError = e
}
}
}
}
if (firstError != null) {
throw firstError
}
}
}
/**
* Actually execute a closure for a stage, conditional or post action.
*
* @param c The closure to execute
*/
def delegateAndExecute(Closure c) {
c.delegate = script
c.resolveStrategy = Closure.DELEGATE_FIRST
c.call()
}
/**
* Evaluate a list of sequential stages.
*
* @param root The root of the Declarative model
* @param stages The list of stages
* @param firstError An error that's already occurred earlier in the build. Can be null.
* @param parent The parent stage for this list of stages. Can be null.
* @param restartedStageName the name of the stage we're restarting at. Null if this is not a restarted build or this is
* called from a nested stage.
* @param skippedReason Possibly null reason the container for the stages was skipped.
* @return A closure to execute
*/
def evaluateSequentialStages(Root root, Stages stages, Throwable firstError, Stage parent, String restartedStageName,
SkippedStageReason skippedReason) {
return {
try {
boolean skippedForRestart = restartedStageName != null
stages.stages.each { thisStage ->
if (skippedForRestart) {
// Check if we're skipping for restart but are now on the stage we're supposed to restart on.
if (thisStage.name == restartedStageName) {
// If so, set skippedForRestart to false, and if the skippedReason is for restart, wipe that out too.
skippedForRestart = false
if (skippedReason instanceof SkippedStageReason.Restart) {
skippedReason = null
}
} else {
// If we skipped for restart and this isn't the restarted name, create a new reason.
skippedReason = new SkippedStageReason.Restart(thisStage.name, restartedStageName)
}
}
try {
evaluateStage(root, thisStage.agent ?: root.agent, thisStage, firstError, parent, skippedReason).call()
} catch (Throwable e) {
script.getProperty("currentBuild").result = Utils.getResultFromException(e)
Utils.markStageFailedAndContinued(thisStage.name)
if (firstError == null) {
firstError = e
}
}
if (skippedForRestart) {
Utils.markStartAndEndNodesInStageAsNotExecuted(thisStage.name)
}
}
} finally {
// And finally, run the post stage steps if this was a parallel parent.
if (skippedReason == null && parent != null &&
root.hasSatisfiedConditions(parent.post, script.getProperty("currentBuild"), parent, firstError)) {
Utils.logToTaskListener("Post stage")
firstError = runPostConditions(parent.post, parent.agent ?: root.agent, firstError, parent.name, parent)
}
}
return firstError
}
}
/**
* Get the map to pass to the parallel step of nested stages to run in parallel for the given stage.
*
* @param root The root of the Declarative model
* @param parentAgent The parent agent definition. Can be null.
* @param thisStage The current stage we'll look in for parallel stages
* @param firstError An error that's already occurred earlier in the build. Can be null.
* @param skippedReason A possibly null reason this stage, its children, and therefore its grandchildren, will be skipped.
* @return A map of parallel branch names to closures to pass to the parallel step
*/
def getParallelStages(Root root, Agent parentAgent, Stage thisStage, Throwable firstError, SkippedStageReason skippedReason) {
def parallelStages = [:]
thisStage?.parallelContent?.each { content ->
if (skippedReason != null) {
parallelStages.put(content.name,
evaluateStage(root, parentAgent, content, firstError, thisStage, skippedReason.cloneWithNewStage(content.name)))
} else {
parallelStages.put(content.name,
evaluateStage(root, thisStage.agent ?: parentAgent, content, firstError, thisStage, null))
}
}
if (!parallelStages.isEmpty() && ( thisStage.failFast || root.options?.options?.get("parallelsAlwaysFailFast") != null) ) {
parallelStages.put("failFast", true)
}
return parallelStages
}
/**
* Evaluate a stage, setting up agent, tools, env, etc, determining any nested stages to execute, skipping
* if appropriate, etc, actually executing the stage via executeSingleStage, parallel, or evaluateSequentialStages.
*
* @param root The root of the Declarative model
* @param parentAgent The parent agent definition, which can be null
* @param thisStage The stage we're actually evaluating.
* @param firstError An error that's already occurred earlier in the build. Can be null.
* @param parent The possible parent stage, defaults to null.
* @param skippedReason Possibly null reason this stage's parent, and therefore itself, is skipped.
* @return
*/
def evaluateStage(Root root, Agent parentAgent, Stage thisStage, Throwable firstError, Stage parent,
SkippedStageReason skippedReason) {
return {
def thisError = null
script.stage(thisStage.name) {
try {
if (skippedReason != null) {
skipStage(root, parentAgent, thisStage, firstError, skippedReason, parent).call()
} else if (firstError != null) {
skippedReason = new SkippedStageReason.Failure(thisStage.name)
skipStage(root, parentAgent, thisStage, firstError, skippedReason, parent).call()
} else if (skipUnstable(root.options)) {
skippedReason = new SkippedStageReason.Unstable(thisStage.name)
skipStage(root, parentAgent, thisStage, firstError, skippedReason, parent).call()
} else {
inWrappers(thisStage.options?.wrappers) {
if (thisStage?.parallelContent) {
stageInput(thisStage.input) {
if (evaluateWhen(thisStage.when)) {
withCredentialsBlock(thisStage.environment) {
withEnvBlock(thisStage.getEnvVars(script)) {
script.parallel(getParallelStages(root, parentAgent, thisStage, firstError, null))
}
}
} else {
skippedReason = new SkippedStageReason.When(thisStage.name)
skipStage(root, parentAgent, thisStage, firstError, skippedReason, parent).call()
}
}
} else {
def stageBody = {
withCredentialsBlock(thisStage.environment) {
withEnvBlock(thisStage.getEnvVars(script)) {
toolsBlock(thisStage.tools, thisStage.agent ?: root.agent, parent?.tools ?: root.tools) {
if (thisStage?.stages) {
def nestedError = evaluateSequentialStages(root, thisStage.stages, firstError, thisStage, null, null).call()
// Propagate any possible error from the sequential stages as if it were an error thrown directly.
if (nestedError != null) {
throw nestedError
}
} else {
// Execute the actual stage and potential post-stage actions
executeSingleStage(root, thisStage, parentAgent)
}
}
}
}
}
boolean isBeforeInput = thisStage.when?.beforeInput != null && thisStage.when?.beforeInput
boolean isBeforeAgent = thisStage.when?.beforeAgent != null && thisStage.when?.beforeAgent
boolean whenPassed = false
// if is beforeInput -> check when before anything
if (isBeforeInput) {
whenPassed = evaluateWhen(thisStage.when)
if(whenPassed) {
stageInput(thisStage.input) {
inDeclarativeAgent(thisStage, root, thisStage.agent) {
stageBody.call()
}
}
}
}else if(isBeforeAgent){
stageInput(thisStage.input) {
whenPassed = evaluateWhen(thisStage.when)
if (whenPassed) {
inDeclarativeAgent(thisStage, root, thisStage.agent) {
stageBody.call()
}
}
}
}else{
stageInput(thisStage.input) {
inDeclarativeAgent(thisStage, root, thisStage.agent) {
whenPassed = evaluateWhen(thisStage.when)
if (whenPassed) {
stageBody.call()
}
}
}
}
if (!whenPassed) {
skippedReason = new SkippedStageReason.When(thisStage.name)
skipStage(root, parentAgent, thisStage, firstError, skippedReason, parent).call()
}
}
}
}
} catch (Throwable e) {
script.getProperty("currentBuild").result = Result.FAILURE
Utils.markStageFailedAndContinued(thisStage.name)
if (firstError == null) {
firstError = e
}
thisError = e
} finally {
// And finally, run the post stage steps if this was a parallel parent.
if (skippedReason == null &&
root.hasSatisfiedConditions(thisStage.post, script.getProperty("currentBuild"), thisStage, firstError) &&
thisStage?.parallelContent) {
Utils.logToTaskListener("Post stage")
firstError = runPostConditions(thisStage.post, thisStage.agent ?: parentAgent, firstError, thisStage.name, thisStage)
}
}
if (firstError != null) {
throw firstError
}
}
}
}
def stageInput(StageInput input, Closure body) {
if (input != null) {
return {
def submitted = script.input(message: input.message, id: input.id, ok: input.ok, submitter: input.submitter,
submitterParameter: input.submitterParameter, parameters: input.parameters)
if (input.parameters.isEmpty() && input.submitterParameter == null) {
// No parameters, so just proceed
body.call()
} else {
def inputEnv = []
if (submitted instanceof Map) {
// Multiple parameters!
inputEnv = submitted.collect { k, v -> "${k}=${v}" }
} else if (input.submitterParameter != null) {
// Single parameter, it's the submitter.
inputEnv = ["${input.submitterParameter}=${submitted}"]
} else if (input.parameters.size() == 1) {
// One defined parameter, so we know its name.
inputEnv = ["${input.parameters.first().name}=${submitted}"]
}
script.withEnv(inputEnv) {
body.call()
}
}
}.call()
} else {
return {
body.call()
}.call()
}
}
def skipStage(Root root, Agent parentAgent, Stage thisStage, Throwable firstError, SkippedStageReason reason,
Stage parentStage) {
return {
Utils.logToTaskListener(reason.message)
Utils.markStageWithTag(thisStage.name, StageStatus.TAG_NAME, reason.stageStatus)
if (thisStage?.parallelContent) {
Map<String,Closure> parallelToSkip = getParallelStages(root, parentAgent, thisStage, firstError, reason)
script.parallel(parallelToSkip)
if (reason instanceof SkippedStageReason.Restart) {
parallelToSkip.keySet().each { k -> Utils.markStartAndEndNodesInStageAsNotExecuted(k) }
}
} else if (thisStage?.stages != null) {
String restartedStage = null
if (reason instanceof SkippedStageReason.Restart) {
restartedStage = reason.restartedStage
}
evaluateSequentialStages(root, thisStage.stages, firstError, thisStage, restartedStage, reason).call()
}
}
}
/**
* Execute the given body closure while watching for errors that will specifically show up when there's an attempt to
* run a step that needs a node context but doesn't have one.
*
* @param agent The {@link Agent} that applies to this execution. Used to clarify error message.
* @param inNotifications Whether we're currently in the notifications section, for error message clarification.
* @param body The closure to call
* @return The return of the resulting executed closure
* @throws Exception
*/
def catchRequiredContextForNode(Agent agent, Closure body) throws Exception {
return {
try {
body.call()
} catch (MissingContextVariableException e) {
if (FilePath.class == e.type || Launcher.class == e.type) {
if (!agent.hasAgent()) {
script.error(Messages.ModelInterpreter_NoNodeContext())
} else {
throw e
}
} else {
throw e
}
}
}.call()
}
@Deprecated
boolean skipUnstable(Options options) {
return skipUnstable(options?.options)
}
boolean skipUnstable(Map<String,DeclarativeOption> options) {
return script.getProperty("currentBuild").result == "UNSTABLE" &&
options?.get("skipStagesAfterUnstable") != null
}
/**
* Execute a body closure within a "withEnv" block.
*
* @param envVars A map of env vars to closures.
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def withEnvBlock(Map<String,Closure> envVars, Closure body) {
if (envVars != null && !envVars.isEmpty()) {
List<String> evaledEnv = envVars.collect { k, v ->
try{
"${k}=${v.call()}"
}catch (NullPointerException e){
throw new IllegalArgumentException(format("Invalid variable (%s) declared in environment; There are some problems with its value", k))
}
}.findAll { it != null}
return {
script.withEnv(evaledEnv) {
body.call()
}
}.call()
} else {
return {
body.call()
}.call()
}
}
/**
* Execute a given closure within a "withCredentials" block.
*
* @param environment The environment we're processing from
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def withCredentialsBlock(@CheckForNull Environment environment, Closure body) {
Map<String,CredentialWrapper> creds = new HashMap<>()
if (environment != null) {
try {
RunWrapper currentBuild = (RunWrapper)script.getProperty("currentBuild")
Utils.getCredsFromResolver(environment, script).each { k, v ->
String id = (String) v.call()
CredentialsBindingHandler handler = CredentialsBindingHandler.forId(id, currentBuild.rawBuild)
creds.put(k, new CredentialWrapper(id, handler.getWithCredentialsParameters(id)))
}
} catch (MissingMethodException e) {
// This will only happen in a running upgrade situation, so check the legacy approach as well.
creds.putAll(Utils.getLegacyEnvCredentials(environment))
}
}
if (!creds.isEmpty()) {
List<Map<String, Object>> parameters = createWithCredentialsParameters(creds)
return {
script.withCredentials(parameters) {
body.call()
}
}.call()
} else {
return {
body.call()
}.call()
}
}
/**
* Takes a map of keys to {@link CredentialWrapper}s and generates the proper output for the "withCredentials" block argument.
* @param credentials A map of keys to {@link CredentialWrapper}s
* @return A list of string->object maps suitable for passing to "withCredentials"
*/
@NonCPS
private List<Map<String, Object>> createWithCredentialsParameters(
@Nonnull Map<String, CredentialWrapper> credentials) {
List<Map<String, Object>> parameters = []
credentials.each { k, v ->
v.addParameters(k, parameters)
}
parameters
}
/**
* Legacy version to pass the root tools in, rather than directly passing in a tools. Only relevant for in-progress
* runs.
* TODO: Delete in 1.4? Or maybe just nuke now.
*/
@Deprecated
def toolsBlock(Agent agent, Tools tools, Root root = null, Closure body) {
return toolsBlock(tools, agent, root?.tools, body)
}
/**
* Executes a given closure in a "withEnv" block after installing the specified tools
* @param tools The tools configuration we're using
* @param agent The agent context we're running in
* @param rootTools The parent level configuration, if we're called within a stage. Can be null.
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def toolsBlock(Tools tools, Agent agent, Tools rootTools, Closure body) {
def toolsList = []
if (tools != null) {
toolsList = tools.mergeToolEntries(rootTools)
} else if (rootTools != null) {
toolsList = rootTools.mergeToolEntries(null)
}
// If there's no agent, don't install tools in the first place.
if (agent.hasAgent() && !toolsList.isEmpty()) {
def toolEnv = []
if (!Utils.withinAStage()) {
script.stage(SyntheticStageNames.toolInstall()) {
toolEnv = actualToolsInstall(toolsList)
}
} else {
toolEnv = actualToolsInstall(toolsList)
}
return {
script.withEnv(toolEnv) {
body.call()
}
}.call()
} else {
return {
body.call()
}.call()
}
}
def actualToolsInstall(List<List<Object>> toolsList) {
def toolEnv = []
toolsList.each { l ->
String k = l.get(0)
Closure v = (Closure)l.get(1)
String toolVer = v.call()
script.tool(name: toolVer, type: Tools.typeForKey(k))
toolEnv.addAll(script.envVarsForTool(toolId: Tools.typeForKey(k), toolVersion: toolVer))
}
return toolEnv
}
/**
* Executes the given closure inside a declarative agent block, if appropriate.
*
* @param context Either a stage or root object, the context we're running in.
* @param root The root object for this pipeline
* @param agent The agent context we're running in
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def inDeclarativeAgent(Object context, Root root, Agent agent, Closure body) {
if (agent != null) {
agent.populateMap((Map<String,Object>)instanceFromClosure(agent.rawClosure, Map.class))
}
if (agent == null
&& root.agent.getDeclarativeAgent(root, root) instanceof AbstractDockerAgent
&& root.options?.options?.get("newContainerPerStage") != null) {
agent = root.agent
}
if (agent == null) {
return {
body.call()
}.call()
} else {
return agent.getDeclarativeAgent(root, context).getScript(script).run {
body.call()
}.call()
}
}
@Deprecated
def inWrappers(Options options, Closure body) {
return inWrappers(options?.wrappers, body)
}
/**
* Executes the given closure inside 0 or more wrapper blocks if appropriate
* @param wrappers A map of wrapper names to wrappers
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def inWrappers(Map<String,Object> wrappers, Closure body) {
if (wrappers != null) {
return {
recursiveWrappers(wrappers.keySet().toList(), wrappers, body)
}.call()
} else {
return {
body.call()
}.call()
}
}
/**
* Generates and executes a single (or no) wrapper block, recursively calling itself on any remaining wrapper names.
* @param wrapperNames A list of wrapper names remaining to run
* @param wrappers The wrappers configuration we're executing in
* @param body The closure to execute
* @return The return of the resulting executed closure
*/
def recursiveWrappers(List<String> wrapperNames, Map<String,Object> wrappers, Closure body) {
if (wrapperNames.isEmpty()) {
return {
body.call()
}.call()
} else {
def thisWrapper = wrapperNames.remove(0)
def wrapperArgs = wrappers.get(thisWrapper)
if (wrapperArgs != null) {
return {
script."${thisWrapper}"(wrapperArgs) {
recursiveWrappers(wrapperNames, wrappers, body)
}
}.call()
} else {
return {
script."${thisWrapper}"() {
recursiveWrappers(wrapperNames, wrappers, body)
}
}.call()
}
}
}
/**
* Executes a single stage and post-stage actions, and returns any error it may have generated.
*
* @param root The root context we're running in
* @param thisStage The stage context we're running in
* @param parentAgent the possible parent agent we should be running in
*/
def executeSingleStage(Root root, Stage thisStage, Agent parentAgent) throws Throwable {
Throwable stageError = null
try {
catchRequiredContextForNode(thisStage.agent ?: parentAgent) {
delegateAndExecute(thisStage.steps.closure)
}
} catch (Throwable e) {
script.getProperty("currentBuild").result = Utils.getResultFromException(e)
Utils.markStageFailedAndContinued(thisStage.name)
if (stageError == null) {
stageError = e
}
} finally {
// And finally, run the post stage steps.
if (root.hasSatisfiedConditions(thisStage.post, script.getProperty("currentBuild"), thisStage, stageError)) {
Utils.logToTaskListener("Post stage")
stageError = runPostConditions(thisStage.post, thisStage.agent ?: parentAgent, stageError, thisStage.name, thisStage)
}
}
if (stageError != null) {
throw stageError
}
}
/**
*
*/
def evaluateWhen(StageConditionals when, boolean skipDueToParent = false) {
if (skipDueToParent) {
return false
} else if (when == null) {
return true
} else {
// To allow for referencing environment variables that have not yet been declared pre-parse time, we need
// to actually instantiate the conditional now, via a closure.
return instancesFromClosure(when.rawClosure, DeclarativeStageConditional.class).every {
it?.getScript(script)?.evaluate()
}
}
}
/**
* Takes a closure that evaluates into a list of instances of a given class, sets that closure to delegate to our
* CpsScript, calls it, and returns a list of the instances of that class.
*
* @param rawClosure
* @param instanceType
* @return A list of instances
*/
private <Z> List<Z> instancesFromClosure(Closure rawClosure, Class<Z> instanceType) {
rawClosure.delegate = script
rawClosure.resolveStrategy = Closure.DELEGATE_FIRST
List<Z> instanceList = []
rawClosure.call().each { inst ->
if (instanceType.isInstance(inst)) {
instanceList.add(instanceType.cast(inst))
}
}
return instanceList
}
private <Z> Z instanceFromClosure(Closure rawClosure, Class<Z> instanceType) {
rawClosure.delegate = script
rawClosure.resolveStrategy = Closure.DELEGATE_FIRST
def inst = rawClosure.call()
if (instanceType.isInstance(inst)) {
return instanceType.cast(inst)
}
return null
}
/**
* Executes the post build actions for this build
* @param root The root context we're executing in
*/
def executePostBuild(Root root) throws Throwable {
Throwable stageError = null
if (root.hasSatisfiedConditions(root.post, script.getProperty("currentBuild"))) {
script.stage(SyntheticStageNames.postBuild()) {
stageError = runPostConditions(root.post, root.agent, stageError)
}
}
if (stageError != null) {
throw stageError
}
}
/**
* Actually does the execution of post actions, both post-stage and post-build.
* @param responder The {@link AbstractBuildConditionResponder} we're pulling conditions from.
* @param agentContext The {@link Agent} context we're running in.
* @param stageError Any existing error from earlier parts of the stage we're in, or null.
* @param stageName Optional - the name of the stage we're running in, so we can mark it as failed if needed.
* @param context Optional - the context where we're being called
* @return The stageError, which, if null when passed in and an error is hit, will be set to the first error encountered.
*/
def runPostConditions(AbstractBuildConditionResponder responder,
Agent agentContext,
Throwable stageError,
String stageName = null,
Object context = null) {
BuildCondition.orderedConditionNames.each { conditionName ->
try {
Closure c = responder.closureForSatisfiedCondition(conditionName, script.getProperty("currentBuild"),
context, stageError)
if (c != null) {
catchRequiredContextForNode(agentContext) {
delegateAndExecute(c)
}
}
} catch (Throwable e) {
script.getProperty("currentBuild").result = Utils.getResultFromException(e)
if (stageName != null) {
Utils.markStageFailedAndContinued(stageName)
}
Utils.logToTaskListener("Error when executing ${conditionName} post condition:")
Utils.logToTaskListener(getFullStackTrace(e))
if (stageError == null) {
stageError = e
}
}
}
return stageError
}
/**
* Load specified libraries.
*
* @param root The root context we're running in
*/
def loadLibraries(Root root) {
if (root.libraries != null) {
root.libraries.libs.each { lib ->
script.library(lib)
}
}
}
/**
* Sets any appropriate job properties for this build.
*
* @param root The root context we're running in
*/
def executeProperties(Root root) {
Utils.updateJobProperties(root.options?.properties, root.triggers?.triggers, root.parameters?.parameters, root.options?.options, script)
}
}