You can use a switch
block as a selection mechanism that allows the value of
an expression to control the flow of a workflow's execution. If a value
matches, that condition's statement is executed.
If no matching expression is found, the workflow looks for an optional default
condition: - condition: true
. This should be the last condition in the
switch
block as they are evaluated in order and the first match found is
executed. If there is neither a match nor a default condition, the workflow
continues execution with the step that immediately follows.
Each switch
block can include a maximum of 50 conditions. Each expression must
evaluate to true or false. (Learn how to define and use expressions.)
For example, you can control the order of a workflow's execution by using a
switch
block to jump between steps
based on a conditional expression:
YAML
- STEP_NAME_A: switch: - condition: ${EXPRESSION_A} next: STEP_NAME_B - condition: ${EXPRESSION_B} next: STEP_NAME_C - condition: true next: STEP_NAME_C next: STEP_NAME_D
JSON
[ { "STEP_NAME_A": { "switch": [ { "condition": "${EXPRESSION_A}", "next": "STEP_NAME_B" }, { "condition": "${EXPRESSION_B}", "next": "STEP_NAME_C" } { "condition": true, "next": "STEP_NAME_C" } ], "next": "STEP_NAME_D" } } ]
You can also nest multiple steps inside a switch
block:
YAML
- STEP_NAME_A: switch: - condition: ${EXPRESSION} steps: - STEP_NAME_B: ... - STEP_NAME_C: ... next: STEP_NAME_D
JSON
[ { "STEP_NAME_A": { "switch": [ { "condition": "${EXPRESSION}", "steps": [ { "STEP_NAME_B": { ... } }, { "STEP_NAME_C": { ... } } ] } ], "next": "STEP_NAME_D" } } ]
Note that the steps
block is optional. It can contain the following:
assign
call
for
parallel
raise
return
steps
switch
try
For more information about defining a workflow's order of execution, see Jumps and Control the order of execution in a workflow.
Samples
These samples demonstrate the syntax.
Conditional jumps to specific steps
You might want a workflow to jump to a specific step when a condition is met. These two samples execute steps depending on the value returned by the first step of the workflow.
YAML
JSON
YAML
JSON
Switch structure with embedded steps
A switch structure can also be used to support direct execution of steps when a condition is met, without jumping to other steps. This improves the readability of a workflow by reducing the number of steps in the workflow definition.