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

如何让GitHub Action检测是否仅特定目录发生变更?

检测GitHub PR中是否仅修改指定目录并跳过构建

要实现仅当my-directory目录下文件被修改,且无其他目录文件变更时跳过构建,可以通过扩展dorny/paths-filter@v2的配置,添加两个过滤器分别检测目标目录变更和其他文件变更,再通过逻辑判断实现需求:

配置步骤

  1. 调整paths-filter的过滤器规则,同时检测两类文件变更:

    - uses: dorny/paths-filter@v2
      id: my-filter
      with:
        filters: |
          my-dir-changed:
            - 'my-directory/**'
          other-files-changed:
            - '**'
            - '!my-directory/**'
    
    • my-dir-changed:当my-directory下至少有一个文件变更时返回true
    • other-files-changed:当除my-directory外的任意文件变更时返回true
  2. 在构建步骤中添加条件判断,仅当存在非my-directory的变更时才执行构建:

    - name: 执行构建
      if: ${{ !(steps.my-filter.outputs.my-dir-changed == 'true' && steps.my-filter.outputs.other-files-changed == 'false') }}
      run: |
        # 这里写你的构建命令
        echo "开始构建..."
    

逻辑说明

  • 当my-dir-changed为true且other-files-changed为false时,说明PR仅修改了my-directory下的文件,此时构建步骤会被跳过
  • 只要存在其他目录的文件变更,或者my-directory没有变更,构建步骤都会正常执行

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:33:17