...
Code Block | ||
---|---|---|
| ||
vars: name: "Sabrina" age: 24 pipeline: - if: true: ${vars.name != ''} - if: true: ${vars.age > 21} - log: message: "${vars.name} may have a drink..." - if.else: - log: message: "${vars.name} is too young to have a drink..." - if.end - if.end |
...
Foreach (Iterator)
Looping over a set of data is also called "iterating" over this set of data. Or in other words, "for each" item from the data set, do some action.
...
For each entry in the data set, the foreach
command will execute all subsequent commands until a foreach.end
has been found.
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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 |
Also nesting of foreach
is possible:
Code Block | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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:
...
In case there is a finally
command in the pipeline, this will be considered before exiting. See Finally.
Example:
Code Block |
---|
pipeline: - exit: if: ${2 > 1} |
...
The command finally
can be used in a pipeline in order to make sure a set of commands is executed in any case at the very end of a pipeline. Even if an error has been occured or the pipeline execution has been cancelled by an exit
command. This approach is useful for example in case you need to cleanup data or would like to get informed about the pipeline execution result in any case.
...
Sometimes it is necessary to pause the execution flow of a pipeline for a certain amount of time. You can do so using the command wait
.
Example:
Code Block | ||
---|---|---|
| ||
pipeline: - log: message: "Lets wait 2 seconds..." - wait: ms: 2000 - log: message: "2 seconds later." |
...
In case you would like to make sure , a condition in the pipeline is true, you can use the assert
command to check that. In case the given condition is wrong, the pipeline execution will end and an error will the be thrown. This is especially useful in writing tests.
This example will end the pipeline execution since it expects the condition to be true
, but it is wrong:
Code Block | ||
---|---|---|
| ||
pipeline: - assert: true: "#${1 > 2}" message: "1 is not greater than 2!" |
...