提交 6588f429 编写于 作者: K Kohsuke Kawaguchi

[FIXED JENKINS-19042]

Added a new overloaded version that works on a project.

Updated CoreEnvironmentContributor accordingly.
上级 93d027bc
......@@ -63,6 +63,9 @@ Upcoming changes</a>
<div id="rc" style="display:none;"><!--=BEGIN=-->
<h3><a name=v1.526>What's new in 1.526</a> <!--=DATE=--></h3>
<ul class=image>
<li class=rfe>
Improved <tt>EnvironmentContributor</tt> to support project-level insertion.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-19042">issue 19042</a>)
<li class=rfe>
Report an user friendly error page if a deletion of a build fails.
(<a href="https://github.com/jenkinsci/jenkins/pull/827">pull request 827</a>)
......
......@@ -73,6 +73,11 @@ public abstract class EnvironmentContributor implements ExtensionPoint {
* This method gets invoked concurrently for multiple {@link Run}s that are being built at the same time,
* so it must be concurrent-safe.
*
* <p>
* When building environment variables for a build, Jenkins will also invoke
* {@link #buildEnvironmentFor(Job, EnvVars, TaskListener)}. This method only needs to add
* variables that are scoped to builds.
*
* @param r
* Build that's being performed. Never null.
* @param envs
......@@ -81,7 +86,29 @@ public abstract class EnvironmentContributor implements ExtensionPoint {
* @param listener
* Connected to the build console. Can be used to report errors. Never null.
*/
public abstract void buildEnvironmentFor(Run r, EnvVars envs, TaskListener listener) throws IOException, InterruptedException;
public void buildEnvironmentFor(Run r, EnvVars envs, TaskListener listener) throws IOException, InterruptedException {}
/**
* Contributes environment variables used for a job.
*
* <p>
* This method can be called repeatedly for the same {@link Job}, thus
* the computation of this method needs to be efficient.
*
* <p>
* This method gets invoked concurrently for multiple {@link Job}s,
* so it must be concurrent-safe.
*
* @param j
* Job for which some activities are launched.
* @param envs
* Partially built environment variable map. Implementation of this method is expected to
* add additional variables here. Never null.
* @param listener
* Connected to the build console. Can be used to report errors. Never null.
* @since 1.527
*/
public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) throws IOException, InterruptedException {}
/**
* Returns all the registered {@link EnvironmentContributor}s.
......
......@@ -369,6 +369,11 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
// see http://www.nabble.com/Run-Job-with-JDK-1.4.2-tf4468601.html
env.put("CLASSPATH","");
// apply them in a reverse order so that higher ordinal ones can modify values added by lower ordinal ones
for (EnvironmentContributor ec : EnvironmentContributor.all().reverseView())
ec.buildEnvironmentFor(this,env,listener);
return env;
}
......
......@@ -6,6 +6,7 @@ import hudson.Util;
import hudson.model.Computer;
import hudson.model.EnvironmentContributor;
import hudson.model.Executor;
import hudson.model.Job;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
......@@ -23,24 +24,32 @@ import java.io.IOException;
public class CoreEnvironmentContributor extends EnvironmentContributor {
@Override
public void buildEnvironmentFor(Run r, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
env.put("BUILD_DISPLAY_NAME",r.getDisplayName());
Jenkins j = Jenkins.getInstance();
String rootUrl = j.getRootUrl();
if(rootUrl!=null) {
env.put("BUILD_URL", rootUrl+r.getUrl());
}
}
@Override
public void buildEnvironmentFor(Job j, EnvVars env, TaskListener listener) throws IOException, InterruptedException {
Computer c = Computer.currentComputer();
if (c!=null){
EnvVars compEnv = c.getEnvironment().overrideAll(env);
env.putAll(compEnv);
}
env.put("BUILD_DISPLAY_NAME",r.getDisplayName());
Jenkins j = Jenkins.getInstance();
String rootUrl = j.getRootUrl();
Jenkins jenkins = Jenkins.getInstance();
String rootUrl = jenkins.getRootUrl();
if(rootUrl!=null) {
env.put("JENKINS_URL", rootUrl);
env.put("HUDSON_URL", rootUrl); // Legacy compatibility
env.put("BUILD_URL", rootUrl+r.getUrl());
env.put("JOB_URL", rootUrl+r.getParent().getUrl());
env.put("JOB_URL", rootUrl+j.getUrl());
}
String root = j.getRootDir().getPath();
String root = jenkins.getRootDir().getPath();
env.put("JENKINS_HOME", root);
env.put("HUDSON_HOME", root); // legacy compatibility
......
package hudson.model
import hudson.EnvVars
import org.junit.Rule
import org.junit.Test
import org.jvnet.hudson.test.CaptureEnvironmentBuilder
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.TestExtension
/**
*
*
* @author Kohsuke Kawaguchi
*/
class EnvironmentContributorTest {
@Rule
public JenkinsRule j = new JenkinsRule()
/**
* Makes sure that the project-scoped environment variables are getting consulted.
*/
@Test
public void testProjectScoped() {
def p = j.createFreeStyleProject()
def c = new CaptureEnvironmentBuilder()
p.buildersList.add(c)
p.description = "Issac Newton";
j.assertBuildStatusSuccess(p.scheduleBuild2(0))
assert c.envVars["ABC"]=="Issac Newton";
assert c.envVars["NODE_NAME"]=="master";
}
@TestExtension("testProjectScoped")
public static class JobScopedInjection extends EnvironmentContributor {
@Override
void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) {
envs.put("ABC",j.description)
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册