fuzzi
fuzzi

Reputation: 2277

Trigger a GitHub action on pull request approval and path

I want to build a GitHub Action that triggers on Pull Request (PR) Approval, but only when the PR contains modification to a particular path.

Currently, I have the following implementation:

on:
  pull_request_review:
    types: [submitted]
    paths: ['mypath/**']

jobs:
  build:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2
      - name: job name
        if: github.event.review.state == 'approved'

However, the build job triggers on Approval, and seems to ignore the path. The build runs on any Approval regardless of what files have been modified in the PR.

Is it possible to trigger a GitHub Action only when a PR modifies a particular path and it is approved?

Upvotes: 12

Views: 14073

Answers (2)

GuiFalourd
GuiFalourd

Reputation: 22890

I know @fuzzi found an alternative using another solution with labels, but here is my contribution if someone wants to resolve the question issue keeping the original premisses:

Context

The ON trigger conditions work as OR and not as AND. Therefore, in the question sample, the workflow will trigger when a pull_request_review is submitted OR when one of the updated file(s) path is the one informed.

Workaround

It's not possible to check both conditions through the on workflow level field alone. Therefore, if you want to check both conditions, you would have to do it separately. A solution could be to check the submitted PR in the ON trigger first, then checking the folder path in a job step.

Example

Here is an example of what the solution suggested above looks like using the paths-changes-filter action:

on:
  pull_request_review:
    types: [submitted]

jobs:
  build:
    runs-on: self-hosted #or any other runner
    steps:
      - uses: actions/checkout@v2
      - uses: dorny/paths-filter@v2
        id: changes
        with:
          filters: |
             mypath:
              - 'mypath/**'
      # run only if some file in 'src' folder was changed
      - if: steps.changes.outputs.mypath == 'true' && github.event.review.state == 'approved'
        run: ...

Upvotes: 11

fuzzi
fuzzi

Reputation: 2277

I found a solution to this using GitHub Labels instead, rather than file paths.

I implemented a GitHub Action for automatically adding a Label to the PR, based on the updated file paths. (https://github.com/actions/labeler)

I then modified my existing GH Action to check the Label value. My condition is now:

if: github.event.label.name == 'project' && github.event.review.state == 'approved'

Upvotes: 2

Related Questions