Sub topic 4
Expressions and the ${{ }} syntax
This chapter assumes you can read a workflow file (on, jobs, runs-on, steps), know basic YAML, and can run simple shell commands. It covers what an expression is, where GitHub evaluates it, and the errors people hit most often.
What an expression is
An expression is a small piece of code that GitHub evaluates before it runs your step. You write it between ${{ and }}. It can read a context value, compare values, or call a function.
The key point is timing. GitHub replaces the expression with its result as plain text. This happens on GitHub's side, before the shell starts. The shell never sees ${{ ... }}. It sees only the final text.
Where you write expressions
You can use ${{ }} in values such as env, with, name, run, and if. Here is a workflow that copies context values into environment variables.
name: expression-basics
on: push
jobs:
info:
runs-on: ubuntu-latest
steps:
- name: Print context values
env:
REF: ${{ github.ref }}
OS_NAME: ${{ runner.os }}
run: |
echo "Ref: $REF"
echo "Runner OS: $OS_NAME"GitHub fills in REF and OS_NAME first. The shell then reads them as ordinary environment variables.
The `if` exception
In an if key, the ${{ }} wrapper is optional. These two lines mean the same thing:
if: github.ref == 'refs/heads/main'
if: ${{ github.ref == 'refs/heads/main' }}What goes inside
An expression can combine these parts:
- Contexts, such as
github.reforrunner.os. - Literals, such as
'main',42,true, andnull. - Operators, such as
==,&&,||, and!. - Function calls, such as
startsWith(github.ref, 'refs/tags/').
Strings use single quotes. Double quotes are a syntax error inside an expression. To write a literal single quote, double it: 'It''s fine'.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Using double quotes in an expression | The workflow fails to parse | Use single quotes |
| Expecting `${{ }}` to run in the shell | The expression is already replaced when the shell starts | Remember it is text substitution, not a shell feature |
| Using `$VAR` inside `${{ }}` | `$VAR` is not valid expression syntax | Use `env.VAR` |
| Using `${{ env.VAR }}` where `env` is not available | The workflow is rejected or the value is empty | Check context availability for that key |
| Putting untrusted text in `run` | Attackers can inject shell commands | Pass it through `env` |
Quick check
Exercise
A step has run: echo "Branch is ${{ github.ref_name }}". At what point does ${{ github.ref_name }} become the branch name, and why does that matter for safety?
Show answer
GitHub replaces it with the branch name before the shell starts, as plain text inside the script. This matters because any special characters in the value become part of the script. For values you don't control, put the expression in env and use the shell variable instead.
Exercise
Why does if: !cancelled() fail, and how do you fix it?
Show answer
YAML reads a leading ! as a tag, so the line is invalid. Wrap it in the expression syntax: if: ${{ !cancelled() }}.