Calling Revert on Merged PR that Fails Test Deletes Contents of Repo

I have a github action that runs unit and end 2 end tests after a PR is merged into the default branch. If the tests fail I run the following steps in order to keep the default branch healthy:

  1. Get the Sha to revert with REVERT_SHA=$(git rev-parse HEAD)
  2. Revert the commit with git revert -m 1 ${{ REVERT_SHA }} --no-edit
  3. Push the reverted commit with git push origin ${{ env.CURRENT_BRANCH }}

Originally this worked and now all of a sudden when these steps execute on test failures it nukes the entire repo deleting everything in it.

Hi @brysonr,

Glad to see you in Github Community Forum!

To revert the default branch, you can use git bash on local machine, git reset --hard commitid, and push back to the remote.

For your CI progress, i don’t think you need to run tests after PR merged.
The github.ref of event pull_request is ‘refs/pull/:prNumber/merge’, which is a fake merged branch, that means you can run tests on it, merge the pull request only if workflow passed. It could be more safe. And you can directly revert the change on the pull request if you don’t want the change.

If you still have any queries, it’s recommended to share your repo/sample repo for further investigation.

Thanks

1 Like

Currently I’m calling the action on PR merged event since i don’t want to waste minutes on running it with ever PR update. As such, is there still a way to run an action when the PR is finished and merged but still prevent the actual merge that triggered the action if the tests fail?

@weide-zhou Can you elaborate on this? It sounds like there’s built-in functionality to revert the change from a pull_request event, but I’m unclear on how to perform this action.

Hi @brysonr,

If the PR is finished and merged, it means changes has been applied to the branch.

Based on your description:

i don’t want to waste minutes on running it with ever PR update

Do you consider to add label to the PR, use activity type ‘label’ in pull request instead of ‘closed(merge)’? For example, once you think it’s time to have a test, add a label to trigger the workflow, merge the PR when it’s passed? It can also avoid unneccessary workflows when you update the PR.

Thanks

Hi @zebraflesh,

After pull request merged, you can click revert button below:

Thanks

Oh I see. I was also looking for a solution to this problem, but I needed one that’s available from GitHub Actions.