Currently EnvStep only accepts String array.

      But it would be nice if it could accept Map<String, String> too
      and convert it to a String array.

      Right now I have to do this in a shared library.

      def call(Closure body) {
        deleteDir()
        def scmVars = checkout scm
        def list = []
        scmVars.each { k, v -> list << "${k}=${v}" }
        withEnv(list) {
          body()
        }
      }
      

       

       

          [JENKINS-46124] EnvStep convert map to list

          Joseph Petersen (old) created issue -
          Tom Ghyselinck made changes -
          Priority Original: Minor [ 4 ] New: Major [ 3 ]

          Tom Ghyselinck added a comment - - edited

          Hi,

          This would be a great improvement for use with declarative pipeline.

          We now have the case where we only perform a checkout in the stages where applicable.

          It could help to have some syntax like:

          pipeline {
              options {
                  skipDefaultCheckout true
              }
          
              stages {
                  stage('Build') {
                      steps {
                          withEnv(checkout(scm)) {
                              echo 'printenv'
                              echo 'Perform the build'
                          }
                      }
                  }
              }
          }
          

          On the other hand, we still have the case that checkout and withEnv are in separate steps.

          We would something like the following (not that this example won't work because of the variable assignment!):

          pipeline {
               options {
                   skipDefaultCheckout true
               }
          
               stages {
                  stage('Build') {
                      steps {
                          def scmVars = checkout(scm)
          
                          echo 'Preparing the build'
          
                          withEnv(scmVars) {
                              echo 'printenv'
                              echo 'Perform the build'
                          }
                      }
                  }
              }
          }
          

          Our current workaround is to set the scmVars as a global variable and then "translate" it into a list for use with withEnv:

          import groovy.transform.Field
          
          @Field XRA31_SCM_VARS = null
          
          /**
           * Sets the XRA31_SCM_VARS variables to the output of the `checkout` command
           */
          def xra31_prepare_source(steps, scm) {
              XRA31_SCM_VARS = steps.checkout changelog: false, poll: false, scm: scm
          }
          
          def flatten_map(Map m) {
              List l = []
              m.each { k,v ->
                  l.add([k, v].join('='))
              }
              return l
          }
          
          pipeline {
               options {
                   skipDefaultCheckout true
               }
          
               stages {
                  stage('Build') {
                      steps {
                          xra31_prepare_source(this, scm)
          
                          echo 'Preparing the build'
          
                          withEnv(flatten_map(XRA31_SCM_VARS)) {
                              echo 'printenv'
                              echo 'Perform the build'
                          }
                      }
                  }
              }
          }
          

          It would be great to see a solution for the latter case since we use/need it extensively.

          With best regards,
          Tom.

          Tom Ghyselinck added a comment - - edited Hi, This would be a great improvement for use with declarative pipeline. We now have the case where we only perform a checkout in the stages where applicable. It could help to have some syntax like: pipeline { options { skipDefaultCheckout true } stages { stage('Build') { steps { withEnv(checkout(scm)) { echo 'printenv' echo 'Perform the build' } } } } } On the other hand, we still have the case that checkout and withEnv are in separate steps. We would something like the following ( not that this example won't work because of the variable assignment! ): pipeline { options { skipDefaultCheckout true } stages { stage('Build') { steps { def scmVars = checkout(scm) echo 'Preparing the build' withEnv(scmVars) { echo 'printenv' echo 'Perform the build' } } } } } Our current workaround is to set the scmVars as a global variable and then " translate " it into a list for use with withEnv : import groovy.transform.Field @Field XRA31_SCM_VARS = null /** * Sets the XRA31_SCM_VARS variables to the output of the `checkout` command */ def xra31_prepare_source(steps, scm) { XRA31_SCM_VARS = steps.checkout changelog: false, poll: false, scm: scm } def flatten_map(Map m) { List l = [] m.each { k,v -> l.add([k, v].join('=')) } return l } pipeline { options { skipDefaultCheckout true } stages { stage('Build') { steps { xra31_prepare_source(this, scm) echo 'Preparing the build' withEnv(flatten_map(XRA31_SCM_VARS)) { echo 'printenv' echo 'Perform the build' } } } } } It would be great to see a solution for the latter case since we use/need it extensively. With best regards, Tom.
          Tom Ghyselinck made changes -
          Component/s New: workflow-scm-step-plugin [ 21717 ]

          Another solution which would help us is to add an option to the checkout command to inject the environment variables directly instead of only returning them.

          Tom Ghyselinck added a comment - Another solution which would help us is to add an option to the checkout command to inject the environment variables directly instead of only returning them.

          jglick,

          I know you work a lot on this stuff.
          Is this something you find useful?

          Thank you in advance!

          With best regards,
          Tom.

          Tom Ghyselinck added a comment - jglick , I know you work a lot on this stuff. Is this something you find useful? Thank you in advance! With best regards, Tom.

          Hey tom_ghyselinck when using declarative pipeline you don't have to use checkout scm because the job is configured to use scm and the declarative pipeline will just checkout from that

          Joseph Petersen (old) added a comment - Hey tom_ghyselinck when using declarative pipeline you don't have to use checkout scm because the job is configured to use scm and the declarative pipeline will just checkout from that

          Hi casz,

          It depends

          I use skipDefaultCheckout true in my pipeline options.
          I updated my comment accordingly.

          Thank you for the suggestion anyway!

          With best regards,
          Tom.

          Tom Ghyselinck added a comment - Hi casz , It depends I use skipDefaultCheckout true in my pipeline options. I updated my comment accordingly. Thank you for the suggestion anyway! With best regards, Tom.

          The envs is passed from the default checkout

          Joseph Petersen (old) added a comment - The envs is passed from the default checkout
          Jesse Glick made changes -
          Component/s Original: workflow-scm-step-plugin [ 21717 ]

            jetersen Joseph Petersen
            casz Joseph Petersen (old)
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: