title: access environment variables in jenkins shared library code
body: <p>When I use my new shared library I cannot access environment variables for any src class which is executed either directly by the Jenkinsfile or via a var/*.groovy script.   This problem persists even when I add <strong>withEnv</strong> to the var/*groovy script.</p>
  
  <p>What is the trick to get environment variables to propagate to jenkins shared library src class execution?</p>
  
  <p><strong>Jenkinsfile</strong></p>
  
  <pre><code>withEnv(["FOO=BAR2"]) {
    println "Jenkinsfile FOO=${FOO}"
    library 'my-shared-jenkins-library'
    lib.displayEnv()
  </code></pre>
  
  <p><strong>Shared Library var/lib.groovy</strong></p>
  
  <pre><code>def displayEnv() {
    println "Shared lib var/lib FOO=${FOO}"
    MyClass c = new MyClass()
  }
  </code></pre>
  
  <p><strong>Shared Library src/MyClass.groovy</strong></p>
  
  <pre><code>class MyClass() {
    MyClass() {
      throw new Exception("Shared lib src/MyClass  FOO=${System.getenv('FOO')")
    }
  }
  </code></pre>
  
  <p>** Run Result **</p>
  
  <pre><code>Jenkinsfile FOO=BAR
  Shared lib var/lib FOO=BAR
  java.lang.Exception: Shared lib src/MyClass FOO=null
  ...
  </code></pre>