forked from boozallen/jenkins-templating-engine
-
Notifications
You must be signed in to change notification settings - Fork 58
/
TemplateFlowDefinition.groovy
98 lines (81 loc) · 4.03 KB
/
TemplateFlowDefinition.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
/*
Copyright 2018 Booz Allen Hamilton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.boozallen.plugins.jte.job
import hudson.Extension
import hudson.model.*
import jenkins.model.Jenkins
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution
import org.jenkinsci.plugins.workflow.flow.FlowDefinition
import org.jenkinsci.plugins.workflow.flow.FlowDefinitionDescriptor
import org.jenkinsci.plugins.workflow.flow.FlowExecution
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.job.WorkflowRun
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
import org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint
import org.jenkinsci.plugins.workflow.flow.DurabilityHintProvider
import org.jenkinsci.plugins.workflow.flow.GlobalDefaultFlowDurabilityLevel
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.*
import org.kohsuke.stapler.DataBoundConstructor
import hudson.Util
@PersistIn(JOB)
class TemplateFlowDefinition extends FlowDefinition {
private final boolean providePipelineTemplate
public String template
private final boolean providePipelineConfig
public String pipelineConfig
@DataBoundConstructor public TemplateFlowDefinition(boolean providePipelineTemplate, String template, boolean providePipelineConfig, String pipelineConfig){
this.providePipelineTemplate = providePipelineTemplate
this.template = providePipelineTemplate ? Util.fixEmptyAndTrim(template) : null
this.providePipelineConfig = providePipelineConfig
this.pipelineConfig = providePipelineConfig ? Util.fixEmptyAndTrim(pipelineConfig) : null
}
public boolean getProvidePipelineTemplate(){ return providePipelineTemplate }
public String getTemplate() { return template }
public boolean getProvidePipelineConfig(){ return providePipelineConfig }
public String getPipelineConfig(){ return pipelineConfig }
@Override
public FlowExecution create(FlowExecutionOwner handle, TaskListener listener, List<? extends Action> actions) throws Exception {
Jenkins jenkins = Jenkins.getInstance()
if (jenkins == null) {
throw new IllegalStateException("inappropriate context")
}
Queue.Executable exec = handle.getExecutable()
if (!(exec instanceof WorkflowRun)) {
throw new IllegalStateException("inappropriate context")
}
FlowDurabilityHint hint = (exec instanceof Item) ? DurabilityHintProvider.suggestedFor((Item)exec) : GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint()
return new CpsFlowExecution("template()", true, handle, hint);
}
@Extension
public static class DescriptorImpl extends FlowDefinitionDescriptor {
@Override
public String getDisplayName() {
return "Jenkins Templating Engine"
}
}
/**
* Want to display this in the r/o configuration for a branch project, but not offer it on standalone jobs or in any other context.
*/
@Extension
public static class HideMeElsewhere extends DescriptorVisibilityFilter {
@Override
public boolean filter(Object context, Descriptor descriptor) {
if (descriptor instanceof DescriptorImpl) {
return context instanceof WorkflowJob && !(((WorkflowJob) context).getParent() instanceof WorkflowMultiBranchProject)
}
return true
}
}
}