Control flow
This page is the reference for the if and for_each control-flow constructs in workflow YAML.
If
An if task routes execution to one of two blocks based on a condition expression.
Syntax
router_task:
name: Router task
if:
condition: <expression>
true: <block-name> # optional when false is given
false: <block-name> # optional when true is given
Fields
| Field | Type | Required | Description |
|---|---|---|---|
condition | string (expression) | Yes | Evaluated once, when the task starts. If truthy, the task enters the true block; if falsy, the false block. |
true | string | No* | Name of the block to enter when condition is truthy. If omitted and the condition is truthy, the if task completes without entering a block. |
false | string | No* | Name of the block to enter when condition is falsy. If omitted and the condition is falsy, the if task completes without entering a block. |
* An if task must give true, false, or both.
Validation rules
- Each block the task names must be defined in the same workflow, and cannot be the
entry_pointblock or a block that contains the if task, directly or through other control tasks. - A control task runs no image, so an if task, like a
for_eachtask, cannot declareuses,argsorimage_credentials. - A task cannot declare both
ifandfor_each. - The
conditionexpression must parse; see Upload checks.
How it runs
- The if task completes once every task of the block it entered has completed, or straight away when it entered no block. Tasks that
start_onthe if task then run. - The if task's execution log records the outcome as
Condition matched:,TrueorFalse, andChosen block:, the block it entered or(none). - If
conditioncannot be evaluated, for example because it givesfromJSONtext that is not JSON, the if task fails, and the run fails with it. The if task's failure reason and execution log say why. - If a task in the block fails, the if task ends Failed, and the run fails with it; see Failure propagation.
- Tasks in the block can read the outputs of the tasks the if task waits on, as well as those of the tasks in the block they wait on.
- No task after the if task can read the outputs of tasks inside its block. To use a value from the block later in the run, export it as an environment variable.
Example
blocks:
main:
tasks:
classify:
name: Classify
check:
name: Check
start_on:
- classify
if:
condition: ${{tasks.classify.outputs.category == 'important'}}
true: priority_path
false: standard_path
priority_path:
tasks:
urgent:
name: Urgent
standard_path:
tasks:
normal:
name: Normal
For Each
A for_each task repeats a block once for each element of a list.
Syntax
loop_task:
name: Loop task
for_each:
items: <expression>
block: <block-name>
Fields
| Field | Type | Required | Description |
|---|---|---|---|
items | string (expression) | Yes | Expression that resolves to a JSON array. Each element becomes one iteration. Other values are described under How it runs below. |
block | string | Yes | Name of the block to run for each element |
Loop variables
Inside the iterated block, ${{tasks.<for_each_task_key>.for_each.item}} is the current element and ${{tasks.<for_each_task_key>.for_each.index}} its zero-based index; see For Each loop variables.
Validation rules
- The value of
blockmust be a block defined in the same workflow, and cannot be theentry_pointblock or a block that contains thefor_eachtask, directly or through other control tasks. - Like an if task, a
for_eachtask cannot declareuses,argsorimage_credentials, or declareifas well. - The
itemsexpression must parse; see Upload checks.
How it runs
itemsis resolved when the task starts. A JSON array runs one iteration per element, and an empty value runs none and completes the task. A value wrapped in{ }, a JSON object, fails the task, and so does a value that starts with[but is not a valid JSON array, or anitemsexpression that cannot be evaluated; the run fails with it, and the task's failure reason and execution log say which. Any other value runs a single iteration with that value.- The execution log lists the resolved items, as
Resolved items (N):followed by each item. - All iterations are created together and run at the same time, as far as the account's limits allow; the rest wait as Pending until there is room (see Tasks that run at once).
- The
for_eachtask completes once every task of every iteration has completed. Tasks thatstart_onthefor_eachtask then run. - If a task in any iteration fails, the
for_eachtask ends Failed and the run fails: every unfinished task in the run is stopped, in this task's other iterations and elsewhere; see Failure propagation. - Tasks in the block can read the outputs of the tasks the
for_eachtask waits on, and of the tasks in the block they wait on. A task in the block is always read from the reader's own iteration, sotasks.<name>.outputs.<key>gives its own iteration's value. - No task after the
for_eachtask can read the outputs of tasks inside its block. To use a value from an iteration later in the run, export it as an environment variable. Environment variables are shared by the whole run, so iterations that export the same name overwrite each other, and the run keeps the value written last.
Example
blocks:
main:
tasks:
fetch_list:
name: Fetch list
process_each:
name: Process each
start_on:
- fetch_list
for_each:
items: ${{tasks.fetch_list.outputs.files}}
block: per_file
per_file:
tasks:
transform:
name: Transform
args:
file: ${{tasks.process_each.for_each.item}}
index: ${{tasks.process_each.for_each.index}}
Nesting
Control tasks can appear inside blocks that are themselves targets of other control tasks: the block of a for_each task may contain an if task, and the block of an if task a for_each task. There is no enforced nesting depth limit, but deeply nested flows can be harder to read in the Workflow Graph.
UI representation
A run's page shows control tasks in its Workflow Graph and in its Tasks card.
- If task — a node with an
IFbadge. The node names the branch its condition took,TrueorFalse, and, expanded, it shows the tasks of the block it entered. - For Each task — a node with a
FOREACHbadge. Expanded, it shows one group per iteration, labelled with its item, orIteration Nwhen the item is empty. Each task in an iteration shows#N, its zero-based index. - Expanding — click the node, or its Expand button, to show the tasks inside it, and again to hide them. A control task that already has tasks inside it when you open the run starts collapsed; one whose tasks appear while you watch shows them.
- Before its tasks exist — an if or
for_eachtask that has not yet created its inner tasks readsPending inner tasks.... An if task that entered no block, or afor_eachtask with no items, finishes with no inner tasks. - Tasks card — lists every task in the run, the tasks inside a control task indented under it, and each iteration's tasks with
#N.