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

          laurent perez created issue -
          James Dumay made changes -
          Epic Link New: JENKINS-45422 [ 183590 ]
          Andrew Bayer made changes -
          Link New: This issue is duplicated by JENKINS-43408 [ JENKINS-43408 ]
          Andrew Bayer made changes -
          Sprint New: Declarative backlog [ 621 ]
          Jose Blas Camacho Taboada made changes -
          Assignee Original: Andrew Bayer [ abayer ] New: Jose Blas Camacho Taboada [ jtaboada ]

          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}" } } } }
          Jose Blas Camacho Taboada made changes -
          Status Original: Open [ 1 ] New: In Progress [ 3 ]
          Jose Blas Camacho Taboada made changes -
          Status Original: In Progress [ 3 ] New: Open [ 1 ]
          Jose Blas Camacho Taboada made changes -
          Status Original: Open [ 1 ] New: In Progress [ 3 ]
          Jose Blas Camacho Taboada made changes -
          Status Original: In Progress [ 3 ] New: In Review [ 10005 ]

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

              Created:
              Updated: