13

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 Answer 1

14

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 }}"
2
  • 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, 2020 at 16:45
  • The github.event.* context is the event payload. You can see an example of the payload in the documentation here. 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, 2020 at 23:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.