Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GitHub Actions, is it possible to access the name of a deleted branch?

It is possible to have a GitHub Action trigged by the delete event. However, according to those, the GITHUB_REF variable then points to the default branch, rather than the branch that was deleted. (Likewise for the push event.)

Is it possible to obtain the name of the branch that was deleted? Specifically, I'd like to clean up a deployment with the branch name's ID that was created in response to the push event.

like image 828
Vincent Avatar asked Jun 09 '20 11:06

Vincent


People also ask

Can I see deleted branches GitHub?

Restoring a deleted branchOn GitHub.com, navigate to the main page of the repository. Under your repository name, click Pull requests. Click Closed to see a list of closed pull requests. In the list of pull requests, click the pull request that's associated with the branch that you want to restore.

What happens when you delete a branch in GitHub?

If you delete a head branch after its pull request has been merged, GitHub checks for any open pull requests in the same repository that specify the deleted branch as their base branch. GitHub automatically updates any such pull requests, changing their base branch to the merged pull request's base branch.

Can I recover deleted branch in git?

A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches.

What happens to pull request when branch is deleted?

Previously, GitHub's web UI did not allow deleting a branch that was associated with an open pull request. Now you can delete such a branch from the UI. However, doing so will close all open pull requests associated with the branch. Before the branch is deleted, you must confirm that the pull requests may be closed.


1 Answers

You can access github.event.ref and github.event.ref_type from the github context.

The event will trigger when other ref types are deleted, too. So you need to filter just branch deletions.

name: Branch Deleted
on: delete
jobs:
  delete:
    if: github.event.ref_type == 'branch'
    runs-on: ubuntu-latest
    steps:
      - name: Clean up
        run: |
          echo "Clean up for branch ${{ github.event.ref }}"
like image 129
peterevans Avatar answered Oct 14 '22 14:10

peterevans