Jenkins 插件看不到使用 withEnv 工作流步骤设置的变量

Jenkins plugin cannot see variables set using the withEnv workflow step

我希望 gradle 插件获取在 withEnv 步骤(或其他包装器类型)中设置的环境变量。当我使用 sh 步骤调用 gradle 时,找到了变量,但是当我使用 gradle 插件时却找不到。

gradle 插件执行与此等效的操作:

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

run.getEnvironment() 的 javadoc 指出:

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.

如果我调试插件,我发现环境中传递给 gradle 调用的变量不到十几个,其中 none 个变量 withEnv 应该是提供。据我所知,sh 步骤使用了一个完全不同的扩展点,并且直接给出了一个似乎更完整的 EnvVars 实例。我相当确定问题不在 withEnv 中,但我不知道如何修复 gradle 插件。

我是不是打错电话了?或者可能是错误的扩展点?

请勿拨打Run.getEnvironment。而是使用传递给 SimpleBuildStep.perform.

EnvVars