patternbashTip
Conditional steps with if expressions and job outputs
Viewed 0 times
dorny/paths-filter@v3
if conditionconditional steppaths-filteroutputssuccess()skip
Problem
CI pipelines run expensive steps (e2e tests, deployments) even when they are not needed, for example when only documentation files change, or when the build step failed.
Solution
Use if conditions on steps and jobs. Read changed files or previous step results:
jobs:
changes:
runs-on: ubuntu-latest
outputs:
src: ${{ steps.filter.outputs.src }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
src:
- 'src/**'
- 'package*.json'
test:
needs: changes
if: needs.changes.outputs.src == 'true'
runs-on: ubuntu-latest
steps:
- run: npm test
deploy:
needs: [test]
if: success() && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: ./deploy.shWhy
Skipping jobs based on file changes saves runner minutes. Using success() and explicit branch checks prevents accidental deploys from PR branches or after test failures.
Gotchas
- if: always() bypasses both success and failure checks—use only when you genuinely want a step to run regardless
- Skipped jobs are still shown in the UI as skipped, not hidden
- Job outputs from skipped jobs are empty strings, not null—guard comparisons accordingly
Revisions (0)
No revisions yet.