Skip to content

Commit

Permalink
[FIXED JENKINS-19042]
Browse files Browse the repository at this point in the history
Added a new overloaded version that works on a project.

Updated CoreEnvironmentContributor accordingly.
  • Loading branch information
kohsuke committed Aug 1, 2013
1 parent 93d027b commit 6588f42
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -63,6 +63,9 @@
<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>)
Expand Down
29 changes: 28 additions & 1 deletion core/src/main/java/hudson/model/EnvironmentContributor.java
Expand Up @@ -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
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/hudson/model/Job.java
Expand Up @@ -369,6 +369,11 @@ public EnvVars getEnvironment(Node node, TaskListener listener) throws IOExcepti
// 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;
}

Expand Down
23 changes: 16 additions & 7 deletions core/src/main/java/jenkins/model/CoreEnvironmentContributor.java
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -23,24 +24,32 @@
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

Expand Down
@@ -0,0 +1,41 @@
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)
}
}
}

1 comment on commit 6588f42

@Vlatombe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit causes some polling failures with the clearcase plugin. I opened JENKINS-19307 about it.

Please sign in to comment.