Branch, Skip, and Poll
A workflow does not have to run every step every time. This lesson picks one branch based on an input, skips a step when its condition is false, and repeats a check until it passes.
The workflow
bash
curl -fsSLO https://docs.dagu.sh/tutorial/dags/tutorial-05-control-flow.yamlyaml
# Dagu tutorial, lesson 5: https://docs.dagu.sh/tutorial/control-flow
description: Branch on a parameter, skip a step, and poll until ready
type: graph
params:
- name: environment
type: string
enum: [staging, prod]
default: staging
- name: notes
type: string
default: ""
steps:
- id: choose_target
action: router.route
with:
value: ${params.environment}
routes:
staging: [deploy_staging]
prod: [deploy_prod]
- id: deploy_staging
run: echo "Deployed to staging"
- id: deploy_prod
run: echo "Deployed to prod"
- id: wait_until_healthy
working_dir: ${context.paths.artifacts_dir}
run: |
echo "check" >> checks.txt
count=$(wc -l < checks.txt | tr -d ' ')
if [ "$count" -lt 3 ]; then
echo "Health check $count: not ready yet"
exit 1
fi
echo "Health check $count: healthy"
repeat_policy:
repeat: until
interval_sec: 1
limit: 10
depends: [deploy_staging, deploy_prod]
- id: publish_notes
run: echo "Release notes - ${params.notes}"
preconditions:
- condition: "${params.notes}"
expected: "re:.+"
depends: wait_until_healthy- Branch.
router.routecomparesvaluewith each route and runs the steps listed for the routes that match. The steps of the other routes are Skipped.deploy_staginganddeploy_prodhave nodepends, because the router starts them. - Poll.
repeat_policywithrepeat: untilrunswait_until_healthyagain, once a second, until it exits with 0, at most 10 times. The script here passes on the third try. - Skip.
preconditionsrunspublish_notesonly when thenotesparameter matches the regular expression.+, that is, when it is not empty. Otherwise the step is Skipped and the run still succeeds.
wait_until_healthy depends on both deploy steps. A step skipped by a router does not block its dependents.
Watch it run
publish_notesrunSkipped
No output
The router runs deploy_staging. wait_until_healthy runs three times, and publish_notes is skipped because notes is empty.
Run it yourself
bash
dagu start tutorial-05-control-flow.yamlTry it
- Run with
-- environment=prod. Which deploy step runs? - Run with
-- notes="Fixed the login bug". What doespublish_notesprint?
Answers
deploy_prodruns anddeploy_stagingis skipped.Release notes - Fixed the login bug. With a non-emptynotes, the precondition is true and the step runs.

