Skip to main content

How control flow works

A workflow's tasks live in named blocks, and a run starts in the block its entry_point names, main by default. Control tasks run other blocks inside themselves: an if task enters the true or false block that matches its condition, if it names one, and a for_each task runs a block once for each item in a list.

How the example below moves between blocks. Tasks in a block start as soon as it is entered and there is room for them, unless start_on makes them wait for other tasks to complete.

Block model​

A workflow definition organizes tasks into named blocks. Each block is a dictionary of tasks that can run as a group, and the entry_point field names the block the run starts in.

A run enters any other block only through a control task. The control task runs that block inside itself and completes once the block's tasks have, and then the tasks that wait on the control task continue, back in the block the control task belongs to.

name: Control flow example
compute_type: cpu-4
inputs:
mode:
default: fast
entry_point: main
blocks:
main:
tasks:
step_one:
name: Step one
uses: my-account/my-[email protected]
router:
name: Router
start_on:
- step_one
if:
condition: ${{inputs.mode == 'fast'}}
true: fast_path
false: slow_path
each_file:
name: Each file
start_on:
- step_one
for_each:
items: ${{tasks.step_one.outputs.files}}
block: per_file
fast_path:
tasks:
do_fast:
name: Do fast
uses: my-account/fast-[email protected]
slow_path:
tasks:
do_slow:
name: Do slow
uses: my-account/slow-[email protected]
per_file:
tasks:
transform:
name: Transform
uses: my-account/transform-[email protected]
args:
file: ${{tasks.each_file.for_each.item}}

If tasks​

An if task evaluates its condition once, when it starts, and enters the block for the outcome: its true block when the condition is truthy, its false block when it is falsy. When it names no block for the outcome, it completes without entering one, and the tasks that wait on it continue. Its execution log, and the run's Workflow Graph, show which branch it took.

For Each tasks​

A for_each task resolves its list when it starts and runs its block once per item, each iteration a copy of the block with its own item and index. The iterations run at the same time, as far as the account's limits allow, and the rest wait as Pending until there is room; see Tasks that run at once.

The Control flow reference gives both tasks' fields and rules, and how they appear in the Workflow Graph.

Outputs and blocks​

A task reads the outputs of the tasks it waits on through start_on. Inside a block it can also read the outputs of the tasks the enclosing if or for_each task waits on, and each for_each iteration reads only its own copies of the block's tasks. Outputs stay inside their block: a task after an if or for_each task cannot read the outputs of the tasks inside it, so export a value as an environment variable to use it later in the run.

Nesting​

Blocks can contain further control tasks, so you can nest if and for_each tasks to any depth your workflow requires. A control task's block runs nested inside it, and the block's tasks can start_on only each other.

A failure inside a block is not contained by it: it fails every if and for_each task around it, and the run, as Failure propagation describes.

Next steps​