Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound variables are not available in environment closure #387

Open
pavelsmolensky opened this issue Jul 7, 2021 · 0 comments
Open

Bound variables are not available in environment closure #387

pavelsmolensky opened this issue Jul 7, 2021 · 0 comments

Comments

@pavelsmolensky
Copy link

pavelsmolensky commented Jul 7, 2021

Given following (simplified) library files in vars directory:
testLib.groovy:

def call(body) {
    pipeline {
        environment {
            HI = helper.sayHi() //here helper is null
        }
        stages {
            stage("One") {
                helper.sayHi()  //however here helper is defined
                echo "${env.HI}"
            }
        }
    }
}

helper.groovy:

def sayHi(){
    return 'hi'
}

And test class:

class DemoTest extends DeclarativePipelineTest {
    @Override
    @BeforeEach
    public void setUp() throws Exception {
        super.setUp();
    }    

    @Test
    public void demo(){
        def lib = loadScript("vars/testLib.groovy")
        def helper = loadScript("vars/helper.groovy")
        binding.setVariable("helper", helper)
        lib.call({})
        printCallStack()
    }
}    

There is an exception when line HI = helper.sayHi() is executed in environment closure:

java.lang.NullPointerException: Cannot invoke method sayHi() on null object
...
	at testLib$_call_closure1$_closure2.doCall(testLib.groovy:4)
...

However calling helper.sayHi() at stage level works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants