Switch icon
Return icon

Switch tasks depending on a specific value

Branch Kestra workflows on input values with the Switch task. Run different task sets per case, with a default fallback, all declared in YAML.

Categories
Core
id: switch
namespace: company.team

inputs:
  - id: string
    type: STRING
    defaults: B

tasks:
  - id: switch
    type: io.kestra.plugin.core.flow.Switch
    value: "{{ inputs.string }}"
    cases:
      A:
        - id: a
          type: io.kestra.plugin.core.debug.Return
          format: The input is {{ inputs.string }}
      B:
        - id: b
          type: io.kestra.plugin.core.debug.Return
          format: The input is {{ inputs.string }}
    defaults:
      - id: default
        type: io.kestra.plugin.core.debug.Return
        format: This is the default case

Build conditional, multi-path workflows in Kestra using the io.kestra.plugin.core.flow.Switch task. This blueprint shows the canonical pattern for routing execution: an input string is evaluated against a set of cases, and a different list of tasks runs for each match, with a default fallback when no case applies. Use it any time a single workflow needs to react differently to events, environments, payload fields, or user choices without splitting logic across several flows.

How it works

The flow declares a single input string (default B) and one task, switch, of type io.kestra.plugin.core.flow.Switch. The task evaluates value: "{{ inputs.string }}" and dispatches to one of three branches:

  1. Case A runs task a (io.kestra.plugin.core.debug.Return) printing the input value.
  2. Case B runs task b (io.kestra.plugin.core.debug.Return) printing the input value.
  3. defaults runs task default returning a literal message when the input matches no case.

Because Switch is a flowable task, each case can hold a full list of child tasks, not just one, and child tasks execute in order.

What you get

  • A working template for if/elif/else style branching in Kestra YAML.
  • A clear example of cases: keyed by string values plus a defaults: fallback.
  • A pattern that scales from two branches to many without nested conditionals.
  • A safe starting point for environment-aware flows (dev, staging, prod) and payload-driven routing.

Who it's for

  • Data and platform engineers building flows that behave differently per tenant, region, or environment.
  • Workflow authors migrating from tools where branching required separate DAGs or external schedulers.
  • Teams that want declarative routing logic in version control instead of branching inside scripts.

Why orchestrate this with Kestra

Switch logic is normally buried inside application code or bash scripts, which makes it invisible to operators. Kestra exposes each branch as a first-class set of tasks with their own logs, retries, and lineage in the Gantt and topology views. Combine Switch with event triggers (webhook, Kafka, Pub/Sub, S3) so that the same flow handles many event shapes, with declarative YAML, per-task retry policies, and replay from any case. No external scheduler offers this level of visibility into conditional branches.

Prerequisites

  • A running Kestra instance (Open Source or Enterprise).
  • Familiarity with Kestra inputs and Pebble expressions like {{ inputs.string }}.

Secrets

This blueprint uses no secrets. Add {{ secret('NAME') }} references inside any case when you extend it to call external systems.

Quick start

  1. Copy this flow into a namespace such as company.team.
  2. Execute it with the default input B and confirm task b runs.
  3. Re-execute with input A to trigger task a, then with input C to hit the default branch.
  4. Inspect the Gantt view to see which case ran and which tasks were skipped.

How to extend

  • Replace Return tasks with real work: API calls, SQL, file moves, notifications.
  • Add more cases keyed by environment (dev, staging, prod) and load credentials per case.
  • Drive value from a webhook payload, a previous task output, or a label, not just an input.
  • Nest a Switch inside another flowable task (Parallel, ForEach) for multi-dimensional routing.
  • Pair with If tasks for boolean checks and Switch for multi-way fan-out.

Links

Share this Blueprint
See How

New to Kestra?

Use blueprints to kickstart your first workflows.