Sub topic 8
Running jobs conditionally with if
This chapter assumes you can read a workflow file (on, jobs, steps) and know that ${{ }} marks an expression. Contexts such as github and needs are explained as they appear.
Why job-level conditions
A workflow-level paths filter decides whether the whole workflow starts. It cannot say "run the backend tests but skip the frontend tests." A job-level if can. Each job evaluates its own condition, and if the result is false, the job is skipped.
Basic syntax
Put if directly under the job. Inside if, the ${{ }} wrapper is optional.
name: conditional-demo
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Running tests"
deploy:
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying"On a pull request, test runs and deploy shows as skipped. On a push to main, both run. The condition uses the github context, which holds event data such as the event name and the ref.
Conditions on outputs
For monorepos, the useful condition reads an output from an earlier job through the needs context. The next chapters build a real change-detection job. This stub sets the output by hand so you can see the pattern.
name: output-demo
on: [pull_request]
jobs:
changes:
runs-on: ubuntu-latest
outputs:
backend: ${{ steps.detect.outputs.backend }}
steps:
- id: detect
run: echo "backend=true" >> "$GITHUB_OUTPUT"
backend-test:
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
steps:
- run: echo "Backend changed"Job outputs are always strings. Compare against 'true', not the boolean true. A bare if: needs.changes.outputs.backend is also risky, because the string 'false' is truthy in some comparisons and the intent is unclear.
Common errors
- Leading
!in YAML.if: !cancelled()fails to parse because YAML reads!as a tag. Writeif: ${{ !cancelled() }}. - Mixing text and expression.
if: ${{ needs.changes.outputs.backend }} == 'true'becomes a non-empty string, which is always true. Put the whole comparison inside the braces or leave the braces off. - Unavailable contexts. A job-level
ifis evaluated before a runner is assigned. It can usegithub,needs,vars, andinputs. It cannot useenvorsteps. - Missing
needs.needs.changes.outputs...is empty unless the job listschangesinneeds.
The implicit success check
GitHub adds success() to any if that does not use a status function such as always(), failure(), or cancelled(). A job is therefore skipped when a needed job failed or was skipped, even if your own condition is true. Writing if: always() && ... removes this behavior. The gate job chapter uses that fact.
Exercise
A job has needs: changes and the condition if: needs.changes.outputs.frontend == true, with no quotes around true. The output is the string "true". Will the job run? What would you change?
Show answer
Do not rely on it. Outputs are strings, so the comparison should be written as needs.changes.outputs.frontend == 'true'. Quoting the value makes it a string comparison and removes any dependence on type coercion rules.