GoldFlsh
GoldFlsh

Reputation: 1334

How to Extract Branch Name on Delete Event Github Actions

I am trying to extract the branch name on a delete event. Turns out it's not in the GITHUB_REF object as that will be the default branch.

Ordinarily I would run

- name: Extract branch name
  shell: bash
  run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  id: extract_branch

But apparently with delete events I need to extract the branch via ${{ github.event.ref }}

- name: Extract branch name
  shell: bash
  run: echo "##[set-output name=branch;]$(echo ${{ github.event.ref }})" # how to drop refs/heads/?
  id: extract_branch

now I don't know how to drop the refs/heads aspect of the branch name.

EDIT: Since in the delete event case, github.event.ref already contains the simple branch name e.g. feature-1-my-branch and not refs/heads/feature-1-my-branch my example code above works.

In the event I want to do some post-processing on this context in a different event type, where github.event.ref returns refs/heads/feature-1-my-branch how would I drop the refs/heads in that case?

Upvotes: 2

Views: 3938

Answers (1)

chenrui
chenrui

Reputation: 9866

You can just use ${{ github.event.ref }} to reference the branch name, the full delete event payload is documented in the GitHub API docs.

I also did a test myself. With workflow defined in here.

    steps:
      - uses: actions/checkout@v2
      - name: run build
        run: |
          echo "GITHUB_SHA is ${{ github.sha }}"
          echo "GITHUB_REF is ${{ github.ref }}"
          echo "${{ github.event.ref }} - ${{ github.event.ref_type }}"

I can trigger a run by pushing and deleting a branch (it would also apply for tag as well). It leads to a run like this.

GITHUB_SHA is feb56d132c8142995b8fea6fd67bdd914e5e0d68
GITHUB_REF is refs/heads/master
so-62779643-test-delete-event-test2 - branch

[update]

For strip out the prefix in GITHUB_REF, here is what I did:

      - uses: actions/checkout@v2
      - name: run build
        run: |
          echo "::set-env name=GITHUB_REF::${{ github.ref }}"
          echo "old GITHUB_REF is $GITHUB_REF"
          GITHUB_REF=$(echo $GITHUB_REF | sed -e "s#refs/heads/##g")
          echo "new GITHUB_REF is $GITHUB_REF"

run log reference

old GITHUB_REF is refs/heads/master
new GITHUB_REF is master

Upvotes: 7

Related Questions