I am having a problem updating a variable based on the result of a shell
script when I create stages dynamically. I think that this is because the
dynamically created stage is a closure and its view of the variable is as
of the time of the closure.

Is there a way to solve this problem?

Here's the sample Jenkinsfile if the stages are run sequentially and not
created dynamically.
It behaves as expected.

node {
    label 'master'
    def needsBuild = []
    myexit = 0
    myscript = """
    echo "Desired exit $myexit"
    exit $myexit
    """
    stage("Status $myexit") {
        status = sh script: myscript, returnStatus: true
        if (status) {
            echo "Changes needed"
            needsBuild.add(myexit)
        }
    }
    myexit = 1
    myscript = """
    echo "Desired exit $myexit"
    exit $myexit
    """
    stage("Status $myexit") {
        status = sh script: myscript, returnStatus: true
        if (status) {
            echo "Changes needed"
            needsBuild.add(myexit)
        }
    }
}

This Jenkinsfile has a traceback when it tries 'needsBuild.add(myexit)

  def createStage(mystatus) {
    def myexit = mystatus
    def myscript = """
    echo "Desired exit $myexit"
    exit $myexit
    """
    return {
        stage("job $myexit") {
            status = sh script: myscript, returnStatus: true
            if (status) {
                echo "Changes needed"
                needsBuild.add(myexit)
            }
        }
    }
}
node {
    label 'master'
    def needsBuild = []
    def stages = [:]
    for (index in [0, 1]) {
        stages["Exit value $index"] = createStage(index)
    }
    parallel stages
}


Here's the traceback:


+ exit 1[Pipeline] echo
<https://bca-bld-013.lvn.broadcom.net:9443/view/all/job/dynamic_stage/9/console#>Changes
needed[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] //
node[Pipeline] End of Pipelinegroovy.lang.MissingPropertyException: No
such property: needsBuild for class: groovy.lang.Binding
        at groovy.lang.Binding.getVariable(Binding.java:63)
        at 
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:271)


Any suggestions or guidance on what I am missing would be appreciated.

--
Eric Boehm

-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

-- 
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/CACL45fj-JkhhiU87WEhHnaEgcBm2Ca0%3Dn4uq8JrGhA5PnKe-Yg%40mail.gmail.com.

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to