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

Viewed 23179

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.

4 Answers

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

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

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

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

on:
  push:
    branches: ['main']
    paths: ['folder/**']
Related