Skip to content

Commit

Permalink
Fixed partially JENKINS-10847
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Aug 30, 2011
1 parent 4af7734 commit 899abfa
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 45 deletions.
Expand Up @@ -8,11 +8,9 @@
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.remoting.Callable;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.envinject.service.EnvInjectMasterEnvVarsSetter;
import org.jenkinsci.plugins.envinject.service.EnvInjectScriptExecutorService;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -49,14 +47,15 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, final Bui

final FilePath ws = build.getWorkspace();

//Add the current system env vars
ws.act(new Callable<Void, Throwable>() {

public Void call() throws Throwable {
resultVariables.putAll(EnvVars.masterEnvVars);
return null;
}
});
//Fix JENKINS-10847 postponed
// //Add the current system env vars
// ws.act(new Callable<Void, Throwable>() {
//
// public Void call() throws Throwable {
// resultVariables.putAll(EnvVars.masterEnvVars);
// return null;
// }
// });

//Always keep build variables (such as parameter variables).
resultVariables.putAll(getAndAddBuildVariables(build));
Expand All @@ -73,8 +72,9 @@ public Void call() throws Throwable {
//Resolve vars each other
EnvVars.resolve(resultVariables);

//Fix JENKINS-10847 postponed
//Set the new build variables map
build.getWorkspace().act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));
//build.getWorkspace().act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));

//Add or get the existing action to add new env vars
addEnvVarsToEnvInjectBuildAction(build, resultVariables);
Expand All @@ -83,13 +83,13 @@ public Void call() throws Throwable {
build.setResult(Result.FAILURE);
}

return new EnvironmentImpl();
}
return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(resultVariables);

class EnvironmentImpl extends Environment {
@Override
public void buildEnvVars(Map<String, String> env) {
}
}
};
}

private Map<String, String> getAndAddBuildVariables(AbstractBuild build) {
Expand Down
Expand Up @@ -4,15 +4,10 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.remoting.Callable;
import hudson.model.*;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.envinject.service.EnvInjectMasterEnvVarsSetter;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -46,14 +41,15 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

FilePath ws = build.getWorkspace();

//Fix JENKINS-10847 postponed
/*
//Add the current system env vars
resultVariables.putAll(ws.act(new Callable<Map<String, String>, Throwable>() {

public Map<String, String> call() throws Throwable {
return EnvVars.masterEnvVars;
}
}));

*/
//Always keep build variables (such as parameter variables).
resultVariables.putAll(getAndAddBuildVariables(build));

Expand All @@ -65,8 +61,26 @@ public Map<String, String> call() throws Throwable {
//Resolve vars each other
EnvVars.resolve(resultVariables);

//Fix JENKINS-10847 postponed
//Set the new build variables map
ws.act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));
//ws.act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));
build.addAction(new EnvironmentContributingAction() {
public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
env.putAll(resultVariables);
}

public String getIconFileName() {
return null;
}

public String getDisplayName() {
return null;
}

public String getUrlName() {
return null;
}
});

//Add or get the existing action to add new env vars
addEnvVarsToEnvInjectBuildAction(build, resultVariables);
Expand Down
Expand Up @@ -16,7 +16,7 @@ public class EnvInjectJobProperty<T extends Job<?, ?>> extends JobProperty<T> {

private boolean on;

private boolean keepSystemVariables;
private transient boolean keepSystemVariables;

private boolean keepBuildVariables;

Expand Down Expand Up @@ -85,7 +85,7 @@ public EnvInjectJobProperty newInstance(StaplerRequest req, JSONObject formData)
envInjectJobProperty.setInfo(info);
envInjectJobProperty.setOn(true);
if (onObject instanceof JSONObject) {
envInjectJobProperty.setKeepSystemVariables(((JSONObject) onObject).getBoolean("keepSystemVariables"));
//envInjectJobProperty.setKeepSystemVariables(((JSONObject) onObject).getBoolean("keepSystemVariables"));
envInjectJobProperty.setKeepBuildVariables(((JSONObject) onObject).getBoolean("keepBuildVariables"));
return envInjectJobProperty;
}
Expand Down
Expand Up @@ -9,7 +9,6 @@
import hudson.tasks.BuildWrapper;
import hudson.tasks.Builder;
import hudson.util.DescribableList;
import hudson.util.LogTaskListener;
import org.jenkinsci.plugins.envinject.service.EnvInjectMasterEnvVarsSetter;
import org.jenkinsci.plugins.envinject.service.EnvInjectScriptExecutorService;
import org.jenkinsci.plugins.envinject.service.PropertiesVariablesRetriever;
Expand All @@ -19,7 +18,6 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand All @@ -33,21 +31,21 @@ public class EnvInjectListener extends RunListener<Run> implements Serializable
@Override
public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {

final Map<String, String> resultVariables = new HashMap<String, String>();
if (isEnvInjectJobPropertyActive(build)) {

Map<String, String> resultVariables = new HashMap<String, String>();
try {

EnvInjectJobProperty envInjectJobProperty = getEnvInjectJobProperty(build);
assert envInjectJobProperty != null;
EnvInjectJobPropertyInfo info = envInjectJobProperty.getInfo();
assert envInjectJobProperty != null && envInjectJobProperty.isOn();

//Fix JENKINS-10847 postponed
//Add system environment variables if needed
if (envInjectJobProperty.isKeepSystemVariables()) {
//The new envMap wins
resultVariables.putAll(build.getEnvironment(new LogTaskListener(LOG, Level.ALL)));
}
// if (envInjectJobProperty.isKeepSystemVariables()) {
// //The new envMap wins
// resultVariables.putAll(build.getEnvironment(new LogTaskListener(LOG, Level.ALL)));
// }

//Add build variables (such as parameter variables).
if (envInjectJobProperty.isKeepBuildVariables()) {
Expand All @@ -61,8 +59,9 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
//Resolves vars each other
EnvVars.resolve(resultVariables);

//Fix JENKINS-10847 postponed
//Set the new computer variables
Computer.currentComputer().getNode().getRootPath().act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));
// Computer.currentComputer().getNode().getRootPath().act(new EnvInjectMasterEnvVarsSetter(new EnvVars(resultVariables)));

//Add a display action
build.addAction(new EnvInjectAction(resultVariables));
Expand All @@ -76,6 +75,12 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
}
}
return new Environment() {

@Override
public void buildEnvVars(Map<String, String> env) {
env.clear();
env.putAll(resultVariables);
}
};
}

Expand Down
Expand Up @@ -6,11 +6,13 @@
checked="${instance.on}"
help="/plugin/envinject/help.html">

<!--
<f:entry field="keepSystemVariables" title="${%Keep System Environment Variables}">
<f:checkbox
name="keepSystemVariables"
checked="${instance.keepSystemVariables}" default="${false}"/>
</f:entry>
-->

<f:entry field="keepBuildVariables" title="${%Keep Jenkins Build Variables}">
<f:checkbox
Expand Down
3 changes: 0 additions & 3 deletions src/main/webapp/help-buildWrapper.html
Expand Up @@ -2,8 +2,5 @@
<p>
Add or override environment variables to the whole build process.<br/>
The variables will be injected after a SCM checkout (workspace elements are available).<br/>
If you don't want to depend on the default environment variables (environment variables inherited by the Jenkins
process), we suggest using 'Prepare an environment for the job' in the top of the configuration file to set a
clean environment.
</p>
</div>
6 changes: 1 addition & 5 deletions src/main/webapp/help.html
Expand Up @@ -4,10 +4,6 @@
for the Job by defining environment variables and execute a script (a setup script).<br/>
All these actions will be executed before a SCM checkout.<br/>
By default, after the set up, only injected variables (with Jenkins variables) will be available in the build
scripts, in
the Jenkins post-actions, and so on. <br/>
Default inherited environment variables (inherited variables by the Jenkins process which often contains
machine-specific information, like PATH, TIMEZONE, etc.) will not be available unless 'Keep system variables' is
checked.
scripts, in the Jenkins post-actions, and so on. <br/>
</p>
</div>

0 comments on commit 899abfa

Please sign in to comment.