Explore the essentials of YAML syntax and structure used in GitHub Actions, learn key principles for workflow configuration, and identify common errors to ensure reliable automation in your projects.
Which line correctly declares a key-value pair in a YAML workflow file?
Explanation: In YAML files, key-value pairs are declared by separating the key and the value with a colon and a space, as in the correct answer. Using an equals sign, arrow, or adding spaces before or after the colon is incorrect and will result in a parsing error. Option B uses an equal sign, which is not valid in YAML. Option C uses an arrow, which is also not valid. Option D places a space before the colon, which may cause the key to be misinterpreted.
How does YAML allow you to reuse a set of steps in multiple jobs within a workflow?
Explanation: Anchors and aliases let you define reusable blocks in YAML, reducing duplication by referencing a previously defined anchor with an alias. Declaring variables does not let you repeat complex blocks like steps. Indented lists are for basic data structures and not for reuse. Commenting sections only adds notes and has no effect on the workflow structure.
What happens if you mix tabs and spaces for indentation in a YAML workflow file?
Explanation: Mixing tabs and spaces is not allowed in YAML and will cause a syntax error, preventing the workflow from running. YAML strictly requires using spaces for indentation. If mixed, the parser stops processing and reports an error. Option B is incorrect because the workflow will not run. Option C is not correct as none of the steps will execute. Option D is wrong as YAML parsers do not automatically fix indentation.
In a YAML workflow file, which way should you declare a list of environment variables?
Explanation: To declare environment variables in a YAML workflow, you use nested key-value pairs under the 'environment' key, properly indented. Option D is the correct format for specifying multiple environment variables. Option A uses a list syntax, which is not suitable for key-value pairs like environment variables. Option B attempts a single-line declaration, but with a syntax invalid in YAML. Option C mixes list and dictionary syntax incorrectly.
In YAML used for workflow conditions, which value will be interpreted as a Boolean ‘true’?
Explanation: YAML recognizes unquoted true as a Boolean true, which is interpreted as a Boolean in conditions. Quoted 'true' is treated as a string, not a Boolean. 'yes' and 'on' are sometimes interpreted as Boolean true in plain YAML, but workflow parsers usually require 'true' or 'false' for clarity. Choosing the true Boolean value helps prevent ambiguity in workflow behaviors.