1
1
package org .jenkins .plugins .lockableresources ;
2
2
3
+ import java .io .IOException ;
3
4
import java .util .ArrayList ;
4
5
import java .util .List ;
5
6
import java .util .logging .Level ;
8
9
import org .jenkins .plugins .lockableresources .queue .LockableResourcesStruct ;
9
10
import org .jenkinsci .plugins .workflow .steps .AbstractStepExecutionImpl ;
10
11
import org .jenkinsci .plugins .workflow .steps .BodyExecutionCallback ;
12
+ import org .jenkinsci .plugins .workflow .steps .BodyInvoker ;
13
+ import org .jenkinsci .plugins .workflow .steps .EnvironmentExpander ;
11
14
import org .jenkinsci .plugins .workflow .steps .StepContext ;
12
15
import org .jenkinsci .plugins .workflow .steps .StepContextParameter ;
13
16
17
+ import com .google .common .base .Joiner ;
14
18
import com .google .inject .Inject ;
15
19
20
+ import hudson .EnvVars ;
16
21
import hudson .model .Run ;
17
22
import hudson .model .TaskListener ;
18
23
19
24
public class LockStepExecution extends AbstractStepExecutionImpl {
20
25
21
- @ Inject (optional = true )
26
+ private static final Joiner COMMA_JOINER = Joiner .on (',' );
27
+
28
+ @ Inject (optional = true )
22
29
private LockStep step ;
23
30
24
31
@ StepContextParameter
@@ -44,14 +51,14 @@ public boolean start() throws Exception {
44
51
LockableResourcesStruct resourceHolder = new LockableResourcesStruct (resources , step .label , step .quantity );
45
52
// determine if there are enough resources available to proceed
46
53
List <LockableResource > available = LockableResourcesManager .get ().checkResourcesAvailability (resourceHolder , listener .getLogger (), null );
47
- if (available == null || !LockableResourcesManager .get ().lock (available , run , getContext (), step .toString (), step .inversePrecedence )) {
54
+ if (available == null || !LockableResourcesManager .get ().lock (available , run , getContext (), step .toString (), step .variable , step . inversePrecedence )) {
48
55
listener .getLogger ().println ("[" + step + "] is locked, waiting..." );
49
56
LockableResourcesManager .get ().queueContext (getContext (), resourceHolder , step .toString ());
50
57
} // proceed is called inside lock if execution is possible
51
58
return false ;
52
59
}
53
60
54
- public static void proceed (List <String > resourcenames , StepContext context , String resourceDescription , boolean inversePrecedence ) {
61
+ public static void proceed (final List <String > resourcenames , StepContext context , String resourceDescription , final String variable , boolean inversePrecedence ) {
55
62
Run <?, ?> r = null ;
56
63
try {
57
64
r = context .get (Run .class );
@@ -62,10 +69,26 @@ public static void proceed(List<String> resourcenames, StepContext context, Stri
62
69
}
63
70
64
71
LOGGER .finest ("Lock acquired on [" + resourceDescription + "] by " + r .getExternalizableId ());
65
- context .newBodyInvoker ().
66
- withCallback (new Callback (resourcenames , resourceDescription , inversePrecedence )).
67
- withDisplayName (null ).
68
- start ();
72
+ try {
73
+ BodyInvoker bodyInvoker = context .newBodyInvoker ().
74
+ withCallback (new Callback (resourcenames , resourceDescription , inversePrecedence )).
75
+ withDisplayName (null );
76
+ if (variable != null && variable .length ()>0 )
77
+ // set the variable for the duration of the block
78
+ bodyInvoker .withContext (EnvironmentExpander .merge (context .get (EnvironmentExpander .class ), new EnvironmentExpander () {
79
+ @ Override
80
+ public void expand (EnvVars env ) throws IOException , InterruptedException {
81
+ final String resources = COMMA_JOINER .join (resourcenames );
82
+ LOGGER .finest ("Setting [" + variable + "] to [" + resources
83
+ + "] for the duration of the block" );
84
+
85
+ env .override (variable , resources );
86
+ }
87
+ }));
88
+ bodyInvoker .start ();
89
+ } catch (IOException | InterruptedException e ) {
90
+ throw new RuntimeException (e );
91
+ }
69
92
}
70
93
71
94
private static final class Callback extends BodyExecutionCallback .TailCall {
0 commit comments