What’s the best way to trigger a GitHub Actions workflow only when specific files or folders change? #164673
-
Why are you starting this discussion?Question What GitHub Actions topic or product is this about?Misc Discussion DetailsI have a monorepo setup, and I want to run certain workflows only when files in a specific folder (like /backend/ or /docs/) are modified. I’ve tried using paths in the on.push trigger, but I’m not sure if I’m doing it optimally or if it works with pull requests too. What’s the best practice for this use case? Here’s a snippet of what I’m currently using: on: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
GitHub Actions Path Filtering for MonoreposThis is about GitHub Actions' path filtering capabilities, specifically for monorepo workflows. Let me break down the best practices and considerations: Optimal Path Filtering SetupFor your monorepo, you can optimize path filtering like this: on:
push:
paths:
- 'backend/**'
- 'backend/*'
pull_request:
paths:
- 'backend/**'
- 'backend/*' Key Considerations
Improved Approach for MonoreposConsider this more robust pattern: on:
push:
paths:
- 'backend/**'
- 'backend/*'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'lerna.json'
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md' Gotchas to Watch For
Advanced ExampleFor complex monorepos, consider using a matrix approach: jobs:
|
Beta Was this translation helpful? Give feedback.
-
The best way to trigger a GitHub Actions workflow only when specific files or folders change is by using the Here’s an example: name: Run on Specific File Changes
on:
push:
paths:
- 'src/**'
- 'config/*.yml'
pull_request:
paths:
- 'src/**'
- 'config/*.yml'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: echo "Changes detected in src or config folder." Explanation:
💡 Pro Tip: If you're combining multiple workflows, use |
Beta Was this translation helpful? Give feedback.
GitHub Actions Path Filtering for Monorepos
This is about GitHub Actions' path filtering capabilities, specifically for monorepo workflows. Let me break down the best practices and considerations:
Optimal Path Filtering Setup
For your monorepo, you can optimize path filtering like this:
Key Considerations
Double Asterisk (
**
) vs Single Asterisk (*
):backend/**
matches all files in backend and its subdirectoriesbackend/*
matches only files directly in the backend directoryPull Request Behavior: