title: Access a Groovy variable from within shell step in Jenkins pipeline
body: <p>Using the <a href="https://jenkins.io/doc/book/pipeline/overview/" rel="noreferrer">Pipeline plugin in Jenkins 2.x</a>, how can I access a Groovy variable that is defined somewhere at stage- or node-level from within a <code>sh</code> step?</p>
  
  <p>Simple example:</p>
  
  <pre><code>node {
      stage('Test Stage') {
          some_var = 'Hello World' // this is Groovy
          echo some_var // printing via Groovy works
          sh 'echo $some_var' // printing in shell does not work
      }
  }
  </code></pre>
  
  <p>gives the following on the Jenkins output page:</p>
  
  <pre><code>[Pipeline] {
  [Pipeline] stage
  [Pipeline] { (Test Stage)
  [Pipeline] echo
  Hello World
  [Pipeline] sh
  [test] Running shell script
  + echo
  
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  </code></pre>
  
  <p>As one can see, <code>echo</code> in the <code>sh</code> step prints an empty string.</p>
  
  <p>A work-around would be to define the variable in the environment scope via </p>
  
  <pre><code>env.some_var = 'Hello World'
  </code></pre>
  
  <p>and print it via</p>
  
  <pre><code>sh 'echo ${env.some_var}'
  </code></pre>
  
  <p>However, this kind of abuses the environmental scope for this task.</p>