TheComputerM
TheComputerM

Reputation: 2021

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.

Upvotes: 184

Views: 159245

Answers (5)

VonC
VonC

Reputation: 1328282

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/**'

Upvotes: 258

Rodrigo L&#243;pez Dato
Rodrigo L&#243;pez Dato

Reputation: 1454

The above answers are correct, but they will not execute workflows again if the workflows themselves has changed. If this is something you want, make sure to include your workflows directory in the paths filters:

on:
  push:
    paths:
      - 'my-nodejs-application-directory/**'
      - '.github/workflows/**'

Upvotes: 29

J Garza
J Garza

Reputation: 541

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

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

Upvotes: 44

navedrizv
navedrizv

Reputation: 501

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

Upvotes: 39

Francisco Cardoso
Francisco Cardoso

Reputation: 1978

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

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

Upvotes: 25

Related Questions