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

Ability to set variables shared between stages

    • Declarative backlog

      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

       

       

          [JENKINS-44376] Ability to set variables shared between stages

          Jose Blas Camacho Taboada added a comment - - edited

          Hi laurentperez,

           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}"
                      }
                  }
              }
          }

          Jose Blas Camacho Taboada added a comment - - edited Hi laurentperez ,  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}" } } } }

          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.

          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.

          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.

          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.

          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.

          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 .

            Unassigned Unassigned
            laurentperez laurent perez
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: