112

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.

riQQ
  • 9,878
  • 7
  • 49
  • 66
TheComputerM
  • 1,281
  • 2
  • 5
  • 10
  • https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-including-paths -> you can reference this link for different variety of option for triggering a GitHub actions pipeline. And as VonC has mentioned the correct answer, i don't want to repeat the same. – shyam Sep 15 '20 at 04:10
  • 1
    It's crazy GitHub isn't better at explaining how to do such a simple thing. – basickarl Feb 01 '22 at 10:04
  • As an addition, see the [filter pattern cheat sheet](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#patterns-to-match-file-paths) for all possible filter patterns. – Robin Kaltenbach Feb 26 '21 at 22:05

4 Answers4

165

The normal syntax involves a path filter

on:
  push:
    paths:
      - folder1/**

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

Flimtix
  • 356
  • 1
  • 4
  • 17
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It is working for pushed files only but not working for pushed with previous commits in the same branch. Does anybody know how to run an action for all the files in the PR not only recently pushed? If I am running tests I want to run them for previous changes as well. – Dzmitry Vasilevsky Dec 11 '22 at 13:44
  • @DzmitryVasilevsky Can you illustrate your use case in a separate question? – VonC Dec 11 '22 at 14:52
  • Appreciate your help https://stackoverflow.com/questions/74772740/run-github-actions-if-branch-has-files-from-a-specific-directory – Dzmitry Vasilevsky Dec 12 '22 at 14:38
23

Path filters only work at workflow level.

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

If you want to apply this at job level, look for changed-files

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
rizways
  • 319
  • 2
  • 5
17

You will need this if also wanting to filter pull request runs

on:
  push:
    paths:
    - 'folder1/**'
  pull_request:
    paths: 
    - 'folder1/**'
Francisco Cardoso
  • 1,438
  • 15
  • 20
13

You can also add branches and remove some lines using square brackets.

on:
  push:
    branches: ['main']
    paths: ['folder/**']
J Garza
  • 131
  • 1
  • 2