Vistari
Vistari

Reputation: 707

Running a Github Actions workflow only on events in a pull request events that change files matching a path

I currently have a Github Actions workflow setup with the following trigger:

on:
  pull_request:
     paths: 
       - 'myFolder/*.yml'

I want this workflow to run on pull request events where a file matching with myfolder/*.yml has been changed. While this workflow does run on pull request events where this file has changed, it also runs on subsequent events even if they do not make any further changes.

The workflow this trigger is for runs a process using configuration from within the yml files and so if no changes happen to any of these files between commits (even if other files that do not match the filter are changed), the result will always be the same and does not need to be run.

I looked through the documentation for Github Actions and could not find anything that exactly matches my situation so would appreciate some help and pointers.

A simplified version with some name changes of the full workflow yml is:

name: Read yml files
on:
  pull_request:
    paths:
      - 'myFolder/*.yml'
jobs:
  promote:
    runs-on: ubuntu-latest

    name: Read files

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Read
        run: npm run read-yml

Upvotes: 2

Views: 2175

Answers (1)

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19792

It's not possible to do on YML workflow level.

You can however detect your case and leave early from a workflow.

I can suggest using changed-files action:

- name: Get changed files
  id: changed-files
  uses: tj-actions/[email protected]

- name: List all added files
  run: |
    for file in ${{ steps.changed-files.outputs.added_files }}; do
      echo "$file was added"
    done

or you can use renamed_files, deleted_files if it fits better to your needs.

Then you can detect if there are any files that may trigger your generation action - if not, simply end the workflow.

Upvotes: 2

Related Questions