access environment variables in jenkins shared library code

It sure looks like the only way to handle this is to pass the this from Jenkins file down to the var/lib.groovy and harvest from that object

Jenkinsfile

withEnv(["FOO=BAR2"]) {
  library 'my-shared-jenkins-library'
  lib.displayEnv(this)

var/lib.groovy

def displayEnv(script) {
 println "Shared lib var/lib FOO=${FOO}"
 MyClass c = new MyClass(script)

}

src class

MyClass(def script) {
  throw new Exception("FOO=${script.env.FOO}")
}

I believe you can populate the environment variable as below, where shared library can access.

Jenkisfile

env.FOO="BAR2"
library 'my-shared-jenkins-library'
lib()

vars/lib.groovy

def call(){
    echo ("FOO: ${FOO}")
    echo ("FOO:"+env.FOO)
}

Another method is use the "steps" variable:

In Jenkinsfile

mypackages.myclass.mymethod(steps)

In src

class myclass implements Serializable {
  void mymethod(steps) {
    String myEnvVar = steps.sh(returnStdout: true, script: "env | grep 'myVar' | cut -f 2- -d '='")
  }
}