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

Allow variables and functions to be defined within pipeline to be used in any stage

    Details

    • Similar Issues:
    • Epic Link:

      Description

      I have several use cases where I may operate on a list or map on different stages of a pipeline. This variable may not be known ahead of time - it could be defined within a stage. I'd like a syntax that allows me to define variables within a pipeline.

      Today, I have to define a shared variable before entering pipeline {}.

      Jenkinsfile
      #!/bin/groovy
      // Define outside of pipeline to make sure accessible in all script {} blocks
      def my_list
      
      pipeline {
          agent { label 'label' }
          stages {
              stage('stage1') {
                steps {
                 script {
                     my_list = [1,2,3]
                 }
                }
              }
          
              stage('stage2') {
                steps {
                  script {
                      for(int i=0; i<my_list.size();i++) {
                          echo "Doing something with ${my_list[i]}"
                      }
                  }
                }
              }
          }
      }
      

      A simple approach would be to allow "def" to work anywhere within pipeline {}. This avoids any overly new syntax for pipeline.

      Another option would allow the user to provide a special pipeline {} block for defining non-env/params style variables. This could be helpful in allowing the user to use items available in script {} for defining shared state.

      Jenkinsfile with possible example syntax
      #!/bin/groovy
      
      
      pipeline {
          agent { label 'label' }    
      
          define {
             def my_map = [:] //empty map
             def my_list //undefined shared variable
           }
      
          stages {
              stage('stage1') {
                steps {
                  script {
                    //If def can be anywhere in pipeline {}, drop script {}
                    my_list = [1,2,3]
                  }
                }
              }
          
              stage('stage2') {
                steps {
                  script {
                      for(int i=0; i<my_list.size();i++) {
                          echo "Doing something with ${my_list[i]}"
                      }
                  }
                }
              }
          }
      }
      

        Attachments

          Issue Links

            Activity

            rpocase Robby Pocase created issue -
            bitwiseman Liam Newman made changes -
            Field Original Value New Value
            Link This issue relates to JENKINS-41396 [ JENKINS-41396 ]
            abayer Andrew Bayer made changes -
            Summary Allow variable to be defined within pipeline to be used in any stage Allow variables and functions to be defined within pipeline to be used in any stage
            abayer Andrew Bayer made changes -
            Link This issue is duplicated by JENKINS-41396 [ JENKINS-41396 ]
            abayer Andrew Bayer made changes -
            Priority Minor [ 4 ] Critical [ 2 ]
            abayer Andrew Bayer made changes -
            Link This issue is blocked by JENKINS-39450 [ JENKINS-39450 ]
            abayer Andrew Bayer made changes -
            Link This issue is duplicated by JENKINS-41396 [ JENKINS-41396 ]
            bitwiseman Liam Newman made changes -
            Link This issue relates to JENKINS-42079 [ JENKINS-42079 ]
            bitwiseman Liam Newman made changes -
            Link This issue relates to JENKINS-42360 [ JENKINS-42360 ]
            jamesdumay James Dumay made changes -
            Epic Link JENKINS-45422 [ 183590 ]
            saucistophe Christophe Carpentier made changes -
            Attachment screenshot-1.png [ 38956 ]
            saucistophe Christophe Carpentier made changes -
            Attachment screenshot-1.png [ 38956 ]
            abayer Andrew Bayer made changes -
            Status Open [ 1 ] Resolved [ 5 ]
            Resolution Won't Fix [ 2 ]
            bitwiseman Liam Newman made changes -
            Status Resolved [ 5 ] Closed [ 6 ]

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                rpocase Robby Pocase
              • Votes:
                43 Vote for this issue
                Watchers:
                57 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: