Sub topic 3
Writing expressions, with or without ${{ }}
This chapter assumes you can read a workflow file (on, jobs, steps), know basic YAML, and have already added a simple if to a step. Here you learn how GitHub Actions reads the text after if:, when the ${{ }} wrapper is optional, and when it is required.
The if value is an expression
The value of if is an expression. GitHub evaluates it before the step or job starts. If the result is truthy, the step or job runs. Otherwise it is skipped.
An expression can contain:
- Contexts such as
github,env,stepsandneeds. - Literals such as
'main',true,42andnull. - Operators such as
==,!=,&&,||and!. - Function calls such as
startsWith()andfailure().
The wrapper is optional in if
In most workflow keys, an expression must be wrapped in ${{ }}. The if key is an exception. GitHub treats its value as an expression automatically, so both forms below mean the same thing.
steps:
- name: Without the wrapper
if: github.event_name == 'push'
run: echo "push event"
- name: With the wrapper
if: ${{ github.event_name == 'push' }}
run: echo "push event"The unwrapped form is the usual style. It is shorter, and it avoids a trap with mixed text that is described below.
When the wrapper is required
An expression that starts with ! needs the wrapper, or quotes around it. YAML reads a leading ! as a tag indicator, so the workflow file fails to parse.
steps:
- name: Skip release tags
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
run: echo "Not a tag build"Quoting the whole value also works: if: "!startsWith(github.ref, 'refs/tags/')". Choose one style and use it consistently in a repository.
String literals use single quotes
Expressions have their own quoting rules, separate from YAML. A string literal inside an expression must use single quotes. Double quotes cause a syntax error.
steps:
- name: Main branch only
if: github.ref == 'refs/heads/main'
run: echo "On main"To put a single quote inside a string literal, double it: 'it''s'.
Expression syntax at a glance
| Part | Example | Note | Column | Column |
|---|---|---|---|---|
| Context property | `github.ref` | Dot notation. | ||
| Index access | `github.event['action']` | Useful for unusual property names. | ||
| String | `'main'` | Single quotes only. | ||
| Function | `contains(github.ref, 'release')` | Function names are case-insensitive. | ||
| Combination | `a && b`, `a \ | \ | b` | Short-circuit left to right. |
A common mistake: text around the expression
If you write ${{ }} and add other text in the same value, GitHub treats the whole value as a string, not as an expression. A non-empty string is always truthy, so the condition never skips anything.
steps:
- name: This always runs
if: ${{ github.ref }} == 'refs/heads/main'
run: echo "Runs on every branch"Here the value is a string that mixes an expression with text. Fix it by putting the whole comparison inside one wrapper, or by removing the wrapper entirely.
steps:
- name: Runs only on main
if: ${{ github.ref == 'refs/heads/main' }}
run: echo "Runs only on main"Diagram: how GitHub reads an if value
Exercise
A step has if: ${{ github.event_name }} == 'pull_request' and it runs on pushes too. Why, and how do you fix it?
Show answer
The wrapper covers only github.event_name, so the full value is a string mixing an expression with text. A non-empty string is truthy, so the step always runs. Move the whole comparison inside the wrapper (if: ${{ github.event_name == 'pull_request' }}) or drop the wrapper (if: github.event_name == 'pull_request').
Exercise
Why does if: !startsWith(github.ref, 'refs/tags/') fail, and what are two ways to fix it?
Show answer
YAML reads a leading ! as a tag indicator, so the file does not parse. Wrap the expression in ${{ }}, or quote the entire value in double quotes.