Sub topic 5
Operators, String Comparison and Truthiness
This chapter assumes you can read a workflow file (on, jobs, steps) and that you have already added a basic if to a step. Here you learn how GitHub evaluates the expression inside if, and why a condition can pass when you expect it to fail.
Operators you can use
An if expression supports a small set of operators.
| Operator | Meaning | Column | Column |
|---|---|---|---|
| `!` | Not | ||
| `==`, `!=` | Equal, not equal | ||
| `<`, `<=`, `>`, `>=` | Less than, greater than (and the "or equal" forms) | ||
| `&&` | And | ||
| `\ | \ | ` | Or |
| `( )` | Grouping |
&& and || short-circuit. In a && b, b is not evaluated if a is false. && binds tighter than ||, so use parentheses when you mix them.
steps:
- name: Publish
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/release')
run: echo "Publishing"String comparison ignores case
Comparison with == and != on strings is case-insensitive. The condition below is true when the branch name is Main, MAIN or main.
steps:
- name: Main branch only
if: github.ref_name == 'MAIN'
run: echo "Case does not matter here"String literals use single quotes. Double quotes are a syntax error inside an expression.
Truthiness and type coercion
When a value is not a boolean, GitHub converts it before using it as a condition. These values are falsy:
false0and-0''(the empty string)null
Every other value is truthy. This includes the non-empty string 'false' and any object or array.
Step outputs and environment variables are always strings, so this is where the rule hurts. If a step writes extra=false, the output is the text false. A bare reference to it is truthy.
steps:
- name: Decide
id: check
run: echo "extra=false" >> "$GITHUB_OUTPUT"
- name: Runs anyway
if: steps.check.outputs.extra
run: echo "The string 'false' is truthy, so this step runs"
- name: Runs only when expected
if: steps.check.outputs.extra == 'true'
run: echo "This step is skipped"When you compare values of different types, GitHub converts both to numbers. A string that is not a valid number becomes NaN, and any comparison with NaN is false. Compare strings with strings and numbers with numbers to avoid surprises.
steps:
- name: Many changed files
if: ${{ fromJSON(env.COUNT) > 10 }}
env:
COUNT: "25"
run: echo "More than 10 files"Here fromJSON turns the string "25" into a number, so the comparison is numeric.
How evaluation flows
Common errors
- Comparing an output to a boolean, as in
== true. The output is a string, so compare to'true'. - Mixing
&&and||without parentheses and getting a different grouping than you meant. - Using double quotes around a literal inside an expression.
Exercise
A step writes run_extra=false to $GITHUB_OUTPUT with id plan. A later step uses if: steps.plan.outputs.run_extra. Does the later step run, and how do you fix the condition?
Show answer
It runs, because the output is the non-empty string 'false', which is truthy. Change the condition to steps.plan.outputs.run_extra == 'true'.