A machine-readable index of this documentation for AI agents is available at https://kubevela.io/llms.txt. Markdown versions of documentation pages are available by appending .md to their URLs.
Skip to main content
Version: Next

Repeating Steps with forEach

Some work is the same step done once per thing: scale a component in each region, deploy to each cluster in an inventory, notify each team. Without a loop, the workflow carries a copy of the step for each one:

steps:
- name: scale-us-east
type: scale-component
properties: { component: web, cluster: prod-us-east, replicas: 3 }
- name: scale-eu-west
type: scale-component
properties: { component: web, cluster: prod-eu-west, replicas: 2 }

forEach runs a step once per item in a list, so the workflow says what to repeat and what to repeat it over. Adding a region adds an item, not a step, and the workflow stops growing with the inventory:

apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: web
spec:
components:
- name: web
type: webservice
properties:
image: nginx:1.27
workflow:
steps:
- name: scale
type: scale-component # any step type, unchanged
properties:
component: web
forEach:
items:
- { cluster: prod-us-east, replicas: 3 }
- { cluster: prod-eu-west, replicas: 2 }
inputs:
- from: each.item.cluster
parameterKey: cluster
- from: each.item.replicas
parameterKey: replicas

Both run one region after the other, and a failure skips the regions after it. The difference is in the status: the loop is one step, scale, with its items, scale-0 and scale-1, as sub-steps.

forEach gives the list. inputs with from: each.item... hands each item to the step's parameters, the same way inputs and outputs pass data between any two steps. The step's definition needs no change to be looped.

forEach is new in v1.12 and has no feature gate: it needs vela-core v1.12 and its CRDs, which a Helm upgrade does not apply for you (see Upgrading). A WorkflowRun needs vela-workflow v0.8.0.

note

scale-component and announce-endpoints on this page stand for step definitions of your own. The other step types are KubeVela's built-ins.

The list​

Give exactly one of items and from.

FieldIs
itemsthe list itself: strings, numbers or objects, mixed if you like
froma workflow variable holding the list, written as in inputs[].from: regions or inventory.clusters
modeStepByStep (the default) or DAG. See Order and failure

A step using from waits, as a pending step, until an earlier step has produced that variable.

An empty list is allowed: the loop runs no items and the step succeeds. A from variable or an expression that gives [] does the same.

From a source​

Where expressions are turned on (the EnableCelExpressions feature gate, and the app.oam.dev/cel-expressions: "true" annotation on the Application while the platform requires opt-in with RequireCelExpressionOptIn, the default), items can be a single expression that reads a source or an earlier step's output:

spec:
sources:
- name: targets
type: configmap-local
properties:
name: rollout-targets # data.clusters: "prod-us-east,prod-eu-west"
workflow:
steps:
- name: deploy
type: apply-component
forEach:
items: '$(has(source.targets.data.clusters) ? source.targets.data.clusters.split(",") : [])'
properties:
component: web
inputs:
- from: each.item
parameterKey: cluster

A ConfigMap key may be missing, so the read needs a has() guard and a fallback (see Optional fields); without one, the Application is refused with source.targets.data.clusters may be absent and feeds forEach.items.

The expression is resolved once, when the loop starts, and the list is then fixed for the run (see below). It can read a source or an earlier step's output, $(steps.discover.outputs.clusters); until what it reads is ready, the loop waits. Where admission knows the type, a source field for instance, an expression that is not a list is refused when you apply. items is either a list or one expression on its own: text around an expression, or two expressions, would resolve to a string. Expressions can also sit inside a literal list, one per item: items: ["$(source.region.primary)", "eu-west-1"].

A WorkflowRun takes an expression in items too, when it is annotated workflowrun.oam.dev/cel-expressions: "true" and the workflow controller runs with EnableCelExpressions (off by default). It reads steps and context, since source belongs to KubeVela.

The list is fixed when the loop starts​

The list is read once, when the loop first runs, and kept for the rest of that run. A source that changes, or an upstream output that is rewritten, cannot reshuffle a loop that is part-way through. The next run of the workflow reads the list again.

Reading the current item​

Inside a loop, inputs[].from can start with each:

fromGives
each.itemthe whole item
each.item.<path>a field of an object item: each.item.cluster, each.item.spec.replicas
each.indexthe item's position, from 0
each.keythe name the item's steps carry: its key, or its position as a string ("0") when the loop has none

parameterKey works as it always does, including nested keys such as spec.replicas. each.index is a number, so it cannot go where only a string is accepted, such as a label value. each.key is a string, so it can: it is the usual way to name what an item creates after the item itself.

Writing a definition for loops

A step definition can also read context.each.item, context.each.index and context.each.key in its template. Prefer inputs where you can: it keeps the definition usable outside a loop.

each is reserved inside a loop, so an output there cannot be named each.

With property expressions​

Where expressions are on, a looped step's properties can read the item directly, and a looped group's sub-steps can decide per item whether to run:

- name: rollout
type: step-group
forEach:
items:
- { cluster: prod-us-east, enabled: true }
- { cluster: prod-eu-west, enabled: false }
subSteps:
- name: deploy
type: apply-component
if: '$(each.item.enabled)'
properties:
component: web
cluster: '$(each.item.cluster)'

each.item, each.index and each.key read as they do in inputs, and a literal list types them when you apply. Inside a looped group, $(steps.deploy.outputs.x) reads the output of this item's deploy.

Repeating several steps​

Put forEach on a step-group to run all of its sub-steps once per item:

- name: rollout
type: step-group
mode: StepByStep # sub-steps in order, within each item
forEach:
items: [us-east-1, eu-west-1, ap-south-1]
mode: DAG # every region at once
subSteps:
- name: deploy
type: apply-component
properties:
component: web
inputs:
- from: each.item
parameterKey: cluster
- name: verify
type: print-message-in-status
dependsOn: [deploy]
properties:
message: web deployed

The two modes are independent. forEach.mode decides how items run against each other; the group's own mode decides how one item's sub-steps run. For a rollout over regions:

group mode: DAG (default)group mode: StepByStep
forEach.mode: StepByStep (default)one region at a time; within a region, its steps start togetherone region at a time; within a region, its steps run in order
forEach.mode: DAGevery region at once; within each, its steps start togetherevery region at once; within each, its steps run in order

For a staged release, keep forEach.mode at StepByStep: a region that fails stops the regions after it. A group with no mode takes the workflow's mode.subSteps, and DAG when that is unset too, looped or not; under DAG, dependsOn between sub-steps still orders them within an item.

A looped group has no outputs of its own: declare them on its sub-steps, whose outputs are collected into lists. The group's own inputs are waited on before the loop starts, as for any group.

A sub-step's dependsOn naming another sub-step means the one in the same item. Only top-level steps can have forEach. On a sub-step the field is not part of the schema: kubectl apply, which validates fields strictly by default, refuses it with unknown field "spec.workflow.steps[1].subSteps[0].forEach". A client that does not ask for strict validation, such as vela up, gets that as a warning instead: the field is dropped and the sub-step runs once.

Order and failure​

SituationWhat happens
StepByStep, an item succeedsthe next item starts
StepByStep, an item failsthe items after it are skipped, as later steps are after a failed step
StepByStep, an item fails but is still being retriedit has not finished, so the loop waits on it
DAGevery item starts at once, with no cap on how many beyond the item limit. A failed item affects only itself: the other items run to the end
the EnableSuspendOnFailure gate is on, and an item fails after its retriesthe workflow suspends on that item, and the items after it wait rather than being skipped. An item whose step fails outright (a definition's #Fail) still ends the workflow
an item suspendsthe loop suspends. vela workflow resume <app> --step <loop> resumes every suspended item; --step <loop>-1 resumes one item (in a looped group, the item's sub-step: --step <loop>-1-<sub-step>)

The looped step's own if, timeout and dependsOn apply to the loop as a whole, not to each item. To leave some items out, filter the list before the loop, or, in a looped group, give a sub-step an if that reads each (see With property expressions).

caution

With the EnableSuspendOnFailure gate on, no step evaluates its if, sub-steps included, so filtering a loop with if leaves nothing out: every item runs, with no error. Filtering the list stays reliable, either before the loop or in items itself, which is not an if: items: '$(source.x.items.filter(i, i.enabled))'.

Restarting​

Restart from a failed item by its step name:

vela workflow restart <app> --step deploy-1
Restart fromWhat reruns
a failed item, StepByStepthat item and every item after it, with the same items as before
a failed item, DAGonly that item
a failed sub-step of an item in a looped groupthat sub-step and what runs after it: its dependents in a DAG group, the later sub-steps in a StepByStep one. The item's other succeeded sub-steps are kept: restarting rollout-1-verify keeps rollout-1-deploy
the failed looped step itselfthe whole loop, which reads the list again, so a from list or a source may give different items. This is the one exception to the list being fixed for a run

In every case the steps after the loop run again as usual. Only a failed item (in a looped group, its failed sub-step, such as rollout-1-verify) or a failed loop can be restarted; anything else is refused with can not restart from a non-failed step.

Status​

Each item shows up as a sub-step of the looped step:

LoopedItem steps are named
a single step scalescale-0, scale-1, ...
a group rollout with sub-step deployrollout-0-deploy, rollout-1-deploy, ...

No other step may have a name the loop could generate; that is refused at admission.

Naming items by a key​

By default an item's steps are named by its position. Give key to name them by the item's own values instead, so the status says which item is which. The example reads the item with $(each.item...), which needs expressions turned on; without them, pass the same fields with inputs as in Reading the current item:

- name: rollout
type: step-group
forEach:
items:
- {cluster: prod-us, namespace: shop}
- {cluster: prod-eu, namespace: shop}
key: [cluster, namespace]
subSteps:
- name: deploy
type: apply-component
properties:
component: shop
cluster: '$(each.item.cluster)'
namespace: '$(each.item.namespace)'
- name: verify
type: print-message-in-status
dependsOn: [deploy]
properties:
message: 'shop is up in $(each.key)'
LoopedWithout keyWith key: [cluster, namespace]
a single step scalescale-0scale-prod-us-shop
a group rollout with sub-step verifyrollout-0-verifyrollout-prod-us-shop-verify

key is a list of one to eight paths into each item, with the same dotted syntax as inputs[].from (spec.zone). One field is still a list: key: [name]. The values are joined with -. The item's outputs and its each variable are named the same way (rollout-prod-us-shop-endpoint), and you restart an item by its keyed step name. each.key reads the joined key. A key changes names only: each.index still counts from 0, and collected outputs keep the order of the items.

The loop fails, naming the items involved, if a key cannot name a step:

  • a value is missing, or is not a string or an integer;
  • the joined key holds anything but letters, digits, - and _, does not start and end with a letter or digit, or is over 63 characters. A . is not allowed, since inputs[].from reads a path by its dots;
  • two items have the same joined key: {a: x-y, b: z} and {a: x, b: y-z} both give x-y-z;
  • a key and a sub-step name together repeat another item's step name: key a with sub-step b-c, and key a-b with sub-step c, both name a step rollout-a-b-c. Output names are checked the same way, since an item's outputs are kept as variables named <loop>-<key>-<output>.

A path that is empty or malformed (spec.), or the same path twice, is refused when you apply; a path the items do not have fails the loop when it runs.

A key is never rewritten to make it fit. Where the values need reshaping, build a key field in the source or an earlier step, and use that.

When an item fails​

What a failed item does to the items around it depends on the mode; see Order and failure. A failed loop's message names the item that failed, so the cause shows in the Application's status without opening each sub-step:

- name: deploy
phase: failed
reason: Action
message: "item 1 (us-east) failed: region us-east is unreachable"
subSteps:
- name: deploy-0
phase: succeeded
- name: deploy-1
phase: failed
reason: Action
message: region us-east is unreachable
- name: deploy-2
phase: skipped
reason: Skip

The message gives the item's index and value, and the failing step's own message. In a group it also names the sub-step, as in item 1 (us-east) failed at verify: .... An item that is not a string is shown as JSON, cut to 60 characters. In a keyed loop the message shows the key instead: item 1 (prod-us-shop) failed at verify: .... With DAG, where several items can fail, the message names the first of them by index and counts the rest: ...; 2 more items failed. Their messages are on their own sub-steps.

To work through a failed item:

  1. Read the loop's message for the item and the error. The item's sub-step has the same error, and the rest of its status.
  2. Fix the cause, then restart from the item's step, deploy-1. It reruns with the same item.
note

To dig further, look at what the item produced. The workflow keeps each item's {item, index, key} and its outputs as variables, deploy-1-each and deploy-1-<output>, in the context ConfigMap named in the workflow status's contextBackend. The collected list under the output's own name is written only once every item succeeds.

Outputs​

An output declared inside a loop is per item while the loop runs. Once the loop succeeds, the output is also written as a list under its own name, one entry per item, with null where an item did not produce it:

- name: scale
type: scale-component
properties:
component: web
forEach:
items: [prod-us-east, prod-eu-west]
inputs:
- from: each.item
parameterKey: cluster
outputs:
- name: endpoint
valueFrom: output.endpoint
- name: announce
type: announce-endpoints
inputs:
- from: endpoint # ["https://...", "https://..."]
parameterKey: endpoints

Pick output names no other step uses, as you would anywhere in a workflow: two steps setting the same variable to different values fail.

Limits​

A loop can have up to 50 items, since every item is recorded in the Application's status. The limit is a controller flag, --max-for-each-items, set through the chart value workflow.step.forEachMaxItems.

Upgrading​

forEach is a new field in the Application, ApplicationRevision, Workflow and WorkflowRun CRDs, read by the v1.12 controllers. Helm does not upgrade CRDs, so apply the new ones when you upgrade vela-core to v1.12:

helm repo add kubevela https://kubevela.github.io/charts
helm repo update
helm show crds kubevela/vela-core --version 1.12.0 | kubectl apply --server-side --force-conflicts -f -

The WorkflowRun CRD comes from vela-workflow, not vela-core. The vela-workflow addon applies its own CRD, so once the addon carries vela-workflow v0.8.0, vela addon upgrade vela-workflow is enough. If you installed the vela-workflow Helm chart instead, apply its CRDs the same way:

helm show crds kubevela/vela-workflow --version <version> | kubectl apply --server-side --force-conflicts -f -

Against an older CRD, forEach is an unknown field: kubectl apply refuses it, and a client without strict field validation, such as vela up, has it dropped with only a warning. A step reading each.item through inputs then waits, pending on Pending on Input: each.item; one that reads nothing from the loop runs once. vela-core logs a warning at startup when the installed Application or Workflow CRD lacks the field: does not declare forEach on workflow steps, so the API server drops it and a looped step runs once.

When something is wrong​

MessageMeans
needs exactly one of items and fromgive the list one way, not both or neither
supported values: "StepByStep", "DAG" on forEach.modethe modes are StepByStep and DAG, in that case
must be a listitems is an object or a plain string. For from, the variable holds one of those, or null
a $( ) expression here needs expressions turned onturn on EnableCelExpressions, and annotate the Application (app.oam.dev/cel-expressions) while RequireCelExpressionOptIn is on, or the WorkflowRun (workflowrun.oam.dev/cel-expressions)
must be a list or one $( ) expression that resolves to oneitems has text around an expression, or two expressions. Use one expression on its own, or a list
may be absent and feeds forEach.items; guard it with has(...)the expression reads a field that may be missing, such as a ConfigMap key. Guard it: has(x) ? x : []
was not resolved; $( ) expressions are not turned on for this workflowthe same, caught when the loop runs
type mismatch: expression ... is string but forEach.items expects listthe source field is not a list. Check vela def show <source-type>
over the limit of 50split the list, or raise workflow.step.forEachMaxItems
clashes with the names forEach step ... generatesrename the other step
is named like the passes of forEach step ...a looped step is named like another loop's items, such as scale-0 beside a loop scale. Rename it
clashes with the variables forEach step ... keeps for its passesan output elsewhere is named like one of the loop's per-item variables. Rename the output
is not a path into the itema key path is empty or malformed, such as spec.
items 0 and 1 have the same key "..."the key fields do not tell those items apart. Add a field to key
items 0 and 1 both name a step ...a key and a sub-step name join into another item's step name. Rename the sub-step or change the key
items 0 and 1 both name a variable ...a key and an output name join into another item's variable name. Rename the output or change the key
item 0: key <path> must be a string or an integer, got ...a key field holds a float, a bool, an object or a list
item 0: key "..." is over 63 charactersthe joined key is too long to name a step. Key on fewer or shorter fields
item 0 has no value at key ...the item lacks that field, or is not an object
item 0: key "..." may only hold letters, digits, '-' and '_'the value holds a ., a space or similar. Use another field, or build one
a step-group with forEach has no outputs of its ownmove the outputs onto its sub-steps
Pending on forEach.from: ...no earlier step has produced that variable yet
Pending on Input: each.item on a step with forEach in its specthe installed CRD predates forEach and dropped it. See Upgrading

Next​