BhanuKiran
BhanuKiran

Reputation: 3151

Get upstream environment variables - Jenkins scripted pipeline

How can we obtain upstream environment variables in jenkins scripted pipeline?

While going through documentation and came across getBuildCauses and upstreamBuilds. And bit googling came across some cases of these functions

def causes = currentBuild.getBuildCauses()
def upstream = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
def upstream = currentBuild.upstreamBuilds  

unfortunately, none of the implementation obtain environment variables from the upstream. Can some one demonstrated simple scripted pipe line example that prints upstream environment variables ? Any help is greatly appreciated.

Upvotes: 2

Views: 2946

Answers (3)

Shawn Hu
Shawn Hu

Reputation: 439

def upstreamEnvVars = urrentBuild.upstreamBuilds[0].buildVariables

Please refer to: https://kb.novaordis.com/index.php/Jenkins_currentBuild#buildVariables

Upvotes: 0

BhanuKiran
BhanuKiran

Reputation: 3151

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

Upvotes: 5

MaratC
MaratC

Reputation: 6889

From the Javadoc of UpstreamCause:

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

This might return the object representing the Run that triggered your run (and so was its UpstreamCause).

From the 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.

Hope this might get you started.

Upvotes: 2

Related Questions