DOC_INDEX
THEME
DOCS/Learn OrchStep/Branching

Branching

if / elif / else and switch / case - routing workflows on variables and step results

Steps can branch two ways: an if/elif/else chain or a switch on one value. Both accept Go-template expressions ({{ gt vars.score 80 }}) or JavaScript (vars.score > 80).

if / elif / else

A branch either delegates to another task or runs inline then steps:

name: grading
defaults:
  score: 85

tasks:
  main:
    steps:
      - name: grade
        if: '{{ gt vars.score 90 }}'
        task: grade_a
        elif:
          - if: '{{ gt vars.score 80 }}'
            task: grade_b
        else:
          task: grade_f

  grade_a:
    steps:
      - name: result
        func: shell
        do: echo "A"
  grade_b:
    steps:
      - name: result
        func: shell
        do: echo "B"
  grade_f:
    steps:
      - name: result
        func: shell
        do: echo "F"

With score: 85 this runs grade_b - verified. Inline form:

- name: gate
  if: '{{ eq vars.environment "production" }}'
  then:
    - name: extra_check
      func: shell
      do: echo "production checks..."

switch / case

Cleaner than an elif ladder when you match one value. when: takes a scalar or an array (array = OR):

- name: pick
  switch:
    value: '{{ vars.environment }}'
    cases:
      - when: production
        then:
          - name: prod_step
            func: shell
            do: echo "production path"
      - when: [dev, staging]
        then:
          - name: nonprod_step
            func: shell
            do: echo "non-production path"
            outputs:
              mode: "nonprod"
    default:
      - name: unknown_step
        func: shell
        do: echo "unknown environment"
  outputs:
    mode: '{{ steps.nonprod_step.mode }}'

Note the last two lines: outputs of steps inside a case are lifted to the outer world through the switch step's own outputs: mapping - afterwards other steps read steps.pick.mode. (Verified behavior.)

Both expression styles

if: '{{ and (eq vars.status "ok") (gt vars.count 5) }}'   # Go template
if: 'vars.status === "ok" && vars.count > 5'              # JavaScript

One verified gotcha: contains takes the needle first - {{ contains "READY" steps.poll.output }}.

Run it

orchstep run --var score=72

Where to go next

Loops - repeat steps over lists, counts and ranges.