Expressions and Conditional Execution
What you'll learn: How to use expressions to make your GitHub Actions workflows smart enough to skip, run, or change behavior based on runtime conditions.
What Are Expressions?
Expressions in GitHub Actions are dynamic snippets that evaluate to true/false or return values at runtime. Think of them like if-statements in programming—they let your workflow make decisions. You write expressions using the ${{ }} syntax and can access contexts (like github, env, secrets) to read runtime information.
Controlling Execution with if Conditions
You can add an if statement to jobs or steps to control whether they run. The workflow evaluates the expression, and only proceeds if it's truthy.
Common use cases:
- Run steps only on the main branch
- Skip jobs for pull requests from forks
- Execute cleanup only on failure
- Run deployment steps only when tests pass
Basic Expression Examples
jobs:
deploy:
runs-on: ubuntu-latest
# Only run on pushes to main branch
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to production
run: echo "Deploying..."
- name: Notify on failure
# Only run if previous steps failed
if: failure()
run: echo "Deployment failed!"
test:
runs-on: ubuntu-latest
steps:
- name: Run tests
run: npm test
- name: Upload coverage
# Only if tests succeeded AND on main branch
if: success() && github.ref == 'refs/heads/main'
run: echo "Uploading coverage..."
Expressions support logical operators (&&, ||, !), comparisons, and status check functions like success(), failure(), cancelled(), and always().
Key Takeaway: Expressions with if conditions let you build intelligent workflows that adapt to branch names, event types, previous step outcomes, and other runtime conditions—making your pipelines flexible and efficient.