Conditions
Run steps conditionally
Conditions
Conditions let you control when steps run. You can skip steps based on the branch, the result of previous steps, or custom expressions.
Basic Conditions
if — Run Only When True
steps:
- name: Deploy
if: github.ref == 'refs/heads/main'
run: ./deploy.shThis step only runs on the main branch.
when — Run Based on Previous Steps
steps:
- name: Build
run: make build
- name: Notify on failure
when: on_failure
run: echo "Build failed!"| Value | Description |
|---|---|
always | Run regardless of previous steps |
on_success | Run only if all previous steps succeeded (default) |
on_failure | Run only if a previous step failed |
Examples
Deploy Only on Main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: make build
- name: Deploy
if: github.ref == 'refs/heads/main'
run: ./deploy.shRun Cleanup Always
steps:
- name: Run tests
run: npm test
- name: Cleanup
when: always
run: rm -rf /tmp/test-dataSkip on Pull Requests
steps:
- name: Publish
if: github.event_name == 'push'
run: npm publishMultiple Conditions
steps:
- name: Deploy to production
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: ./deploy-production.shEnvironment Variables in Conditions
You can use environment variables in conditions:
steps:
- name: Set environment
run: echo "DEPLOY=true" >> $GITHUB_ENV
- name: Deploy
if: env.DEPLOY == 'true'
run: ./deploy.sh