Use environment variable from upstream build

39 views
Skip to first unread message

Stefan.R...@tk.de

unread,
Feb 24, 2020, 11:33:15 AM2/24/20
to jenkins...@googlegroups.com
Hi everyone,
 
I used to set an environment variable via
  env.x = "…"
in my Jenkinsfile.
Another pipeline has an upstream trigger configured and runs right after the first pipeline build finishes. It reads the environment from the upstream build.
It used to work, that the downstream build could make decisions, based on the value of x of the upstream build.
 
Now,
  currentBuild.rawBuild.getCause(Cause$UpstreamCause).getUpstreamRun().getEnvironment(TaskListener.NULL)
returns all well known environment variables, like BUILD_NUMBER, JENKINS_HOME, etc. but NOT x anymore.
 
I tried different things…
  • set x in "environment { }"
  • initialize x with a default value in the top level "environment { }" bevor setting it in later stages
  • use env.x = …
  • use sh "export x=…"
 
Nothing worked to get access to x from the downstream build.
 
Does anyone have a hint, how to get this working again?
 
Thanks and best regards
Stefan
 

Stefan.R...@tk.de

unread,
Feb 25, 2020, 10:24:23 AM2/25/20
to jenkins...@googlegroups.com

Hi,

 

as a simple test case, I created these two pipelines.

 

First, the "upstream pipeline":

pipeline {

    agent {

        label 'Linux-Test'

    }

   

    environment {

        name = 'foo'

    }

   

    stages {

        stage('Set environment') {

            environment {

                name = 'bar'

            }

            steps {

                echo "name=${name}"

            }

        }

    }

}

 

Second, the "downstream pipeline":

pipeline {

    agent {

        label 'Linux-Test'

    }

   

    triggers {

        upstream(

            upstreamProjects: "upstream-pipeline",

            threshold: hudson.model.Result.SUCCESS

        )

    }

   

    stages {

        stage('print upstream environment') {

            steps {

                script {

                    echo currentBuild.rawBuild.getCause(Cause$UpstreamCause).getUpstreamRun().getEnvironment(TaskListener.NULL).toMapString()

                }

            }

        }

    }

}

 

Anyone?

 

Best regards

Stefan

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c7050334228f44a382f82cfd69c6f965%40tk.de.

Mark Waite

unread,
Feb 25, 2020, 1:07:18 PM2/25/20
to Jenkins Users
I wonder if you need to use  https://issues.jenkins-ci.org/browse/JENKINS-56875 and its "allow undefined parameters" setting to grant permission for the environment variables from the preceding job to be visible in the successor job?

Alternatively, have you considered passing the values as parameters instead of using environment variables?

Stefan.R...@tk.de

unread,
Feb 26, 2020, 7:35:15 AM2/26/20
to jenkins...@googlegroups.com

Hi,

 

thanks for the suggestions.

Sidenote: This solution, using environment variables used to work and afaik, we didn't make any pipeline changes, related to this. Unfortunately I can't narrow down, since when exactly it broke.

 

I can confirm, that allowing undefined parameters (JENKINS-56875) does not help.

 

I realized, that the environment, returned by Run.getEnvironment() does not even contain my custom environment variables, when I call it on currentBuild.rawBuild.

So this issue is not even related to a upstream/downstream relation between two pipelines.

The getEnvironment() method simply seems to behave differently since…???

 

Here is my simplified example.

pipeline {

    agent {

        label 'Linux-Test'

    }

   

    environment {

        name = 'foo'

    }

   

    stages {

        stage('Set environment') {

            environment {

                name = 'bar'

            }

            steps {

                echo "${currentBuild.rawBuild.getEnvironment(TaskListener.NULL)}"

            }

        }

    }

}

 

environment (from console log):

[BUILD_DISPLAY_NAME:#5, BUILD_ID:5, BUILD_NUMBER:5, BUILD_TAG:jenkins-upstream-pipeline-5, BUILD_TIMESTAMP:2020-02-26_13-20-47, BUILD_URL:https://jenkins-test8080/job/upstream-pipeline/5/, CLASSPATH:, GIT_HOME:C:\ tools\git, HUDSON_HOME:c:\jenkins\jenkins-test, HUDSON_SERVER_COOKIE:662af83ef93bf80d, HUDSON_URL:https://jenkins-test:8080/, JDK_18_LATEST_X64:C:\SPU\ext64\jdk1.8.0_112, jenkins.slaves.DefaultJnlpSlaveReceiver.disableStrictVerification:true, JENKINS_HOME:c:\jenkins\jenkins-test, JENKINS_SERVER_COOKIE:662af83ef93bf80d, JENKINS_URL:https://jenkins-test.dst.tk-inline.net:8080/, JOB_BASE_NAME:upstream-pipeline, JOB_DISPLAY_URL:https://jenkins-test.dst.tk-inline.net:8080/job/upstream-pipeline/display/redirect, JOB_NAME:upstream-pipeline, JOB_URL:https://jenkins-test:8080/job/upstream-pipeline/, RUN_CHANGES_DISPLAY_URL:https://jenkins-test:8080/job/upstream-pipeline/5/display/redirect?page=changes, RUN_DISPLAY_URL:https://jenkins-test:8080/job/upstream-pipeline/5/display/redirect]

 

Still hoping, that there is a way to get the complete environment…

Reply all
Reply to author
Forward
0 new messages