Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 06cb328

Browse files
committedJun 30, 2015
Merge pull request #145 from recena/master
[JENKINS-28131] - Pass NODE_NAME into node{}
2 parents b127fd4 + 36af71a commit 06cb328

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
 

‎CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Only noting significant user-visible or major API changes, not internal code cle
44

55
## 1.9 (upcoming)
66

7+
* [JENKINS-28131](https://issues.jenkins-ci.org/browse/JENKINS-28131): pass NODE_NAME into node{}.
78
* [JENKINS-26860](https://issues.jenkins-ci.org/browse/JENKINS-26860): added _Execute concurrent builds if necessary_ option for Workflow projects.
89
* [JENKINS-28756](https://issues.jenkins-ci.org/browse/JENKINS-28756): dropdown for _General SCM_ step incorrectly listed SCMs not compatible with Workflow.
910

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2015 CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jenkinsci.plugins.workflow;
26+
27+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
28+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.jvnet.hudson.test.JenkinsRule;
32+
33+
/**
34+
* Verifies that specific environment variables are available.
35+
*
36+
*/
37+
public class EnvWorkflowTest {
38+
39+
@Rule public JenkinsRule r = new JenkinsRule();
40+
41+
/**
42+
* Verifies if NODE_NAME environment variable is available on a slave node and on master.
43+
*
44+
* @throws Exception
45+
*/
46+
@Test public void areAvailable() throws Exception {
47+
r.createSlave("node-test", null, null);
48+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "workflow-test");
49+
50+
p.setDefinition(new CpsFlowDefinition(
51+
"node('master') {\n" +
52+
" echo \"My name on master is ${env.NODE_NAME}\"\n" +
53+
"}\n"
54+
));
55+
r.assertLogContains("My name on master is master", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
56+
57+
p.setDefinition(new CpsFlowDefinition(
58+
"node('node-test') {\n" +
59+
" echo \"My name on a slave is ${env.NODE_NAME}\"\n" +
60+
"}\n"
61+
));
62+
r.assertLogContains("My name on a slave is node-test", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
63+
}
64+
}

‎cps/src/main/resources/org/jenkinsci/plugins/workflow/cps/EnvActionImpl/Binder/help.jelly

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ node {
2828
</p>
2929
<ul>
3030
<li><code>EXECUTOR_NUMBER</code></li>
31-
<li><code>NODE_NAME</code></li>
3231
<li><code>NODE_LABELS</code></li>
3332
<li><code>WORKSPACE</code></li>
3433
<li>SCM-specific variables such as <code>SVN_REVISION</code></li>

‎support/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.logging.Logger;
3636
import javax.annotation.CheckForNull;
3737
import jenkins.model.Jenkins;
38+
import jenkins.model.Jenkins.MasterComputer;
3839
import jenkins.model.queue.AsynchronousExecution;
3940
import jenkins.util.Timer;
4041
import org.acegisecurity.Authentication;
@@ -417,9 +418,18 @@ private final class PlaceholderExecutable implements ContinuableExecutable {
417418
cookie = UUID.randomUUID().toString();
418419
// Switches the label to a self-label, so if the executable is killed and restarted via ExecutorPickle, it will run on the same node:
419420
label = computer.getName();
421+
420422
EnvVars env = computer.getEnvironment();
421423
env.overrideAll(computer.buildEnvironment(listener));
422424
env.put(COOKIE_VAR, cookie);
425+
// TODO: Copied from https://github.com/jenkinsci/jenkins/blob/9c443c8d5bafd63fce574f6d0cf400cd8fe1f124/core/src/main/java/jenkins/model/CoreEnvironmentContributor.java#L59
426+
// TODO: It is interesting to add NODE_LABELS and EXECUTOR_NUMBER
427+
if (exec.getOwner() instanceof MasterComputer) {
428+
env.put("NODE_NAME", "master");
429+
} else {
430+
env.put("NODE_NAME", label);
431+
}
432+
423433
synchronized (runningTasks) {
424434
runningTasks.put(cookie, new RunningTask(context));
425435
}

0 commit comments

Comments
 (0)
This repository has been archived.