You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于推送变更的目录有条件触发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:

  1. The paths filter first ensures the push has at least one change in sub-project/**—so pushes only modifying files outside this directory are already ignored.
  2. The if condition uses GitHub's expression functions:
    • every() checks that all items in the modified array (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.js or sub-project/src/index.js: Triggers the workflow (matches paths and passes the if check).
  • ❌ Push modifying sub-project/index.js AND sub-project/docs/readme.md: Does NOT trigger (fails the if condition, since one file is in the excluded path).
  • ❌ Push only modifying sub-project/docs/readme.md: Does NOT trigger (doesn't pass the initial paths filter).

内容的提问来源于stack exchange,提问作者Yash Shrivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:37:41