Jenkins plugin cannot see variables set using the withEnv workflow step

1.2k views Asked by At

I want the gradle plugin to pick up environment variables that are set in a withEnv step (or other wrapper-types). When I invoke gradle using a sh step the variable is found, but when I use the gradle plugin it is not.

The gradle plugin performs the equivalent of this:

EnvVars env = run.getEnvironment(taskListener);
launcher.launch().cmds(args).envs(env).stdout(gca)
                        .pwd(rootLauncher).join();

The javadoc for run.getEnvironment() states:

Returns the map that contains environmental variables to be used for launching processes for this build. BuildSteps that invoke external processes should use this. This allows BuildWrappers and other project configurations (such as JDK selection) to take effect.

Unlike earlier getEnvVars(), this map contains the whole environment, not just the overrides, so one can introspect values to change its behavior.

If I debug the plugin I see that there are less than a dozen variables in the environment passed to the gradle invocation, none of which are the variables withEnv should be providing. To the best I could tell, the sh step uses a completely different extension point, and is straight up given an instance of EnvVars that appears to be much more complete. I'm fairly certain the problem isn't in withEnv, but I don't see how to fix the gradle plugin.

Am I using the wrong call? Or perhaps the wrong extension point?

1

There are 1 answers

2
Jesse Glick On BEST ANSWER

Do not call Run.getEnvironment. Rather use the EnvVars passed to SimpleBuildStep.perform.