foreach directive in Keep workflows allows you to iterate over a list of items and perform actions for each item. This is particularly useful for processing multiple results returned by a step or performing actions on a collection of entities.
Key Features
- Dynamic Iteration: Iterate over any list or array returned by a step or defined in the workflow.
- Scoped Variables: Each iteration exposes the current item under the
foreachvariable, allowing you to access its properties directly. - Action Chaining: Multiple actions can use
foreachto work sequentially on the same list of items.
Defining a foreach
To use foreach, include it as part of an action. The value of foreach should be a reference to the list you want to iterate over.
Example Workflow with foreach
- The
get-podsstep retrieves a list of Kubernetes pods. - The
foreachiterates over theresultsreturned by theget-podsstep. - For each pod, it prints its
name,namespace, andstatus.
Using foreach Variables
The foreach variable provides scoped access to the current item in the iteration.
Example of Scoped Variables
{{ foreach.value }}refers to the current item in the list.- Access properties like
metadata.name,metadata.namespace, andstatus.phase` dynamically.
Using Conditions with foreach
You can combine foreach with if conditions to filter or act selectively.

