如何基于推送变更的目录有条件触发GitHub Workflow:仅在变更不含指定排除路径时触发
How to prevent GitHub Workflow from triggering if changes include both allowed and excluded paths
Great question! The default behavior of GitHub Actions' paths filter is indeed that any positive match will trigger the workflow, even if there are also matches to negative (!) patterns—which isn't what you're looking for.
You want to run the workflow only when changes are in sub-project/** AND none of the changes are in sub-project/docs/**. Here's how to achieve that:
Solution: Combine paths filter with a workflow-level if condition
First, keep the paths filter to ensure we only consider pushes with changes in sub-project/**. Then, add an if condition on your job to verify that none of the modified files in the commit are in the excluded sub-project/docs/** path.
Here's the updated workflow:
on: push: paths: - 'sub-project/**' jobs: your-job-name: # This condition checks that every modified file does NOT start with the excluded path if: ${{ every(github.event.head_commit.modified, fn -> !startsWith(fn, 'sub-project/docs/')) }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 # Add your workflow steps here
How this works:
- The
pathsfilter first ensures the push has at least one change insub-project/**—so pushes only modifying files outside this directory are already ignored. - The
ifcondition uses GitHub's expression functions:every()checks that all items in themodifiedarray (files changed in the commit) meet the condition.!startsWith(fn, 'sub-project/docs/')ensures no modified file is in the excluded docs directory or its subdirectories.
Test cases:
- ✅ Push only modifying
sub-project/index.jsorsub-project/src/index.js: Triggers the workflow (matchespathsand passes theifcheck). - ❌ Push modifying
sub-project/index.jsANDsub-project/docs/readme.md: Does NOT trigger (fails theifcondition, since one file is in the excluded path). - ❌ Push only modifying
sub-project/docs/readme.md: Does NOT trigger (doesn't pass the initialpathsfilter).
内容的提问来源于stack exchange,提问作者Yash Shrivastava
相关产品推荐
相关产品推荐

