jenkinsjenkins-pipeline

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.


Solution

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