Advanced Workflow PatternsLesson 5.3
How to use conditional logic with if expressions in GitHub Actions
if expression, github context, job status functions, success(), failure(), always(), cancelled(), branch conditions, event conditions, expression syntax
if Expressions
if expressions control whether a step or job runs. They evaluate a GitHub expression that must return true or false. Steps with a false condition are skipped, not failed.
Common Conditions
steps:
- name: Notify on success
if: success()
run: echo "Build passed"
- name: Notify on failure
if: failure()
run: echo "Build failed — check logs"
- name: Always clean up
if: always()
run: rm -rf /tmp/build-cache
- name: Deploy only on main
if: github.ref == 'refs/heads/main' && success()
run: ./deploy.sh
- name: Skip on fork PRs
if: github.event.pull_request.head.repo.full_name == github.repository
run: ./integration-test.shsuccess() is the default — steps only run when all prior steps passed. failure() runs only after a failure. always() runs regardless of outcome, useful for cleanup. Combine with context comparisons using &&. Note: expressions use == not ===, and strings must be quoted.
