diff --git a/core/src/main/java/hudson/Launcher.java b/core/src/main/java/hudson/Launcher.java index dbbf7b759c..ff84aa9373 100644 --- a/core/src/main/java/hudson/Launcher.java +++ b/core/src/main/java/hudson/Launcher.java @@ -51,6 +51,7 @@ import java.util.Arrays; import java.util.Map; import java.util.List; import java.util.ArrayList; +import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; @@ -690,6 +691,52 @@ public abstract class Launcher { }; } + /** + * Returns a decorated {@link Launcher} that automatically adds the specified environment + * variables. + * + * Those that are specified in {@link ProcStarter#envs(String...)} will take precedence over + * what's specified here. + * + * @since 1.488 + */ + public final Launcher decorateByEnv(EnvVars _env) { + final EnvVars env = new EnvVars(_env); + final Launcher outer = this; + return new Launcher(outer) { + @Override + public Proc launch(ProcStarter starter) throws IOException { + EnvVars e = new EnvVars(env); + if (starter.envs!=null) { + for (int i=0; i i : e.entrySet()) { + r[idx++] = i.getKey(); + r[idx++] = i.getValue(); + } + + starter.envs = r; + return outer.launch(starter); + } + + @Override + public Channel launchChannel(String[] cmd, OutputStream out, FilePath workDir, Map envVars) throws IOException, InterruptedException { + EnvVars e = new EnvVars(env); + e.putAll(envVars); + return outer.launchChannel(cmd,out,workDir,e); + } + + @Override + public void kill(Map modelEnvVars) throws IOException, InterruptedException { + outer.kill(modelEnvVars); + } + }; + } + /** * {@link Launcher} that launches process locally. */