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

Commit

Permalink
Merge pull request #145 from recena/master
Browse files Browse the repository at this point in the history
[JENKINS-28131] - Pass NODE_NAME into node{}
  • Loading branch information
jglick committed Jun 30, 2015
2 parents b127fd4 + 36af71a commit 06cb328
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,7 @@ Only noting significant user-visible or major API changes, not internal code cle

## 1.9 (upcoming)

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

Expand Down
@@ -0,0 +1,64 @@
/*
* The MIT License
*
* Copyright (c) 2015 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

/**
* Verifies that specific environment variables are available.
*
*/
public class EnvWorkflowTest {

@Rule public JenkinsRule r = new JenkinsRule();

/**
* Verifies if NODE_NAME environment variable is available on a slave node and on master.
*
* @throws Exception
*/
@Test public void areAvailable() throws Exception {
r.createSlave("node-test", null, null);
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "workflow-test");

p.setDefinition(new CpsFlowDefinition(
"node('master') {\n" +
" echo \"My name on master is ${env.NODE_NAME}\"\n" +
"}\n"
));
r.assertLogContains("My name on master is master", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));

p.setDefinition(new CpsFlowDefinition(
"node('node-test') {\n" +
" echo \"My name on a slave is ${env.NODE_NAME}\"\n" +
"}\n"
));
r.assertLogContains("My name on a slave is node-test", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}
}
Expand Up @@ -28,7 +28,6 @@ node {
</p>
<ul>
<li><code>EXECUTOR_NUMBER</code></li>
<li><code>NODE_NAME</code></li>
<li><code>NODE_LABELS</code></li>
<li><code>WORKSPACE</code></li>
<li>SCM-specific variables such as <code>SVN_REVISION</code></li>
Expand Down
Expand Up @@ -35,6 +35,7 @@
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import jenkins.model.Jenkins.MasterComputer;
import jenkins.model.queue.AsynchronousExecution;
import jenkins.util.Timer;
import org.acegisecurity.Authentication;
Expand Down Expand Up @@ -417,9 +418,18 @@ private final class PlaceholderExecutable implements ContinuableExecutable {
cookie = UUID.randomUUID().toString();
// Switches the label to a self-label, so if the executable is killed and restarted via ExecutorPickle, it will run on the same node:
label = computer.getName();

EnvVars env = computer.getEnvironment();
env.overrideAll(computer.buildEnvironment(listener));
env.put(COOKIE_VAR, cookie);
// TODO: Copied from https://github.com/jenkinsci/jenkins/blob/9c443c8d5bafd63fce574f6d0cf400cd8fe1f124/core/src/main/java/jenkins/model/CoreEnvironmentContributor.java#L59
// TODO: It is interesting to add NODE_LABELS and EXECUTOR_NUMBER
if (exec.getOwner() instanceof MasterComputer) {
env.put("NODE_NAME", "master");
} else {
env.put("NODE_NAME", label);
}

synchronized (runningTasks) {
runningTasks.put(cookie, new RunningTask(context));
}
Expand Down

0 comments on commit 06cb328

Please sign in to comment.