获取上游环境变量-Jenkins脚本化管道

时间:2020-10-15 16:18:30

标签: jenkins jenkins-pipeline

如何在jenkins脚本化管道中获取上游环境变量?

在经历documentation时遇到了getBuildCauses和上游Builds。在这些功能的某些情况下,出现了谷歌搜索

def causes = currentBuild.getBuildCauses()
def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
def upstream = currentBuild.upstreamBuilds  
不幸的是,没有一个实现从上游获得环境变量。有人可以演示打印上游环境变量的简单脚本管道示例吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

来自Javadoc of UpstreamCause

getUpstreamRun
@CheckForNull
public Run<?,?> getUpstreamRun()
Since: 1.505

这可能会返回代表触发运行的Run的对象(其UpstreamCause也是如此)。

来自Javadoc of Run

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.

希望这可以帮助您入门。

答案 1 :(得分:0)

def upStreamBuilds = currentBuild.upstreamBuilds
if (!upStreamBuilds.isEmpty()) {
  // Only immediate upstream
  Run<?,?> upstream = upStreamBuilds.get(0).getRawBuild()
  def upstreamEnvVars  = upstream.getEnvironment(TaskListener.NULL)
}