Skip to content

Commit

Permalink
docs: Improve documentation on loops
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Clucas <alan@clucas.org>
  • Loading branch information
Joibel committed Mar 10, 2023
1 parent 9510568 commit de07bcf
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions docs/walk-through/loops.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Loops

When writing workflows, it is often very useful to be able to iterate over a set of inputs as shown in this example:
When writing workflows, it is often very useful to be able to iterate over a set of inputs.

There are two basic ways of running a template multiple times.
- `withItems` takes a list of things to work on. Either
- plain, single values, which are then usable in your template as '{{item}}'
- a json object where each element in the object can be addressed by it's key as '{{item.key}}'
- `withParams` takes a json array of items, and iterates over it - again the items can be objects like with `withItems`. This is very powerful, as you can generate the json in another step in your workflow, so creating a dynamic workflow.

This example is the simplest. We are taking a basic list of items and iterating over it with `withItems`. It is limited to one varying field for each of the workflow templates instatiated.

```yaml
apiVersion: argoproj.io/v1alpha1
Expand Down Expand Up @@ -32,7 +40,7 @@ spec:
args: ["{{inputs.parameters.message}}"]
```
We can also iterate over sets of items:
If we'd like more than one piece of information in each workflow, you can instead pass a json object to `withItems` and then address the elements by key, as shown in this example.

```yaml
apiVersion: argoproj.io/v1alpha1
Expand All @@ -42,7 +50,9 @@ metadata:
spec:
entrypoint: loop-map-example
templates:
- name: loop-map-example
- name: loop-map-example parameter specifies the list to iterate over
steps:
- - name: test-linux
template: cat-os-release
Expand All @@ -69,7 +79,7 @@ spec:
args: [/etc/os-release]
```

We can pass lists of items as parameters:
This example does exactly the same job as the previous example, but using `withParam` to pass the information as a JSON array argument, instead of directly hardcoded into the template.

```yaml
apiVersion: argoproj.io/v1alpha1
Expand Down Expand Up @@ -117,7 +127,7 @@ spec:
args: [/etc/os-release]
```

We can even dynamically generate the list of items to iterate over!
Finally, the most powerful form of this is to generate that JSON array of objects dynamically in one step, and then pass it to the next step so that the number and values used in the second step are only calculated at runtime.

```yaml
apiVersion: argoproj.io/v1alpha1
Expand Down

0 comments on commit de07bcf

Please sign in to comment.