如何在jenkins脚本化管道中获取上游环境变量?
在查看文档时遇到了getBuildCauses和upstreamBuilds。谷歌搜索发现了这些函数的一些案例
def causes = currentBuild.getBuildCauses()
def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
def upstream = currentBuild.upstreamBuilds
不幸的是,没有一个实现从上游获取环境变量。有人可以演示打印上游环境变量的简单脚本化管道示例吗?非常感谢任何帮助。
def upStreamBuilds = currentBuild.upstreamBuilds
if (!upStreamBuilds.isEmpty()) {
// Only immediate upstream
Run<?,?> upstream = upStreamBuilds.get(0).getRawBuild()
def upstreamEnvVars = upstream.getEnvironment(TaskListener.NULL)
}
来自 UpstreamCause 的 Javadoc:
getUpstreamRun
@CheckForNull
public Run<?,?> getUpstreamRun()
Since: 1.505
这可能会返回代表触发您的运行的
Run
的对象(其 UpstreamCause 也是如此)。
来自 Run 的 Javadoc:
EnvVars getEnvironment()
Deprecated.
as of 1.305 use getEnvironment(TaskListener)
-----
EnvVars getEnvironment(TaskListener listener)
Returns the map that contains environmental variables to be used
for launching processes for this build.
-----
Map<String,String> getEnvVars()
Deprecated.
as of 1.292 Use getEnvironment(TaskListener) instead.
希望这可以帮助您开始。