Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For each entry in the data set, the foreach command will execute all subsequent commands until a foreach.end has been found.

By default On start of the iteration, the a loop context will be created and put into the vars scope under name loop. Inside the iteration block, you can access the loop context attributes, which are:

  • item = Contains the current iteration item.

  • index = Returns the current iteration index (0 based)

  • even = Returns true in case the current iteration index is an even number.

So for example the current iteration item can be accessed using ${vars.loop.item} inside the iteration body.

For example:

Code Block
languageyaml
vars:
  people: ["Sam", "Sarah", "Carol"]

pipeline:

  - foreach:
      in: ${vars.people}

  - log:
      message: "Hello ${vars.loop.item}, you are on index: ${vars.loop.index}"

  - foreach.end

You can also define a loop item name using the as parameter. In this case for reach iteration, the item is placed in the vars scope under the name specified by the as parameter:

Code Block
languageyaml
vars:
  people: ["Sam", "Sarah", "Carol"]

pipeline:

  - foreach:
      in: ${vars.people}
      as: person

  - log:
      message: "Hello ${vars.person}, you are on index: ${vars.loop.person.index}"

  - foreach.end
Info

Note: If the iteration variable was defined using the parameter as, the loop context will also be put under this same name: vars.loop.index -> vars.loop.person.index.

Also nesting of foreach is possible:

Code Block
languageyaml
body: []

vars:
  people: ["Sam", "Carol"]  
  activities: "biking, swimming, hiking"

pipeline:

  - foreach:
      in: ${vars.people}
      as: person

  - foreach:
      in: ${vars.activities}
      as: activity

  - eval:
      expr: "${@list.add(body, vars.person + ' could do: ' + vars.activity)}"

  - foreach.end
  - foreach.end

body: []

This would produce an output like this:

...

You can simplify this by using the eval parameter instead of the eval command:

Code Block
languageyaml
body: []
vars:
  people: ["Sam", "Carol"]  
  activities: "biking, swimming, hiking"

pipeline:

  - foreach:
      in: ${vars.people}
      as: person

  - foreach:
      in: ${vars.activities}
      as: activity
      eval: "${@list.add(body, vars.person + ' could do: ' + vars.activity)}"

  - foreach.end
  - foreach.end

body: []

You can also combine the foreach with the if command:

Code Block
languageyaml
body: []
vars:
  people: [{"name":"Sam", "age": 15}, {"name": "Carol", "age": 35}]  
  activities: "biking, clubbing"

pipeline:

  - foreach:
      in: ${vars.people}
      as: person

  - foreach:
      in: ${vars.activities}
      as: activity

  - if:
      true: ${vars.person.age < 18 and vars.activity == 'clubbing'}

  - set.var:
      key: allowed
      value: "NOT "

  - if.else:

  - set.var:
      key: allowed
      value: ""

  - if.end:
      eval: "${@list.add(body, vars.person.name + ' could ' + vars.allowed + 'do: ' + vars.activity)}"

  - foreach.end
  - foreach.end

body: []

This would result in an output like this:

...