11package com.lesfurets.jenkins.unit.declarative
22
33
4- import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.createComponent
54import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
6- import static groovy.lang.Closure.*
5+ import static groovy.lang.Closure.DELEGATE_FIRST
76
8- class StageDeclaration {
7+ class StageDeclaration extends GenericPipelineDeclaration {
98
10- AgentDeclaration agent
11- Closure environment
129 String name
1310 Closure steps
1411 WhenDeclaration when
1512 ParallelDeclaration parallel
16- PostDeclaration post
1713 boolean failFast = false
1814 List<Closure > options = []
19- Map<String , StageDeclaration > stages = [:]
2015
2116 StageDeclaration (String name ) {
2217 this . name = name
2318 }
2419
25- def agent (Object o ) {
26- this . agent = new AgentDeclaration (). with { it. label = o; it }
27- }
28-
29- def agent (@DelegatesTo (strategy = DELEGATE_FIRST , value = AgentDeclaration ) Closure closure ) {
30- this . agent = createComponent(AgentDeclaration , closure)
31- }
32-
33- def environment (Closure closure ) {
34- this . environment = closure
35- }
36-
37- def post (@DelegatesTo (strategy = DELEGATE_FIRST , value = PostDeclaration ) Closure closure ) {
38- this . post = createComponent(PostDeclaration , closure)
39- }
40-
4120 def steps (Closure closure ) {
4221 this . steps = closure
4322 }
4423
45- def stages (@DelegatesTo (DeclarativePipeline ) Closure closure ) {
46- closure. call()
47- }
48-
4924 def failFast (boolean failFast ) {
5025 this . failFast = failFast
5126 }
@@ -63,8 +38,6 @@ class StageDeclaration {
6338 }
6439
6540 def execute (Object delegate ) {
66- Map envValuestoRestore = [:]
67-
6841 String name = this . name
6942 this . options. each {
7043 executeWith(delegate, it)
@@ -79,26 +52,16 @@ class StageDeclaration {
7952 }
8053
8154 if (! when || when. execute(delegate)) {
82- // Set environment for stage
55+ Map envValuestoRestore = [:]
56+
57+ // set environment
8358 if (this . environment) {
84- Binding subBinding = new Binding ()
85- subBinding. metaClass. invokeMissingProperty = { propertyName ->
86- delegate. getProperty(propertyName)
87- }
88- subBinding. metaClass. setProperty = { String propertyName , Object newValue ->
89- if (delegate. hasProperty(propertyName)){
90- envValuestoRestore. put(propertyName, delegate. getProperty(propertyName))
91- }
92- (delegate. env as Map ). put(propertyName, newValue)
93- }
94- def envClosure = this . environment. rehydrate(subBinding, delegate, this )
95- envClosure. resolveStrategy = DELEGATE_FIRST
96- envClosure. call()
59+ envValuestoRestore = initEnvironment(this . environment, delegate)
9760 }
9861
9962 // TODO handle credentials
10063 this . stages. entrySet(). forEach { stageEntry ->
101- stageEntry. getValue() . execute(delegate)
64+ stageEntry. value . execute(delegate)
10265 }
10366 if (steps) {
10467 Closure stageBody = { agent?. execute(delegate) } >> steps. rehydrate(delegate, this , this )
@@ -108,13 +71,10 @@ class StageDeclaration {
10871 if (post) {
10972 this . post. execute(delegate)
11073 }
74+ resetEnvironment(envValuestoRestore, delegate)
11175 } else {
11276 executeWith(delegate, { echo " Skipping stage $name " })
11377 }
114- envValuestoRestore. entrySet(). forEach { entry ->
115- def envMap = delegate. env as Map
116- envMap. put(entry. getKey(), entry. getValue())
117- }
11878 }
11979
12080}
0 commit comments