Path filtering on required pull request checks

We have a monorepo holding multiple microservices. On a pull request, we want to only run unit tests on a service that has been changed within the PR. We accomplish this with a workflow syntax like this:

on:
  pull_request:
    paths:
      - 'serviceA/*'

However, when we mark the branch protection check as required then we can’t merge any pull requests that do not include changes to serviceA. Is there a way we can work around this?

1 Like

Hi @connorgraham ,

I can repro the same on my side, the required check run will stay at ‘Expected- Waiting…’ status.

You can remove paths setting for pull_request event, and remove checkrun from branch protection rule. Instead, check the file commit info, execute different jobs according to the file path. If it comes from service A, execute job1, if it comes from other file, execute job2…etc, it will skip unneccessary jobs and only one check run in pull request.

Please check the link about how to get the file name(path).

Or you can create another yaml file with different paths setting, cover the files changes out of service A.

Thanks

1 Like

Thanks @weide-zhou 

I solved it using the github actions script:

on: [pull_request]

jobs:
   test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Check if service has changed
        uses: actions/github-script@0.9.0
        id: service-changed
        with:
          result-encoding: string
          script: |
            const result = await github.pulls.listFiles({
              owner: context.payload.repository.owner.login,
              repo: context.payload.repository.name,
              pull_number: context.payload.number,
              per_page: 100
            })
            const serviceChanged = result.data.filter(f => f.filename.startsWith("serviceA/")).length > 0
            console.log(serviceChanged)
            return serviceChanged

      - uses: actions/checkout@v2
        if: ${{ steps.service-changed.outputs.result == 'true' }}

I published an Action that solves all those required-check-problems: https://github.com/marketplace/actions/skip-duplicate-actions

This is similar to GitHub’s path filter, but the underlying algorithm is smarter.
You can give it a try if this is still an issue.