Expressions
GitHub Actions · Reference cheat sheet
Expressions
GitHub Actions · Reference cheat sheet
📋 Overview
Expressions ($\{\{ \}\}) evaluate values for if, env, with, and names. Contexts expose github metadata, secrets, vars, matrix, needs, and steps outputs. Functions like contains, hashFiles, and success() power conditionals.
🔧 Core concepts
| Context | Contents |
|---|---|
github | Event, ref, sha, actor, repository |
env | Environment variables |
secrets / vars | Config |
matrix | Strategy values |
needs | Upstream job results/outputs |
steps | Step outputs |
runner | OS, arch, temp |
inputs | workflow_dispatch / workflow_call |
Common functions: success(), failure(), cancelled(), always(), contains(), startsWith(), format(), toJSON(), hashFiles(), fromJSON().
💡 Examples
Conditionals:
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
if: failure()
if: always() && needs.build.result == 'success'hashFiles + format:
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
name: Build ${{ format('{0} ({1})', matrix.os, matrix.node) }}FromJSON for dynamic matrices:
strategy:
matrix: ${{ fromJSON(needs.setup.outputs.matrix) }}Step output consumption:
if: steps.changes.outputs.app == 'true'⚠️ Pitfalls
- Inside
if:, the$\{\{ \}\}wrapper is optional but mixing quotes is error-prone. - Secrets are not available in some contexts (e.g.
hashFilesinputs)—don’t try to hash secrets. github.head_refis empty outside PRs—guard with event checks.- Boolean strings:
'false'is truthy as a non-empty string—compare carefully. hashFilesreturns empty if no match—keys may collide unexpectedly.- Multi-line expressions and YAML parsing—prefer single-line
ifwhen possible.