1
1
package com .lookout .jenkins ;
2
- import hudson .EnvVars ;
3
2
import hudson .Extension ;
4
3
import hudson .FilePath ;
5
4
import hudson .Launcher ;
13
12
14
13
import java .io .ByteArrayOutputStream ;
15
14
import java .io .IOException ;
16
- import java .util .AbstractMap ;
17
- import java .util .ArrayList ;
18
15
import java .util .Arrays ;
16
+ import java .util .HashMap ;
19
17
import java .util .List ;
20
18
import java .util .Map ;
21
19
24
22
import org .kohsuke .stapler .DataBoundConstructor ;
25
23
26
24
/**
27
- *
25
+ * Runs a specific chunk of code before each build, parsing output for new environment variables.
28
26
*
29
27
* @author Jørgen P. Tjernø
30
28
*/
31
29
public class EnvironmentScript extends BuildWrapper {
32
30
private final String script ;
33
31
34
- // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
35
32
@ DataBoundConstructor
36
33
public EnvironmentScript (String script ) {
37
34
this .script = script ;
@@ -48,7 +45,7 @@ public String getScript() {
48
45
@ Override
49
46
public Environment setUp (AbstractBuild build ,
50
47
final Launcher launcher ,
51
- BuildListener listener ) throws IOException , InterruptedException {
48
+ final BuildListener listener ) throws IOException , InterruptedException {
52
49
53
50
// First we create the script in a temporary directory.
54
51
FilePath ws = build .getWorkspace ();
@@ -77,9 +74,10 @@ public Environment setUp(AbstractBuild build,
77
74
return null ;
78
75
}
79
76
77
+
80
78
// Then we parse the variables out of it. We could use java.util.Properties, but it doesn't order the properties, so expanding variables with previous variables (like a shell script expects) doesn't work.
81
79
String [] lines = commandOutput .toString ().split ("(\n |\r \n )" );
82
- final List < Map . Entry <String , String >> injectedVariables = new ArrayList < Map . Entry < String , String > >(lines .length );
80
+ final Map <String , String > envAdditions = new HashMap < String , String >(lines .length );
83
81
for (String line : lines )
84
82
{
85
83
if (line .trim ().isEmpty ()) {
@@ -90,29 +88,23 @@ public Environment setUp(AbstractBuild build,
90
88
if (keyAndValue .length < 2 ) {
91
89
listener .error ("[environment-script] Invalid line encountered, ignoring: " + line );
92
90
} else {
93
- Map .Entry <String , String > entry = new AbstractMap .SimpleEntry <String , String >(keyAndValue [0 ], keyAndValue [1 ]);
94
- listener .getLogger ().println ("[environment-script] Adding variable '" + entry .getKey () + "' with value '" + entry .getValue () + "'" );
95
- injectedVariables .add (entry );
91
+ listener .getLogger ().println ("[environment-script] Adding variable '" + keyAndValue [0 ] + "' with value '" + keyAndValue [1 ] + "'" );
92
+ envAdditions .put (keyAndValue [0 ], keyAndValue [1 ]);
96
93
}
97
94
}
98
95
99
96
return new Environment () {
100
97
@ Override
101
98
public void buildEnvVars (Map <String , String > env ) {
102
- // Here we evaluate the variables we parsed above, in order.
103
- // We expand against all the environment variables we have so far.
104
- EnvVars vars = new EnvVars (env );
105
- for (Map .Entry <String , String > variable : injectedVariables ) {
106
- env .put (variable .getKey (), vars .expand (variable .getValue ()));
107
- }
99
+ env .putAll (envAdditions );
108
100
}
109
101
};
110
102
}
111
103
112
104
// Mostly stolen from hudson.tasks.Shell.buildCommandLine.
113
105
public String [] buildCommandLine (FilePath scriptFile ) {
114
106
// Respect shebangs
115
- if (script .startsWith ("#!" )) {
107
+ if (script .startsWith ("#!" )) {
116
108
// Find first line, or just entire script if it's one line.
117
109
int end = script .indexOf ('\n' );
118
110
if (end < 0 )
0 commit comments