Why an each loop in a Jenkinsfile stops at first iteration

前端 未结 4 731
终归单人心
终归单人心 2021-02-04 04:34

Here is the content of my Jenkinsfile :

node {
    // prints only the first element \'a\'
    [ \'a\', \'b\', \'c\' ].each {
        echo it
    }
}         


        
4条回答
  •  爱一瞬间的悲伤
    2021-02-04 05:00

    A workaround for this issue is to expand all the commands to a flat text file as a groovy script. Then use load step to load the file and execute.

    For example:

    @NonCPS
    def createScript(){
        def cmd=""
        for (i in [ 'a', 'b', 'c' ]) {
            cmd = cmd+ "echo $i"
        }
        writeFile file: 'steps.groovy', text: cmd
    }
    

    Then call the function like

    createScript()
    load 'steps.groovy'
    

提交回复
热议问题