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

Viewed 3365

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.

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 }}"
Related