0

I've looked at this Pass Artifact or String to upstream job in Jenkins Pipeline and this Pass variables between Jenkins stages and this How do I pass variables between stages in a declarative Jenkins pipeline?, but none of these questions seem to deal with my specific problem.

Basically I have a pipeline consisting of multiple stages, each run in its own agent.

In the first stage I run a shell script. Here two variables are generated. I would like to use these variables in the next stage. The methods I've seen so far seem to only work when passing variables within the same agent.

pipeline {

    stages {
        stage("stage 1") {
           agent {
                docker {
                    image 'my_image:latest'
                }
            }
            steps {
               sh ("""
                export VAR1=foo
                export VAR2=bar
             """)                
            }
        }

       stage("stage 2") {
           agent {
                docker {
                    image 'my_other_image:latest'
                }
            }
            steps {
               sh ("echo "$VAR1 $VAR2")
               //expecting to see "foo bar" printed here             
            }
        }
Karl
  • 5,573
  • 8
  • 50
  • 73
  • Answer to global environment variable setting is here: https://stackoverflow.com/questions/62880159/is-it-possible-to-set-the-value-of-an-environmental-variable-on-an-agent-node-fr/62882438#62882438 – Matthew Schuchard Nov 02 '20 at 19:44
  • See [this](https://stackoverflow.com/a/64572833/2047614) for information on what happens to your variables. – MaratC Nov 03 '20 at 02:43

0 Answers0