The following pipeline script fails

class Test implements Serializable {

    final def buildIn
    final def allUpstreamDependencies = [:]

    Test(buildIn) {
        this.buildIn = buildIn
    }

    void test() {
        buildIn.node("master") {
            allUpstreamDependencies["projecA"] = ["A", "B", "C"]
            buildIn.deleteDir()
        }
    }

    def getAllUpstreamDependencies() {
        return allUpstreamDependencies
    }
}

def test = new Test(this)
test.test()
def allUpstreamDependencies = test.getAllUpstreamDependencies()
println allUpstreamDependencies


Running without Groovy Sandbox

groovy.lang.MissingFieldException: No such field: allUpstreamDependencies for 
class: org.jenkinsci.plugins.workflow.cps.CpsClosure2
        at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2846)
        at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:3782)
        at 
org.codehaus.groovy.runtime.InvokerHelper.getAttribute(InvokerHelper.java:147)
        at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getField(ScriptBytecodeAdapter.java:306)
        at 
com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getAttribute(DefaultInvoker.java:48)
        at 
com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20)
        at Test.test(WorkflowScript:12)
        at ___cps.transform___(Native Method)


Running with Groovy Sandbox

No such field found: field org.jenkinsci.plugins.workflow.cps.CpsClosure2 
allUpstreamDependencies. Administrators can decide whether to approve or reject 
this signature. <https://build-ci.spacetec.no:8443/scriptApproval>[Pipeline] 
End of 
Pipelineorg.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: 
No such field found: field org.jenkinsci.plugins.workflow.cps.CpsClosure2 
allUpstreamDependencies
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:397)
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:408)
        at org.kohsuke.groovy.sandbox.impl.Checker$8.call(Checker.java:369)
        at 
org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:374)
        at 
com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:37)
        at 
com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20)
        at Test.test(WorkflowScript:12)
        at ___cps.transform___(Native Method)




I came up with the following workaround

class Test implements Serializable {

    final def buildIn

    def allUpstreamDependencies2 = [:]

    Test(buildIn) {
        this.buildIn = buildIn
    }

    void test() {
        def allUpstreamDependencies = [:]

        buildIn.node("master") {
            allUpstreamDependencies["projecA"] = ["A", "B", "C"]
            buildIn.deleteDir()
        }

        allUpstreamDependencies2 = allUpstreamDependencies
    }

    def getAllUpstreamDependencies() {
        return allUpstreamDependencies2
    }
}

def test = new Test(this)
test.test()
def allUpstreamDependencies = test.getAllUpstreamDependencies()
println allUpstreamDependencies


What is the deal here anyway?

Last I decided to try something I have used in my vars/ groovy scripts. 
Using this when writing to class variables.
Seems this is the way to do it, but I am not sure why I needed to reference 
this within test method.
Would appreciate if anyone could explain a little.

class Test implements Serializable {

    final def buildIn
    final def allUpstreamDependencies = [:]

    Test(buildIn) {
        this.buildIn = buildIn
    }

    void test() {
        buildIn.node("master") {
            this.allUpstreamDependencies["projecA"] = ["A", "B", "C"]
            buildIn.deleteDir()
        }
    }

    def getAllUpstreamDependencies() {
        return allUpstreamDependencies
    }
}

def test = new Test(this)
test.test()
def allUpstreamDependencies = test.getAllUpstreamDependencies()
println allUpstreamDependencies



-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/1d9242a9-4b2a-4bed-9971-18a81e85d1b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to