title: Can I build stages with a function in a Jenkinsfile?
body: <p>I'd like to use a function to build some of the stages of my Jenkinsfile. This is going to be a build with a number of repetitive stages/steps - I'd not like to generate everything manually.</p>
  
  <p>I was wondering if it's possible to do something like this:</p>
  
  <pre><code>_make_stage() {
      stage("xx") {
          step("A") {
              echo "A"
          }
  
          step("B") {
              echo "B"
          }
      }
  }
  
  _make_stages() {
      stages {
          _make_stage()
      }
  }
  
  // pipeline starts here!
  pipeline {
      agent any
      _make_stages()
  }
  </code></pre>
  
  <p>Unfortunately Jenkins doesn't like this - when I run I get the error:</p>
  
  <pre><code>WorkflowScript: 24: Undefined section "_make_stages" @ line 24, column 5.
         _make_stages()
         ^
  
  WorkflowScript: 22: Missing required section "stages" @ line 22, column 1.
     pipeline {
     ^
  </code></pre>
  
  <p>So what's going wrong here? The function _make_stages() really looks like it returns whatever the stages object returns. Why does it matter whether I put that in a function call or just inline it into the pipeline definition?</p>