12

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.

Vincent
  • 4,876
  • 3
  • 44
  • 55

1 Answers1

12

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 }}"
peterevans
  • 34,297
  • 7
  • 84
  • 83
  • Are the variables `github.event.*` populate from the content of the file `$GITHUB_EVENT_PATH`? How come they are not environment variables like the others? – Lucas Jun 10 '20 at 16:45
  • The `github.event.*` context is the event payload. You can see an example of the payload in the documentation [here](https://developer.github.com/webhooks/event-payloads/#delete). Every event has a payload like this that is accessible during the workflow via the `github.event` context. Yes, I'm fairly sure the file content at `GITHUB_EVENT_PATH` is the same payload. It wouldn't be sensible for GitHub to turn every property of the payload into environment variables. There would be too many. – peterevans Jun 10 '20 at 23:50