quickstart →

if conditions in GitHub Actions jobs and steps

How if works on jobs and steps: the implicit success(), always() vs !cancelled(), failure(), running after a skipped job, and an expression cookbook.

if: decides whether a job or a step runs. It takes an expression ↗, and the ${{ }} around it is optional. One rule explains most surprises: every job and step has an implicit success() condition, and it only goes away when your if: calls a status function such as always(), failure() or cancelled() itself.

Job-level conditions#

Only deploy from main:

.github/workflows/ci.yml
jobs:
deploy:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh

A job whose if: is false is skipped, not failed, and shows as skipped in the run. Jobs that need it are skipped too, unless their own condition says otherwise.

For branch protection, a skipped job counts as a success: it doesn’t block a pull request from merging, even when it’s a required check. That makes the backend-tests pattern below safe to require, but a wrong condition lets a pull request merge untested, and so does a failed changes job, unless changes is a required check too.

Job-level if: is evaluated before the job is sent to a runner, so it can use the github, needs, vars and inputs contexts, but not env, steps or runner, which only exist on the runner.

Step-level conditions#

.github/workflows/ci.yml
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 2 # HEAD~1 needs the parent commit; checkout fetches one commit by default
- id: changes
run: |
if git diff --quiet HEAD~1 -- docs/; then
echo "docs=false" >> "$GITHUB_OUTPUT"
else
echo "docs=true" >> "$GITHUB_OUTPUT"
fi
- name: Build the docs
if: steps.changes.outputs.docs == 'true'
run: make docs

Step conditions can read steps, env, runner, job, matrix and strategy, on top of the job-level contexts. Outputs are strings, so compare with 'true', not true.

Status check functions#

FunctionTrue when
success()No earlier step failed or was cancelled. For a job, every job above it in the needs chain succeeded: a skipped job doesn’t count as a success. The implicit default.
failure()An earlier step failed. For a job, any job above it in the needs chain failed.
cancelled()The run was cancelled.
always()Always, even when the run is cancelled.

The implicit success() is dropped only when your expression calls a status function itself. Otherwise GitHub keeps it in front of your condition:

  • if: github.event_name == 'push' behaves as success() && github.event_name == 'push': it still skips after a failure.
  • if: failure() runs only when something before it failed.
  • if: always() runs no matter what, even after the run is cancelled, which is rarely what you want for anything slow. Keep it for quick diagnostics and for a required gate job, which must fail, not be skipped, when the run is cancelled.
  • if: ${{ !cancelled() }} runs after a success or a failure, but not after a cancel. Prefer it to always() for reports and notifications. For cleanup that must also run after a cancel, such as tearing down a preview environment or releasing a lock, keep always() and give the step a timeout-minutes, since a cancel doesn’t stop it.
.github/workflows/ci.yml
steps:
- run: npm test
- name: Upload the test report even when tests fail
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v7
with:
name: junit
path: reports/
- name: Notify on failure
if: failure() && github.ref == 'refs/heads/main'
run: ./notify-slack.sh

Write ${{ !cancelled() }} with the braces: YAML reads a plain value that starts with ! as a tag and the workflow fails to parse.

Conditions on needs#

The needs context exposes each direct dependency’s result (success, failure, cancelled or skipped) and its outputs:

.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: npm test
report:
needs: test
if: ${{ !cancelled() && needs.test.result == 'failure' }}
runs-on: ubuntu-latest
steps:
- run: echo "Tests failed, opening an issue"

With !cancelled() rather than always(), report still runs after test fails, but cancelling the run stops it.

needs only contains the jobs listed in this job’s own needs, not their dependencies. To check a job two levels up, add it to needs as well.

Run a job when a dependency was skipped#

A job is skipped when any job it needs was skipped, even if nothing failed. This bites as soon as a job in the chain has its own if::

.github/workflows/ci.yml
jobs:
test:
if: github.event_name != 'workflow_dispatch' # skip tests on manual runs
runs-on: ubuntu-latest
steps:
- run: npm test
deploy:
needs: test
# Without this, deploy is skipped whenever test is skipped.
if: ${{ !failure() && !cancelled() }}
runs-on: ubuntu-latest
steps:
- run: ./deploy.sh

!failure() && !cancelled() reads as “nothing upstream failed and nobody cancelled”: it runs when test passed or was skipped. Use ${{ !cancelled() && needs.test.result != 'failure' }} when you need to spell out one dependency among several. Start from !cancelled(), not always(): a cancelled test reports cancelled, not failure, so with always() the deploy runs even after the run is cancelled. Also check every job whose failure must block the deploy: a job skipped because its own dependency failed reports skipped, not failure.

The skip carries down the chain: a job that needs deploy, such as smoke tests or a notification, is skipped too, even when deploy ran and succeeded, because its implicit success() looks at every job above it. Give that job a condition on its direct dependency, if: ${{ !cancelled() && needs.deploy.result == 'success' }}, rather than another !failure() && !cancelled(), which would also run it when deploy is skipped by a condition of its own, such as a main-only rule.

Skip a job based on another job’s output#

Publish the value as a job output and test it in the next job’s if::

.github/workflows/ci.yml
jobs:
changes:
runs-on: ubuntu-latest
outputs:
backend: ${{ steps.filter.outputs.backend }}
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 2 # HEAD~1 needs the parent commit; checkout fetches one commit by default
- id: filter
run: |
if git diff --quiet HEAD~1 -- api/; then
echo "backend=false" >> "$GITHUB_OUTPUT"
else
echo "backend=true" >> "$GITHUB_OUTPUT"
fi
backend-tests:
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
steps:
- run: make test-api

On pull_request, HEAD is the merge commit GitHub creates, so HEAD~1 is the tip of the base branch and the diff covers the whole pull request. On push, it only covers the last commit of the push. To cover every pushed commit, diff against ${{ github.event.before }} with fetch-depth: 0 (that SHA is all zeros when the push creates the branch), or use an action such as dorny/paths-filter ↗. The script writes backend=false only when git diff --quiet exits with 0, which means nothing under api/ changed. If git fails, for example on a commit that isn’t in the clone, the tests run rather than being skipped.

Expressions you’ll use#

# Branches and tags
if: github.ref == 'refs/heads/main'
if: startsWith(github.ref, 'refs/tags/v')
if: github.ref_name == 'main' # without the refs/heads/ prefix
# Events
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
if: contains(github.event.pull_request.labels.*.name, 'deploy-preview')
# Several values
if: contains(fromJSON('["main", "staging"]'), github.ref_name)
# Either of two refs (parentheses group the ||)
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/'))
# Repository and actor
if: github.repository == 'my-org/my-repo' # don't run in forks
if: github.actor != 'dependabot[bot]'
# Multi-line, with explicit status
if: |
success() &&
github.event_name == 'push' &&
github.ref == 'refs/heads/main'

Comparisons of strings are case-insensitive ('Main' == 'main' is true). A missing value evaluates to an empty string, which is falsy, so if: github.head_ref is true only on pull_request and pull_request_target runs.

If/else in GitHub Actions#

Expressions have no ternary operator. To pick one of two values, use case() or the older && / || pattern. To run one step or another, give two steps opposite if: conditions.

Pick a value with case()#

case(), added in January 2026, returns the value of the first condition that is true, or its last argument when none is:

.github/workflows/ci.yml
jobs:
deploy:
runs-on: ubuntu-latest
# production on main, staging everywhere else
environment: ${{ case(github.ref == 'refs/heads/main', 'production', 'staging') }}
steps:
- uses: actions/checkout@v7
- run: ./deploy.sh
env:
# Conditions are checked in order; the last value is the default
CHANNEL: ${{ case(startsWith(github.ref, 'refs/tags/v'), 'stable', github.ref == 'refs/heads/main', 'beta', 'nightly') }}

GitHub documents case() for GitHub.com and GitHub Enterprise Cloud, not GitHub Enterprise Server, and act rejects it as of November 2026. Use the && / || pattern there.

The && / || pattern#

Before case(), the usual if/else was ${{ condition && 'value-if-true' || 'value-if-false' }}. && returns its first falsy operand, or its last operand when none is falsy, and || returns its first truthy operand, or its last operand when none is truthy. So the expression gives the first value when the condition is true and the second one otherwise.

It breaks when the first value is itself falsy: false, 0, '' or null. ${{ github.event_name == 'push' && '' || '--dry-run' }} always gives --dry-run, because the '' makes the && falsy and || moves on to its fallback. Negate the condition so that the falsy value comes last, ${{ github.event_name != 'push' && '--dry-run' || '' }}, or use case(), which returns the value of the matching condition, whatever it is.

Run one step or the other#

.github/workflows/ci.yml
steps:
- name: Deploy to production
if: github.ref == 'refs/heads/main'
run: ./deploy.sh production
- name: Deploy a preview
if: github.ref != 'refs/heads/main'
run: ./deploy.sh preview

One of the two runs, as long as every step before them succeeded: each keeps its implicit success(), so after a failure both are skipped. The same works for two jobs, but a job that needs both is then always skipped, because one of them always is: give it the condition from Run a job when a dependency was skipped. When the branches differ only by a value, as here, one step with case() is simpler.

Things that trip people up#

  • if: false as a string. if: ${{ 'false' }} is a non-empty string, which is truthy. Compare outputs explicitly: == 'true'. github.event.inputs has the same trap: it holds every input as a string, so if: github.event.inputs.dry_run runs even when dry_run is false. Read boolean inputs from the inputs context, which keeps them as booleans: if: inputs.dry_run, if: ${{ !inputs.dry_run }}.
  • Text outside ${{ }}. if: ${{ steps.changes.outputs.docs }} == 'true' becomes a string such as false == 'true', and a non-empty string is truthy, so the condition is always true. Put the whole condition inside one ${{ }}, or drop the braces. actionlint flags it, and since January 2026 so do GitHub’s workflow editor and the GitHub Actions extension for VS Code.
  • Secrets in if:. The secrets context isn’t available in if:. Map the secret to an env value at the job level and test env.MY_SECRET != '' in a step condition.
  • job.status vs success(). Inside a step, job.status is the job’s current status (success, failure or cancelled), handy in a notification message. In if:, use the status functions.
  • continue-on-error hides the failure. When a step with continue-on-error: true fails, its outcome is failure but its conclusion is success, so failure() stays false and later if: failure() steps don’t run. Give the step an id and test its outcome: if: steps.lint.outcome == 'failure'.
  • No matrix in a job-level if:. The job’s if: is evaluated before the matrix expands, so it can’t read matrix. To skip one combination, use exclude in the matrix, or put the condition on the steps.
  • A condition on the runner. if: decides whether a job runs, not where. To pick the runner from a condition, use an expression in runs-on instead of two copies of the job with opposite if: conditions.

When a condition still surprises you, print the contexts to see the values it compares.

Why was my job skipped?#

Since January 2026, GitHub logs how it evaluated each job-level if:, without debug logging. Open the job, choose Download log archive from the gear menu, and read JOB-NAME/system.txt. The Evaluating: line shows the condition as GitHub evaluated it, implicit success() included, for example (success() && (...)). The Expanded: line shows the same expression with the context values filled in, and Result: shows the outcome.

These logs only cover job-level conditions. For a step’s if:, turn on step debug logging.

Resources#