Is it possible to update a variable in a dynamically created stage?

7 views
Skip to first unread message

Eric Boehm

unread,
Jun 21, 2021, 4:58:41 PMJun 21
to jenkins...@googlegroups.com
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
Changes needed
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.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.
Reply all
Reply to author
Forward
0 new messages