Sub topic 15
Debugging a Condition That Does Not Behave as Expected
This chapter assumes you can read a workflow file (on, jobs, steps) and know the difference between a job and a step. It also assumes you have seen if on both. Here you will learn a repeatable way to find out why a step ran when it should not have, or was skipped when it should have run.
Start with what the log says
Every skipped step or job appears in the run log with a skipped marker. Open the run and check two things first:
- Was the whole job skipped, or only one step? A job-level
ifis evaluated before any step starts, so step-level contexts such assteps.<id>do not exist there. - Did an earlier step fail? Without a status function, a step runs only while all earlier steps succeeded.
Many "broken" conditions are only the default success() check doing its job.
Print the values you compare
The most direct technique is to print the contexts your condition reads. Use a step with no if of its own, placed before the step you are debugging. Add always() so it still runs after a failure.
name: Debug
on: [push, pull_request]
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Show values used by conditions
if: ${{ always() }}
env:
EVENT: ${{ github.event_name }}
REF: ${{ github.ref }}
JOB_STATUS: ${{ job.status }}
run: |
echo "event_name=[$EVENT]"
echo "ref=[$REF]"
echo "job.status=[$JOB_STATUS]"
- name: Dump the github context
if: ${{ always() }}
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: echo "$GITHUB_CONTEXT"The square brackets make stray spaces and empty values visible. The values go through env so that untrusted text is never pasted straight into the shell script.
Match the symptom to the likely cause
| Symptom | Likely cause | Fix |
|---|---|---|
| Step runs even though the output is `'false'` | A bare output is a non-empty string, so it is truthy | Compare with `== 'true'` |
| Workflow file is rejected with a YAML error | The expression starts with `!` and is not wrapped | Use `${{ !startsWith(...) }}` |
| Step skipped after an earlier failure | Implicit `success()` | Add `failure()` or `always()` |
| Dependent job skipped | A job it needs was skipped or failed | Use `always()` with `needs.<id>.result` |
| Branch check never matches | `github.ref` holds `refs/heads/main`, not `main` | Compare the full ref, or use `github.ref_name` |
| Secret check never matches | Secrets are not allowed in a step `if` | Copy the secret into `env` and test that |
Check the order of evaluation
Do not use the diagram block above. Follow this order instead when you debug:
- Read the skipped marker to see which level was skipped.
- Print the contexts the condition reads.
- Compare the printed text with your literal, character by character.
- Simplify the condition to one clause, then add clauses back one at a time.
Case and whitespace
String comparison with == ignores case, but contains on strings also ignores case, while the text itself must still match in spelling. Leading or trailing spaces in an output are a common hidden difference. The bracketed echo from the earlier example reveals them.
Exercise
A step has if: steps.check.outputs.extra and runs even though the earlier step wrote extra=false to $GITHUB_OUTPUT. Why, and how do you fix it?
Show answer
Step outputs are strings, and the non-empty string false is truthy. Compare it as text: if: steps.check.outputs.extra == 'true'.