How to efficiently skip unnecessary jobs in a monorepo GitHub Actions workflow? #177835
-
| Why are you starting this discussion?Question What GitHub Actions topic or product is this about?Workflow Deployment Discussion DetailsWe have a large monorepo that uses GitHub Actions for CI/CD. The workflow currently runs all jobs (e.g., build-frontend, test-backend, deploy-docs) on every push to main, even if only a single, unrelated project directory has changed.This is causing our CI/CD pipeline to take an excessive amount of time and consume too many minutes.Goal: I need a robust and efficient way to conditionally skip specific jobs in the workflow based only on which directories have changed in the current commit or pull request.For example, if only files in the docs/ directory have changed, only the deploy-docs job should run, and all other jobs like build-frontend and test-backend should be skipped.What is the recommended, best-practice approach to implement this file-path-based conditional job execution in a GitHub Actions workflow for a monorepo? | 
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
| This is a very common and critical challenge in monorepos. The best and most efficient solution is to use the paths-filter action, which leverages the git diff command under the hood to determine the changed files and set output variables that can be used in the if condition of your jobs.Here is a step-by-step guide and the YAML implementation:1. Use the paths-filter Action The dorny/paths-filter action is the industry standard for this use case. It allows you to define a map of paths and will output a boolean (true or false) for each map key, indicating if any files in that path have changed.2. Implementation in Workflow YAML You will add a new job (or step in an existing job) to calculate the changes, and then use its output in the if condition of your other jobs. Key Takeaway The critical part is the needs: filter-paths and the if: needs.filter-paths.outputs.YOUR_KEY == 'true' condition on the subsequent jobs. This ensures the job is only queued and executed if a change was detected in the specified path, drastically reducing your CI/CD time and cost. | 
Beta Was this translation helpful? Give feedback.
-
| If each workflow YAML corresponds to a project (frontend, backend, docs, etc.), the simplest method is to split workflows and scope their triggers. Example: jobs: Then another workflow for backend: .github/workflows/backend.ymlon: | 
Beta Was this translation helpful? Give feedback.
This is a very common and critical challenge in monorepos. The best and most efficient solution is to use the paths-filter action, which leverages the git diff command under the hood to determine the changed files and set output variables that can be used in the if condition of your jobs.Here is a step-by-step guide and the YAML implementation:1. Use the paths-filter Action
The dorny/paths-filter action is the industry standard for this use case. It allows you to define a map of paths and will output a boolean (true or false) for each map key, indicating if any files in that path have changed.2. Implementation in Workflow YAML
You will add a new job (or step in an existing job) to calcula…