Tad
Tad

Reputation: 913

github action - trigger the workflow only when there is a change in files in .github/workflows/

I need github workflow to be triggered only when there is a change in files in .github/workflows/*. It shouldn't trigger the workflow if the there is a change in any other files.

For e.g., if I make change in the Docker file, the workflow shouldn't trigger.

My branch looks like this: enter image description here

My workflow yaml look like this

on: 
  #[push]
  pull_request:
    branches:    
      - test  
    types:
      - closed
    #paths-ignore:
    #  - '.github/*'
    paths:
      - '.github/*'     

The above yaml is not working for what I desire.

Upvotes: 2

Views: 1393

Answers (2)

cerossi
cerossi

Reputation: 11

This answer isn't quite correct. I mean, you're right about events, but there's only one event in the workflow above which is pull_request. branches, type and path are filters and all of them should be satisfied in order to trigger the workflow. In fact, the example above is pretty similar to one present in the official filter doc

The most likely issue here is the path expression .github/*, which I believe it won't consider the .github/workflows folder (it'll only look for changes in the .github instead). In this case, the correct path expression should be .github/workflows/*

Upvotes: 0

GuiFalourd
GuiFalourd

Reputation: 23270

The issue here is that the workflow trigger configurations (the on field) doesn't exactly work as you expect.

According to the official documentation:

If you specify multiple events, only one of those events needs to occur to trigger your workflow. If multiple triggering events for your workflow occur at the same time, multiple workflow runs will be triggered.

Therefore, in your case, each subtype in the pull_request event is considered as a separate event, and would trigger the workflow even if the conditions of the other subtypes aren't met.

What is happening here, is that any PR to the test branch triggers the pull_request: branches: - test subtype, independently of the paths: - '.github/*' configuration (it won't matter which file you update if the branch is test).

Moreover, with those configurations, the workflow would also trigger if you updated the '.github/*' path when opening a PR from another branch.


Summarizing, with your current configurations:

  • open PR from branch test: the workflow will trigger.
  • open PR from a branch which is not test: the workflow will trigger only if there are changes in the path is '.github/*'
  • close any PR: the workflow will trigger.

If you want the workflow to trigger only when there are changes to the specific path, when opening a PR, you should use:

on: 
  pull_request:
    paths:
      - '.github/workflows/*'  

(Without configuring any other event.)

Now, if you want the workflow to trigger each time there is a push to a branch (when you open OR update a PR for example), you should use:

on: 
  push:
    paths:
      - '.github/workflows/*'  

Upvotes: 3

Related Questions