Skip to content

Branch, Skip, and Poll ​

Lesson 5 of 10 · 4 minutes

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.yaml
yaml
# 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.route compares value with each route and runs the steps listed for the routes that match. The steps of the other routes are Skipped. deploy_staging and deploy_prod have no depends, because the router starts them.
  • Poll. repeat_policy with repeat: until runs wait_until_healthy again, once a second, until it exits with 0, at most 10 times. The script here passes on the third try.
  • Skip. preconditions runs publish_notes only when the notes parameter 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 ​

2.3s / 2.3sSucceeded
publish_notesrunSkipped

No output

A recorded run of this lesson's DAG. Select a step to see its log.

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.yaml

Try it ​

  1. Run with -- environment=prod. Which deploy step runs?
  2. Run with -- notes="Fixed the login bug". What does publish_notes print?
Answers
  1. deploy_prod runs and deploy_staging is skipped.
  2. Release notes - Fixed the login bug. With a non-empty notes, the precondition is true and the step runs.

Go deeper: Control flow →

Dagu is open source under the GNU General Public License v3.0.