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 withEnv to the var/*groovy script.
What is the trick to get environment variables to propagate to jenkins shared library src class execution?
Jenkinsfile
withEnv(["FOO=BAR2"]) {
println "Jenkinsfile FOO=${FOO}"
library 'my-shared-jenkins-library'
lib.displayEnv()
Shared Library var/lib.groovy
def displayEnv() {
println "Shared lib var/lib FOO=${FOO}"
MyClass c = new MyClass()
}
Shared Library src/MyClass.groovy
class MyClass() {
MyClass() {
throw new Exception("Shared lib src/MyClass FOO=${System.getenv('FOO')")
}
}
** Run Result **
Jenkinsfile FOO=BAR
Shared lib var/lib FOO=BAR
java.lang.Exception: Shared lib src/MyClass FOO=null
...