Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-44376

Ability to set variables shared between stages

    Details

    • Similar Issues:
    • Epic Link:
    • Sprint:
      Declarative backlog

      Description

      It would be extremely useful to be able to share variables between stages ; currently, environment variables are overriden per-stage only, given the following example : 

       

      stages {
          stage('Setup profile') {
              environment {
                  MVNARGS = "-P${params.profile}"
              }
              when {
                  expression {
                      params.profile != '' 
                  }
              }
              steps {
                  echo "deploying as args:${env.MVNARGS} - env:${MVNARGS} - pprofile:${params.profile} - ..."
              }
          }
          stage('Build') {
              steps {
                  sh "mvn ${env.MVNARGS} xxxxxxx"
              }
          }
      }
      

      In Build, MVNARGS is null because the override from the Setup profile is stage scoped.

      I did not find a workaround for this, I've tried to share using the params Map but it's an unmodifiable read-only map. 

      Any ideas ?

       

      Thanks

       

       

        Attachments

          Issue Links

            Activity

            Hide
            jtaboada Jose Blas Camacho Taboada added a comment - - edited

            Hi laurent perez,

             have you tried to set the environment variables in the top level?

              

            pipeline {
                environment {
                    FOO = "FOO"
                }
                agent any
            
                stages {
                    stage("foo") {
                        steps {
                            echo " FOO-1 is ${env.FOO}"
                        }
                    }
                    stage("group") {
                        steps {
                            echo " FOO-2 is ${env.FOO}"
                        }
                    }
                    stage("Override"){
                        environment {
                            FOO = "BOO"
                        }
                        steps{
                            echo " FOO-3 is ${env.FOO}"
                        }
                    }
                }
            }
            Show
            jtaboada Jose Blas Camacho Taboada added a comment - - edited Hi laurent perez ,  have you tried to set the environment variables in the top level?    pipeline { environment { FOO = "FOO" } agent any stages { stage("foo") { steps { echo " FOO-1 is ${env.FOO}" } } stage("group") { steps { echo " FOO-2 is ${env.FOO}" } } stage("Override"){ environment { FOO = "BOO" } steps{ echo " FOO-3 is ${env.FOO}" } } } }
            Hide
            abayer Andrew Bayer added a comment -

            Lemme try to dig up a better use case/example - the most notable use case is to do something like read a config file in one stage and refer to what was read in a later one.

            Show
            abayer Andrew Bayer added a comment - Lemme try to dig up a better use case/example - the most notable use case is to do something like read a config file in one stage and refer to what was read in a later one.
            Hide
            jtaboada Jose Blas Camacho Taboada added a comment -

            The fact that a value can be set in an environment and be accessed by child stages lgtm, but setting values for siblings is unclear to be as it breaks the var scope.

            Show
            jtaboada Jose Blas Camacho Taboada added a comment - The fact that a value can be set in an environment and be accessed by child stages lgtm, but setting values for siblings is unclear to be as it breaks the var scope.
            Hide
            jglick Jesse Glick added a comment -

            read a config file in one stage

            Trivial in Scripted. This is antithetical to the notion of “Declarative”.

            Anyway I think this is asking the wrong question. The right way to write a Pipeline which reads and then uses a config file is

            pipeline {
              agent any
              stages {
                stage('All') {
                  steps {
                    sh 'do-it-all'
                  }
                }
              }
            }
            

            where the script in question is written in Ruby or whatever you find most convenient for such things.

            If you just want Blue Ocean to display a stage boundary between parts of a single sh step, that is another matter, and better done by creating a Pipeline analogue to Collapsing Console Sections.

            Show
            jglick Jesse Glick added a comment - read a config file in one stage Trivial in Scripted. This is antithetical to the notion of “Declarative”. Anyway I think this is asking the wrong question. The right way to write a Pipeline which reads and then uses a config file is pipeline { agent any stages { stage( 'All' ) { steps { sh ' do -it-all' } } } } where the script in question is written in Ruby or whatever you find most convenient for such things. If you just want Blue Ocean to display a stage boundary between parts of a single sh step, that is another matter, and better done by creating a Pipeline analogue to Collapsing Console Sections .

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                laurentperez laurent perez
              • Votes:
                1 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated: