Skip to content

Commit bdaca59

Browse files
committed
Add support of default core substitution variables and the computer environment
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
1 parent 93a7535 commit bdaca59

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/main/java/hudson/plugins/perforce/utils/MacroStringHelper.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ private static String substituteParametersNoCheck (
224224

225225
// Prepare the substitution container and substitute vars
226226
Map<String, String> substitutions = new HashMap<String, String>();
227+
getDefaultCoreSubstitutions(substitutions);
227228
NodeSubstitutionHelper.getDefaultNodeSubstitutions(instance, node, substitutions);
228229
if (project != null) {
229230
getDefaultSubstitutions(project, substitutions);
@@ -295,6 +296,19 @@ private static String getSafeJobName(@Nonnull AbstractProject project) {
295296
return project.getFullName().replace('/', '-').replace('=', '-').replace(',', '-');
296297
}
297298

299+
/**
300+
* Gets variables of {@link Hudson} instance.
301+
*/
302+
private static void getDefaultCoreSubstitutions(@Nonnull Map<String, String> env) {
303+
String rootUrl = Hudson.getInstance().getRootUrl();
304+
if (rootUrl != null) {
305+
env.put("JENKINS_URL", rootUrl);
306+
env.put("HUDSON_URL", rootUrl); // Legacy compatibility
307+
}
308+
env.put("JENKINS_HOME", Hudson.getInstance().getRootDir().getPath());
309+
env.put("HUDSON_HOME", Hudson.getInstance().getRootDir().getPath()); // legacy compatibility
310+
}
311+
298312
/**
299313
* Substitutes {@link PerforceSCM}-specific variables.
300314
* In order to retain the backward compatibility, the input subst map's
@@ -316,6 +330,10 @@ private static void getDefaultBuildSubstitutions(
316330
subst.put("BUILD_TAG", hudsonName + "-" + build.getProject().getName() + "-" + String.valueOf(build.getNumber()));
317331
subst.put("BUILD_ID", build.getId());
318332
subst.put("BUILD_NUMBER", String.valueOf(build.getNumber()));
333+
String rootUrl = Hudson.getInstance().getRootUrl();
334+
if (rootUrl != null) {
335+
subst.put("BUILD_URL", rootUrl + build.getUrl());
336+
}
319337
}
320338

321339

@@ -324,6 +342,11 @@ private static void getDefaultSubstitutions(
324342
@Nonnull Map<String, String> subst) {
325343

326344
subst.put("JOB_NAME", MacroStringHelper.getSafeJobName(project));
345+
String rootUrl = Hudson.getInstance().getRootUrl();
346+
if (rootUrl != null) {
347+
subst.put("JOB_URL", rootUrl + project.getUrl());
348+
}
349+
327350
for (NodeProperty nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) {
328351
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
329352
subst.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars());

src/main/java/hudson/plugins/perforce/utils/NodeSubstitutionHelper.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424

2525
package hudson.plugins.perforce.utils;
2626

27+
import hudson.EnvVars;
28+
import hudson.Util;
2729
import hudson.model.Computer;
30+
import hudson.model.Executor;
2831
import hudson.model.Hudson;
2932
import hudson.model.Node;
3033
import hudson.plugins.perforce.PerforceSCM;
3134
import hudson.slaves.EnvironmentVariablesNodeProperty;
3235
import hudson.slaves.NodeProperty;
36+
import java.io.IOException;
3337
import java.util.Map;
3438
import java.util.logging.Level;
3539
import java.util.logging.Logger;
@@ -56,13 +60,14 @@ public class NodeSubstitutionHelper {
5660
@Nonnull PerforceSCM instance,
5761
@CheckForNull Node node,
5862
@Nonnull Map<String, String> target) {
63+
5964
// Global node properties
6065
for (NodeProperty globalNodeProperty: Hudson.getInstance().getGlobalNodeProperties()) {
6166
if (globalNodeProperty instanceof EnvironmentVariablesNodeProperty) {
6267
target.putAll(((EnvironmentVariablesNodeProperty)globalNodeProperty).getEnvVars());
6368
}
6469
}
65-
70+
6671
// Local node properties
6772
if (node != null) {
6873
for (NodeProperty nodeProperty : node.getNodeProperties()) {
@@ -71,10 +76,35 @@ public class NodeSubstitutionHelper {
7176
}
7277
}
7378

79+
final String nodeName = node.getNodeName();
80+
7481
// Push legacy variables
75-
target.put("nodename", node.getNodeName());
82+
target.put("nodename", nodeName);
7683
target.put("hostname", getHostName(node));
7784
target.put("hash", getNodeHash(node));
85+
86+
// Push modern variables
87+
target.put("NODE_NAME", nodeName.isEmpty() ? "master" : nodeName);
88+
target.put("NODE_LABELS", Util.join(node.getAssignedLabels(), " "));
89+
Thread t = Thread.currentThread();
90+
if (t instanceof Executor) {
91+
Executor e = (Executor) t;
92+
target.put("EXECUTOR_NUMBER", String.valueOf(e.getNumber()));
93+
}
94+
95+
// Get environment
96+
Computer c = node.toComputer();
97+
if (c != null) {
98+
try {
99+
EnvVars env = c.getEnvironment().overrideAll(target);
100+
target.putAll(env);
101+
} catch (IOException ex) {
102+
// Ignore exception
103+
} catch (InterruptedException ex) {
104+
// Ignore exception
105+
// TODO: Handle the exception
106+
}
107+
}
78108
}
79109
}
80110

0 commit comments

Comments
 (0)