diff --git a/core/src/main/java/hudson/model/AbstractBuild.java b/core/src/main/java/hudson/model/AbstractBuild.java index d62f427289..42a46999b5 100644 --- a/core/src/main/java/hudson/model/AbstractBuild.java +++ b/core/src/main/java/hudson/model/AbstractBuild.java @@ -844,27 +844,26 @@ public abstract class AbstractBuild

,R extends Abs /** * During the build, expose the environments contributed by {@link BuildWrapper}s and others. - * + * + *

+ * Since 1.444, executor thread that's doing the build can access mutable underlying list, + * which allows the caller to add/remove environments. The recommended way of adding + * environment is through {@link BuildWrapper}, but this might be handy for build steps + * who wants to expose additional environment variables to the rest of the build. + * * @return can be empty list, but never null. Immutable. * @since 1.437 */ public EnvironmentList getEnvironments() { + Executor e = Executor.currentExecutor(); + if (e!=null && e.getCurrentExecutable()==this) { + if (buildEnvironments==null) buildEnvironments = new ArrayList(); + return new EnvironmentList(buildEnvironments); + } + return new EnvironmentList(buildEnvironments==null ? Collections.emptyList() : ImmutableList.copyOf(buildEnvironments)); } - /** - * Allows you to contribute an environment without going through {@link BuildWrapper}. - *

- * For concurrency, this method is only designed to be called from the executor that's doing a build, - * during the build. - * - * @since 1.444 - */ - public void addEnvironment(Environment env) { - if (env!=null) - buildEnvironments.add(env); - } - public Calendar due() { return getTimestamp(); } diff --git a/core/src/main/java/hudson/model/EnvironmentList.java b/core/src/main/java/hudson/model/EnvironmentList.java index 67918a3e0a..1c1b07a205 100644 --- a/core/src/main/java/hudson/model/EnvironmentList.java +++ b/core/src/main/java/hudson/model/EnvironmentList.java @@ -56,4 +56,19 @@ public final class EnvironmentList extends AbstractList { } return null; } + + @Override + public Environment set(int index, Environment element) { + return base.set(index, element); + } + + @Override + public void add(int index, Environment element) { + base.add(index, element); + } + + @Override + public Environment remove(int index) { + return base.remove(index); + } }