Skip to main content

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​

FieldTypeRequiredDescription
conditionstring (expression)YesEvaluated once, when the task starts. If truthy, the task enters the true block; if falsy, the false block.
truestringNo*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.
falsestringNo*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_point block 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_each task, cannot declare uses, args or image_credentials.
  • A task cannot declare both if and for_each.
  • The condition expression 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_on the if task then run.
  • The if task's execution log records the outcome as Condition matched:, True or False, and Chosen block:, the block it entered or (none).
  • If condition cannot be evaluated, for example because it gives fromJSON text 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
uses: my-account/classify-[email protected]
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
uses: my-account/urgent-[email protected]

standard_path:
tasks:
normal:
name: Normal
uses: my-account/normal-[email protected]

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​

FieldTypeRequiredDescription
itemsstring (expression)YesExpression that resolves to a JSON array. Each element becomes one iteration. Other values are described under How it runs below.
blockstringYesName 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 block must be a block defined in the same workflow, and cannot be the entry_point block or a block that contains the for_each task, directly or through other control tasks.
  • Like an if task, a for_each task cannot declare uses, args or image_credentials, or declare if as well.
  • The items expression must parse; see Upload checks.

How it runs​

  • items is 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 an items expression 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_each task completes once every task of every iteration has completed. Tasks that start_on the for_each task then run.
  • If a task in any iteration fails, the for_each task 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_each task 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, so tasks.<name>.outputs.<key> gives its own iteration's value.
  • No task after the for_each task 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
uses: my-account/list-files-[email protected]
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
uses: my-account/transform-[email protected]
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 IF badge. The node names the branch its condition took, True or False, and, expanded, it shows the tasks of the block it entered.
  • For Each task — a node with a FOREACH badge. Expanded, it shows one group per iteration, labelled with its item, or Iteration N when 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_each task that has not yet created its inner tasks reads Pending inner tasks.... An if task that entered no block, or a for_each task 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.