如何让GitHub Action检测是否仅特定目录发生变更?
检测GitHub PR中是否仅修改指定目录并跳过构建
要实现仅当my-directory目录下文件被修改,且无其他目录文件变更时跳过构建,可以通过扩展dorny/paths-filter@v2的配置,添加两个过滤器分别检测目标目录变更和其他文件变更,再通过逻辑判断实现需求:
配置步骤
调整
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下至少有一个文件变更时返回trueother-files-changed:当除my-directory外的任意文件变更时返回true
在构建步骤中添加条件判断,仅当存在非
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
相关产品推荐
相关产品推荐

