Sub topic 12
Quoting and expression pitfalls
This chapter assumes you can read a workflow file with on, jobs and steps, and that you know basic YAML scalars. Most broken if conditions come from one of three places: YAML reads the line before GitHub does, string literals are quoted wrongly, or a string is treated as a boolean.
YAML reads the line first
GitHub Actions parses your file as YAML before it evaluates any expression. A condition that is valid as an expression can still be invalid YAML.
A leading ! is read as a YAML tag, so this step fails to load:
steps:
- name: Skip release tags
if: !startsWith(github.ref, 'refs/tags/')
run: echo "Not a tag build"Wrap the expression in ${{ }}, or quote the whole value:
steps:
- name: Skip release tags
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
run: echo "Not a tag build"A colon followed by a space inside a plain value has a similar effect, because YAML sees a new key. This happens when you compare against text such as a commit message:
steps:
- name: Run on fix commits
if: "contains(github.event.head_commit.message, 'fix: ')"
run: echo "Fix commit"Without the outer double quotes, the : inside the string breaks the YAML.
Expression quoting rules
Inside an expression, string literals must use single quotes. Double quotes are a syntax error. That is why the outer YAML quotes above are double and the inner expression quotes are single.
| Written | Result |
|---|---|
| `github.ref == 'refs/heads/main'` | Works |
| `github.ref == "refs/heads/main"` | Syntax error |
| `${{ github.ref == 'refs/heads/main' }}` | Works |
| `${{ github.ref }} == 'refs/heads/main'` | Always true |
The last row is the dangerous one. When ${{ }} is mixed with other text, GitHub substitutes the value into a string. The result is a non-empty string, and a non-empty string is truthy. The condition passes on every branch. Put the entire expression inside one ${{ }}, or leave out the wrapper.
Outputs are strings
Step outputs, env values and inputs from workflow_dispatch can all arrive as text. The text 'false' is a non-empty string, so a bare reference to it is truthy.
steps:
- name: Decide whether to run extra checks
id: check
run: echo "extra=true" >> "$GITHUB_OUTPUT"
- name: Extra checks
if: steps.check.outputs.extra == 'true'
run: ./extra-checks.shAlways compare to the literal text, as the == 'true' check does here. A missing output becomes an empty string, which is falsy, so an unset output skips the step quietly.
String comparison with == ignores case. 'Main' == 'main' is true, which can surprise you when you compare branch names.
Quick checklist
- Start the value with
${{if the expression begins with!. - Quote the whole value if it contains
:or starts with a special YAML character. - Use single quotes for strings inside the expression.
- Keep the full expression inside one
${{ }}. - Compare outputs and inputs to
'true'or'false'.
Exercise
This step is meant to run only on the main branch, but it runs on every branch. Why, and how do you fix it? if: ${{ github.ref }} == 'refs/heads/main'
Show answer
The ${{ }} wraps only part of the value, so the result is a string like "refs/heads/dev == 'refs/heads/main'". A non-empty string is truthy, so the step always runs. Put the whole comparison inside one wrapper: if: ${{ github.ref == 'refs/heads/main' }}, or drop the wrapper.