1

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 Answers1

2

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"
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thansk for the quick reply @tim_yates but unfortunately the result I am getting is `--application-name '[tea, black]'` and for `--source null` I need to get `--application-name tea` and `--source black` and for the next loop i need to get `--application-name suagr` and `--source white` and so on – startprogramming Nov 08 '18 at 23:11
  • @startprogramming Please check the update in the answer. – dmahapatro Nov 09 '18 at 02:40
  • Thanks @dmahapatro this works fine I have checked in tutoriaslpoint but when you execute in Jenkins its still the same as my previous comment and I want to execute in **shell** script not just **echo** or **println** – startprogramming Nov 09 '18 at 15:30
  • And `sh """aws deploy push --application-name "$x" --source "$y" """` doesn't work? – tim_yates Nov 09 '18 at 15:37
  • @tim_yates Thanks, No its the same issue I have updated my question as I said I am getting `--application-name "[tea, black]" --source "null"` when you run in jenkins – startprogramming Nov 09 '18 at 15:58
  • 1
    Try: `[foo, col].transpose().each { x -> sh """aws deploy push --application-name "${x[0]}" --source "${x[1]}" """ }` – tim_yates Nov 09 '18 at 19:01
  • @tim_yates Wow.. Thanks a lot Tim worked like a charm was picking my hair out for this .. – startprogramming Nov 12 '18 at 15:24