在每个循环中同时在两个列表中同时传递项目Jenkinsfile [英] passing items in two lists simultaneoulsy in each loop Jenkinsfile

查看:509
本文介绍了在每个循环中同时在两个列表中同时传递项目Jenkinsfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表foo = ['tea',''sugar','milk']col = ['black','white','pink']我想做的是嵌套循环

I have a list foo = ['tea',''sugar','milk'] and col = ['black','white','pink'] what I am trying to do is nested loop

def foo = ['tea','sugar','milk']
def col = ['black','white','pink']

[foo, col].transpose().each { x, y ->
   sh """aws deploy push --application-name "${x}" --source "${y}" """
}

所需结果

--application-name "tea" --source "black" 
--application-name "sugar" --source "white" 
--application-name "milk" --source "pink"

我得到的结果

[Pipeline] script
[Pipeline] {
[Pipeline] echo
--application-name "[tea, black]" --source "null" 
[Pipeline] echo
--application-name "[sugar, white]" --source "null" 
[Pipeline] echo
--application-name "[milk, pink]" --source "null" 
[Pipeline] }
[Pipeline] // script
[Pipeline] }

我希望将 foo col 中的列表项一一注入到上述shell脚本中 有没有一种方法可以同时将两个列表项传递到上述Shell脚本

I want the list items in foo and col to be injected one by one to the above shell script Is there a way where we can pass both list items at once to the above shell script

参考在Groovy中嵌套的"each"循环

我们可以做类似(foo,col).each

或可能使用for循环for(x in foo && y in col)

or maybe using for loop for(x in foo && y in col)

引用我的Jenkinsfile

Ref my Jenkinsfile

pipeline {
agent any
stages {
    stage('hello'){
        steps{
        script{ 
        def foo = ['tea','sugar','milk']
        def col = ['black','white','pink']

        [foo, col].transpose().each { x, y ->
        sh """aws deploy push --application-name "${x}" --source "${y}" """
        //echo """--application-name \"${x}\" --source \"${y}\" """
        }
      }
    }
}      

} }

推荐答案

我相信转置是您追求的方法,将两个列表配对,然后可以遍历结果:

I believe transpose is the method you are after, to pair up the two lists, then you can iterate through the result:

[foo, col].transpose().each { x, y ->
    ...
}

更新:

这就是我的目标.请注意,为简洁起见,删除了一些参数

This is what I was aiming at. Note that some of the parameters are removed for brevity

def foo = ['tea','sugar','milk']
def col = ['black','white','pink']

[foo, col].transpose().each { x, y ->
   println """--application-name "${x}" --source "${y}" """
}

结果

--application-name "tea" --source "black" 
--application-name "sugar" --source "white" 
--application-name "milk" --source "pink"

这篇关于在每个循环中同时在两个列表中同时传递项目Jenkinsfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆