GitHub Actions workflow YAML traps
A workflow is YAML with an expression language layered on top. Where ${{ }} meets colons and multi-line strings, error messages point somewhere unexpected.
The on key and booleans
A YAML 1.1 parser reads on as the boolean true. GitHub uses a parser closer to YAML 1.2, but converting with other tools can turn the key into true.
This tool keeps on as a string key. When you move the file to another tool, quoting it as "on" is the safe move.
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
Multi-line run needs a block scalar
To write several commands after run:, use |. The > form folds lines into spaces, which is wrong for shell commands.
Each command line must be indented deeper than the block scalar, and the first line sets the baseline.
- name: Test
run: |
npm ci
npm test -- --reporter=dot
Wrap expressions in quotes
When ${{ }} fills the entire value it can be read as a number or boolean. Quote it to keep it a string.
Version values especially should be "${{ matrix.node }}" so 20 does not become the number 20.
- Whole value: node-version: "${{ matrix.node }}"
- Part of a value: name: test-${{ matrix.target }}
- Condition: if: ${{ always() }}
Questions
How is a build matrix written?
Put axes under strategy.matrix and list values. A short flow sequence such as [unit, integration] works.
Can I convert a workflow file to JSON?
Syntactically yes, but GitHub only reads .github/workflows/*.yml. Use the conversion for checking and comparing, not for running.
Sources
Every example here can be pasted into the converter as it is.
Open the converter