Episode
Understanding YAML
Questions
- What is YAML?
Objectives
- Learn about YAML
YAML
YAML (YAML Ain’t Markup Language (a recursive acronym), originally standing for Yet Another Markup Language) is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. CI systems’ modus operandi typically rely on YAML for configuration. We’ll cover, briefly, some of the native types involved and what the structure looks like.
Scalars
number-value: 42
floating-point-value: 3.141592
boolean-value: true # on, yes -- also work
# strings can be both 'single-quoted` and "double-quoted"
string-value: 'Bonjour'
python-version: "3.10"
unquoted-string: Hello World
hexadecimal: 0x12d4
scientific: 12.3015e+05
infinity: .inf
not-a-number: .NAN
null: ~
another-null: null
key with spaces: value
datetime: 2001-12-15T02:59:43.1Z
datetime_with_spaces: 2001-12-14 21:59:43.10 -5
date: 2002-12-14: . This is important for YAML parsing and is a common mistake.python-version: 3.10 will automatically
be converted to python-version: 3.1 (notice 3.1 instead of 3.10). The conversion will lead to unexpected failures
as your CI will be running on a version not specified by you. This behavior resulted in several failed jobs after the release
of Python 3.10 on CI services. The conversion (and the build failure) can be avoided by converting the floating-point numbers
to strings - python-version: "3.10".Lists and Dictionaries
Elements of a list start with a “- " (a dash and a space) at the same indentation level.
jedis:
- Yoda
- Qui-Gon Jinn
- Obi-Wan Kenobi
- Luke SkywalkerElements of a dictionary are in the form of “key: value” (the colon must be followed by a space).
jedi:
name: Obi-Wan Kenobi
home-planet: Stewjon
species: human
master: Qui-Gon Jinn
height: 1.82mInline-Syntax
Since YAML is a superset of JSON, you can also write JSON-style maps and sequences.
episodes: [1, 2, 3, 4, 5, 6, 7]
best-jedi: {name: Obi-Wan, side: light}Multiline Strings
In YAML, there are two different ways to handle multiline strings. This is useful, for example, when you have a long code block that you want to format in a pretty way, but don’t want to impact the functionality of the underlying CI script. In these cases, multiline strings can help. For an interactive demonstration, you can visit https://yaml-multiline.info/.
Put simply, you have two operators you can use to determine whether to keep newlines (|, exactly how you wrote it) or to remove newlines (>, fold them in). Similarly, you can also choose whether you want a single newline at the end of the multiline string, multiple newlines at the end (+), or no newlines at the end (-). The following is a summary of some variations:
folded_no_ending_newline:
script:
- >-
echo "foo" &&
echo "bar" &&
echo "baz"
- echo "do something else"
unfolded_ending_single_newline:
script:
- |
echo "foo" && \
echo "bar" && \
echo "baz"
- echo "do something else"Nested
requests:
# first item of `requests` list is just a string
- http://example.com/
# second item of `requests` list is a dictionary
- url: http://example.com/
method: GETComments
Comments begin with a pound sign (#) and continue for the rest of the line:
# This is a full line comment
foo: bar # this is a comment, tooKey Points
- YAML is a plain-text format, similar to JSON, useful for configuration
- YAML is a superset of JSON, so it contains additional features like comments, while still supporting JSON.