Search code examples
githubgithub-actions

Triggering based in modified file pattern with per-commit evaluation (instead of per-PR evaluation)


Let's consider a GitHub Actions workflow like this one:

name: Check in JSON files
on:
  push:
    paths:
      - '**/*.json'
  pull_request:
    branches:
      - master
    paths:
      - '**/*.json'      
jobs:
  model_validation:
    ...

The idea is to check only in the case JSON files are modified. So, I can have a sequence of commits in my PR like this:

commit modification workflow triggered?
1 .md file modified no
2 .py file modified no
3 .json file modified yes

So far so good.

The problem is that if new commits are added, the workflow is triggered, no matter the kind of file the commit modifies. Let's consider in the above example PR that I add two new commits modifying not .json files. What I get is:

commit modification workflow triggered?
1 .md file modified no
2 .py file modified no
3 .json file modified yes
4 .py file modified yes
5 .md file modified yes

while I would like to get this:

commit modification workflow triggered?
1 .md file modified no
2 .py file modified no
3 .json file modified yes
4 .py file modified no
5 .md file modified no

My understanding is that the paths condition is evaluated at PR level (i.e. summing all the commits in the PR). So, given the sum from 1 to 6 includes a .json file, the workflow is triggered. What I'm looking for is for commit level evaluation.

Thus, is there any way of achieving what I want (i.e. per commit file path evaluation)? In that case, how would it possible?


Solution

  • I have found a solution (although I'm not sure is the optimal one) removing the pull_request section, this way:

    name: Check in JSON files
    on:
      push:
        paths:
          - '**/*.json'      
    jobs:
      model_validation:
        ...
    

    Doing so, I achieve the desired case:

    commit modification workflow triggered?
    1 .md file modified no
    2 .py file modified no
    3 .json file modified yes
    4 .py file modified no
    5 .md file modified no