Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ScottPierce opened this issue Sep 12, 2023 · 6 comments
Open

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

ScottPierce opened this issue Sep 12, 2023 · 6 comments

Comments

@ScottPierce
Copy link

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
Copy link

castarco commented Sep 27, 2023

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 actions/runner#409 , the approximation is a bit brittle ("same" as in POSIX shell, ugh), but better than nothing.

SubJunk added a commit to UniversalMediaServer/UniversalMediaServer that referenced this issue Nov 5, 2023
SubJunk added a commit to UniversalMediaServer/UniversalMediaServer that referenced this issue Nov 5, 2023
* run tests

* Try to stop the test failing in main

Based on dorny/paths-filter#201
@ozancaglayan
Copy link

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
Copy link

RuBiCK commented Aug 10, 2024

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

@ahmedhosny
Copy link

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

@RuBiCK
Copy link

RuBiCK commented Aug 14, 2024

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants