passing items in two lists simultaneoulsy in each

2019-08-06 08:43发布

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}" """
}

Desired Result

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

the result I am getting

[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] }

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

Ref Nested `each` loops in Groovy

Can we do something like (foo,col).each

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

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}\" """
        }
      }
    }
}      

} }

1条回答
三岁会撩人
2楼-- · 2019-08-06 09:44

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 ->
    ...
}

UPDATE:

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}" """
}

results

--application-name "tea" --source "black" 
--application-name "sugar" --source "white" 
--application-name "milk" --source "pink"
查看更多
登录 后发表回答