AI Hints
CI/CD with GitHub Actions · Lesson 13 of 138

YAML Gotchas

Tabs, colons, and surprises.

⏱ 2 min read▶ Video at 14:01🎓 Free
Watch this lesson in the CI/CD with GitHub Actions courseWatch on YouTube ↗Full CI/CD with GitHub Actions course

YAML is friendly, but it has a few traps. A wrong indent, a tab, or a value that YAML guesses wrongly can break a workflow, with a confusing error. Let's see the common ones, so they never surprise you.

YAML Gotchas
From the video: YAML Gotchas
  • Friendly, with traps
  • Indents, tabs, guesses
  • Spot them early

Bad indentation

This file has a step indented by one extra space. Cat dash A shows every space, with a dollar sign at the end of each line. When Python reads it, YAML refuses, and points at the line and column where it got confused. GitHub does the same thing, and the workflow doesn't run at all. Always indent with two spaces, and never with tabs.

Terminal
cat -A bad.yml
steps:$
  - name: one$
    run: echo one$
   - name: two$
    run: echo two$
python3 -c "import yaml; yaml.safe_load(open('bad.yml'))" 2>&1 | tail -4
yaml.parser.ParserError: while parsing a block collection
  in "bad.yml", line 2, column 3
expected <block end>, but found '<block sequence start>'
  in "bad.yml", line 4, column 4

Values YAML guesses

YAML guesses the type of every value. Cat shows our file. On is a famous one. In older YAML rules, on means true, so the key name changes. Version three point ten becomes the number three point one, which is wrong. And the zip code, starting with zero, is read as an octal number, six six eight! Putting values in quotes keeps them exactly as written.

Terminal
cat guess.yml
on: push
python: 3.10
quoted: "3.10"
zip: 01234
python3 -c "import yaml; print(yaml.safe_load(open('guess.yml')))"
{True: 'push', 'python': 3.1, 'quoted': '3.10', 'zip': 668}

Rules of thumb

Indent with two spaces. Never use tabs. Quote version numbers, and anything with a colon, or a hash sign, which starts a comment. And GitHub reads the on key correctly as the events key, so you can write on safely in workflows. When in doubt, add quotes.

keyvalue
Indent2 spaces, no tabs
Versionsalways quote: "3.10"
# and :quote them
When in doubtadd quotes

Summary

Wrong indents break the file, and YAML guesses value types. Quote versions and anything unusual. Next, writing long commands on several lines.

Key takeaways
  • 2 spaces, no tabs
  • Quote "3.10"
  • Read the error's line
Next up: Multi-line Strings — pipe and fold