githubcontinuous-integrationgithub-actions

How to run GitHub Actions workflow only if the pushed files are in a specific folder


I have a folder structure that looks something like this.

- folder1
  - file1
  - *other files*
- folder2
  - file1
  - *other files*
- .gitignore
- package.json
- *other files*

I want to run my GitHub Actions workflow on push, only if any of the changed/pushed files are located in the folder1 directory/folder.


Solution

  • The normal syntax involves a path filter:

    on:
      push:
        paths:
          - folder1/**
    

    If that is not enough, you also have the GitHub Action Path Filter.


    Can a path be ignored also? How do I do that? Does the below example work?

    paths:
         - !folder1/**
    

    The official documentation is "on.<push|pull_request|pull_request_target>.<paths|paths-ignore>":

    You can indeed use negative patterns (prefixed with !) in the paths filter to exclude specific files or directories, provided that you also have at least one positive pattern. The order of patterns matters: patterns are evaluated in the order they are listed.

    So - '!folder1/**' alone would not work because there is no positive pattern included.
    If you prefer, you can achieve the same effect using paths-ignore, but you cannot use both paths and paths-ignore for the same event:

    on:
      push:
        paths-ignore:
          - 'folder1/**'
    

    An example from the documentation would run on changes to sub-project/** but excludes sub-project/docs/**:

    on:
      push:
        paths:
          - 'sub-project/**'
          - '!sub-project/docs/**'