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

YAML Lists and Maps

Nesting data.

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

Workflows nest data a lot. A job contains steps, which is a list, and each step contains keys, which is a map. Let's get comfortable with lists of maps, because you'll see them in every workflow.

YAML Lists and Maps
From the video: YAML Lists and Maps
  • Workflows nest data
  • Lists of maps
  • In every workflow

A list of maps

Steps is a list, so each item starts with a dash. And each item is a map, with keys like name and run. The keys of one item line up under each other. This one shape, a list of maps, is how every workflow's steps are written. The short form, with square brackets, is a list on one line.

steps.ymlyaml
steps:
  - name: Say hello
    run: echo hello
  - name: Show the date
    run: date
  - name: Pick branches
    branches: [main, release]

Checking the shape

Python shows that steps is a list of three maps. And with a small loop, we print each step's name. Notice the last step's branches became a proper list, even though it was written on one line, with square brackets.

Terminal
python3 -c "import yaml; d = yaml.safe_load(open('steps.yml')); print(type(d['steps']).__name__, len(d['steps']))"
list 3
python3 -c "import yaml; [print('-', s['name']) for s in yaml.safe_load(open('steps.yml'))['steps']]"
- Say hello
- Show the date
- Pick branches
python3 -c "import yaml; print(yaml.safe_load(open('steps.yml'))['steps'][2]['branches'])"
['main', 'release']

Summary

A dash starts a list item, and a list item can be a map with its own keys. Square brackets write a list on one line. Next, the YAML mistakes that catch everyone.

Key takeaways
  • - starts an item
  • Lists of maps
  • [a, b] on one line
Next up: YAML Gotchas — tabs, colons, and surprises