2

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?

GoldFlsh
  • 1,095
  • 2
  • 11
  • 26
  • See https://stackoverflow.com/questions/58033366/how-to-get-current-branch-within-github-actions – riQQ Jul 07 '20 at 19:14
  • 1
    That's not the same thing, as that's specifically about getting the branch name from the env variable `GITHUB_REF`. My question is about how to extract the branch name from `github.event.ref`. This wasn't a good example since in the delete event, `github.event.ref` has the simple branch name. But if I was in a different event, how would I drop the prefix `refs/heads` from this? – GoldFlsh Jul 08 '20 at 12:02

1 Answers1

6

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
chenrui
  • 8,910
  • 3
  • 33
  • 43
  • Thanks. I just noticed that myself in the docs that it's just the simple-ref on delete events and not the full one. Though it looks like `github.event.ref` is sometimes the full ref. I wonder how to manipulate github context data if I were to need to drop a prefix for a different event type. – GoldFlsh Jul 07 '20 at 17:15
  • 1
    Updated the original post to include the prefix strip script. – chenrui Jul 07 '20 at 17:53