-
Notifications
You must be signed in to change notification settings - Fork 831
/
WorkflowDefinitionContext.groovy
59 lines (50 loc) · 2.12 KB
/
WorkflowDefinitionContext.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
package javaposse.jobdsl.dsl.helpers.workflow
import javaposse.jobdsl.dsl.AbstractExtensibleContext
import javaposse.jobdsl.dsl.ContextHelper
import javaposse.jobdsl.dsl.ContextType
import javaposse.jobdsl.dsl.DslContext
import javaposse.jobdsl.dsl.Item
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.dsl.Preconditions
import javaposse.jobdsl.dsl.RequiresPlugin
@ContextType('org.jenkinsci.plugins.workflow.flow.FlowDefinition')
class WorkflowDefinitionContext extends AbstractExtensibleContext {
Node definitionNode
WorkflowDefinitionContext(JobManagement jobManagement, Item item) {
super(jobManagement, item)
}
/**
* Defines a Groovy CPS DSL definition.
*/
@RequiresPlugin(id = 'workflow-cps', minimumVersion = '2.29')
void cps(@DslContext(CpsContext) Closure cpsClosure) {
CpsContext context = new CpsContext()
ContextHelper.executeInContext(cpsClosure, context)
definitionNode = new NodeBuilder().'definition'(class: 'org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition') {
script(context.script ?: '')
sandbox(context.sandbox)
}
}
/**
* Loads a pipeline script from SCM.
*
* @since 1.44
*/
@RequiresPlugin(id = 'workflow-cps', minimumVersion = '2.29')
void cpsScm(@DslContext(CpsScmContext) Closure cpsScmClosure) {
CpsScmContext context = new CpsScmContext(jobManagement, item)
ContextHelper.executeInContext(cpsScmClosure, context)
Preconditions.checkState(!context.scmContext.scmNodes.empty, 'SCM must be specified')
Preconditions.checkState(context.scmContext.scmNodes.size() == 1, 'only one SCM can be specified')
definitionNode = new NodeBuilder().
definition(class: 'org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition') {
scriptPath(context.scriptPath)
lightweight(context.lightweight)
}
definitionNode.children().add(context.scmContext.scmNodes[0])
}
@Override
protected void addExtensionNode(Node node) {
definitionNode = ContextHelper.toNamedNode('definition', node)
}
}