Jenkins管道 - 如何遍历列表

mdo*_*123 6 groovy jenkins delivery-pipeline jenkins-pipeline

我需要从管道中的文件中读取值.我正在使用split()将它们放入一个数组中.我需要将它们放入Arraylist中,所以我使用的是Arrays.asList().我遇到的问题是我无法使用size()length()方法,所以我不能做一个for循环,如

for (ii = 0; ii < var.length; ii++)
Run Code Online (Sandbox Code Playgroud)

要么

for (ii = 0; ii < var.size; ii++)
Run Code Online (Sandbox Code Playgroud)

因为我得到错误:unclassified field java.util.Arrays $ ArrayList length

所以我尝试在每个循环中使用a,但是当我在finally块中执行某些操作(例如ls命令)时,它只迭代1次.但是,如果我只是运行命令'echo',它将按照它应该的方式迭代每个元素.有关如何修改我的代码以使其在使用任何命令时对列表中的每个元素进行迭代的任何建议吗?

工作正常....

node{
    wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [[fileId: 'dest_hosts.txt', targetLocation: '', variable: 'DEST_HOST']]]) {
        HOST = Arrays.asList(readFile(env.DEST_HOST).split("\\r?\\n"))
        deploy(HOST)
    }
}

@NonCPS
def deploy(host){
    for (String target : host){
        try {
            echo target
        }
        finally {
           echo target
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT(为每个元素迭代):

[Pipeline] node
Running on <obfuscated>
[Pipeline] {
[Pipeline] wrap
provisoning config files...
copy managed file [<obfuscated>] to file:/var/lib/jenkins/<obfuscated>
[Pipeline] {
[Pipeline] readFile
[Pipeline] echo
www.testhost.com
[Pipeline] echo
www.testhost.com
[Pipeline] echo
www.testhost2.com
[Pipeline] echo
www.testhost2.com
[Pipeline] }
Deleting 1 temporary files
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)

但是,如果我采取一些行动,例如'ls -l',它只会迭代一次

node{
    wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [[fileId: 'dest_hosts.txt', targetLocation: '', variable: 'DEST_HOST']]]) {
        HOST = Arrays.asList(readFile(env.DEST_HOST).split("\\r?\\n"))
        deploy(HOST)
    }
}

@NonCPS
def deploy(host){
    for (String target : host){
        try {
            echo target
        }
        finally {
           sh 'ls -l'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出(仅迭代1次):

[Pipeline] node
Running on <obfuscated>
[Pipeline] {
[Pipeline] wrap
provisoning config files...
copy managed file [<obfuscated>] to file:/var/lib/jenkins/<obfuscated>
[Pipeline] {
[Pipeline] readFile
[Pipeline] echo
www.testhost.com
[Pipeline] sh
[sandbox%2Fpipeline-test-new1] Running shell script
+ ls -l
total 8
-rw-r--r-- 1 jenkins jenkins 10 Jun 17 16:07 someFile
[Pipeline] }
Deleting 1 temporary files
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)

Krz*_*soń 7

ArrayList(并且通常Lists)没有长度或大小的字段,他们有一个size()方法.所以在for中使用它:

for (ii = 0; ii < var.size(); ii++)
Run Code Online (Sandbox Code Playgroud)


Mat*_*s M 6

我更喜欢这个解决方案

node('master') {
    stage('Test 1: loop of echo statements') {
        echo_all(abcs)
    }
}

@NonCPS // has to be NonCPS or the build breaks on the call to .each
def echo_all(list) {
    list.each { item ->
        echo "Hello ${item}"
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用声明性管道,则必须将调用包装在脚本语句中:

stage('master') {
    steps {
        script {
            echo_all(abcs);
        }
    }
Run Code Online (Sandbox Code Playgroud)