dorny / paths-filter

Conditionally run actions based on files modified by PR, feature branch or pushed commits
MIT License
2.14k stars 239 forks source link

Detecting changes against main branch on main branch merge? #201

Open ScottPierce opened 1 year ago

ScottPierce commented 1 year ago

I'm trying to detect changes on the main branch when merging a PR into the main branch. I kept on getting

Error: The process '/usr/bin/git' failed with exit code 128

I've solved it by

  1. Checking out the repository beforehand
  2. Manually grabbing the previous commit sha and saving it to an environment variable
  3. Using the commit sha as a base for the query.

Surely I'm over complicating this, and there is a simpler way to do this?

on:
  push:
    branches:
      - main
  workflow_dispatch:
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}
          fetch-depth: 2

      - name: Get previous commit SHA
        id: prev_commit
        run: echo "PREV_COMMIT_SHA=$(git rev-parse HEAD~1)" >> $GITHUB_ENV

      - uses: dorny/paths-filter@v2
        id: filter
        with:
          base: ${{ env.PREV_COMMIT_SHA }}
          filters: |
            changed:
              - 'example**'
castarco commented 1 year ago

I'm having a similar problem. The action works perfectly fine in PRs/branches, but once I perform a merge operation... it fails because of the same problem.

I'm considering doing something similar as to what you have done, although perhaps even more complicated, with some added conditional logic to only perform the checkout when there is a merge operation (what remains to be seen is how I'll make it to define base conditionally, as it doesn't make sense to obtain PREV_COMMIT_SHA if I didn't perform a checkout yet).

Another option is to make this to not execute this action on merge, and modify the conditionals that depend on this one to also execute on merge. That would save less time in CI, but I don't expect as many merge operations as commits to PRs.

EDIT: It seems that we can use conditionals in expressions https://github.com/actions/runner/issues/409 , the approximation is a bit brittle ("same" as in POSIX shell, ugh), but better than nothing.

ozancaglayan commented 11 months ago

Hi both.

Can't you achieve this with just base: ${{ github.ref }} ? It seems that its working correctly for both open PRs and further pushes to the PR and when I merge the PR to main. Curious if I'm missing anything.

RuBiCK commented 1 month ago

I have the same problem and I'm curious to know if anyone has a solution before I start spending time on tests

ahmedhosny commented 1 month ago

Same issue here where no changes exist when PR is merged into main.

RuBiCK commented 1 month ago

My use case involves a merge commit from a main branch PR to a different branch. I have successfully managed to get it working.

I needed to download the full repo, get the previous commit and use it as a base.

      - uses: actions/checkout@v4
        with:
         fetch-depth: 0   

      - name: Get previous commit
        id: prev-commit
        run: |
          PREV_COMMIT=$(git rev-list -n 1 HEAD^1)
          echo "PREV_COMMIT=$PREV_COMMIT" >> $GITHUB_ENV

      - id: filter
        uses: dorny/paths-filter@v3
        with:
          base: ${{ env.PREV_COMMIT }}
ahmedhosny commented 1 month ago

Thanks! My solution was to avoid running the action all together on pushes to main (not needed for my use case to begin with) - and instead run it on pull requests only.