16

I trigger my workflow using

on:
  push:
    tags:

GITHUB_REF won't contain a branch name in this case, how could I get it?

Dmitrii
  • 161
  • 1
  • 4
  • 1
    You could use `git branch --contains $GITHUB_REF` or `git_branch=$(git for-each-ref | grep ${commit_num} | grep origin | sed "s/.*\///")` (see https://stackoverflow.com/q/12754078/10871900) – dan1st Sep 04 '20 at 17:45
  • @dan1st that doesn't work due to the way that github actions checkout works. You will need to add `-r` flag to contains command to make it work. Example provided in my answer. – Edward Romero Sep 04 '20 at 19:34

3 Answers3

13

You will need to do a bit of string manipulation to get this going. Basically during a tag creation push, is like if you were to do git checkout v<tag> in your local but there's no reference to the original branch. This is why you will need to use the -r flag in the git branch contains command.

We get the clean branch with the following two commands.

    raw=$(git branch -r --contains ${{ github.ref }})
    branch=${raw/origin\/}

Here is a pipeline that creates a branch env

 name: Tag
 on: 
   create:
     tags:
       - v*
 jobs:
   job1:
     runs-on: ubuntu-latest
     steps:
     - name: checkout source code
       uses: actions/checkout@v1
     - name: Get Branch
       run: |
         raw=$(git branch -r --contains ${{ github.ref }})
         branch=${raw/origin\/}
         echo ::set-env name=BRANCH::$branch
     - run: echo ${{ env.BRANCH }}

Working Example

NOTE: I triggered the above pipeline by creating a tag and pushing it to origin

Edward Romero
  • 2,905
  • 1
  • 5
  • 17
  • 8
    Nice solution! I had some issues: 1. `set-env` is deprecated, so I used `echo "BRANCH=$branch" >> $GITHUB_ENV` 2. The output of git branch contains leading whitespace. I used `branch=${raw##*/}` to remove everything before the last slash. – wensveen Dec 02 '20 at 10:56
  • 5
    If you use checkout@v2, only the last commit will be fetched by default, which makes git branch unable to find the current branch. To resolve this, you need to set `fetch-depth: 0` in the checkout@v2 step. See: https://github.com/actions/checkout#Fetch-all-history-for-all-tags-and-branches – wensveen Dec 02 '20 at 14:07
  • Thanks a lot for this guys, awesome solution. – Kevin Vella Jan 20 '21 at 08:44
  • Nice solution. Though to note: A tag can be on multiple branches, so raw can contain a number of branches. – samwize Oct 28 '22 at 06:57
  • I tried to run "git branch -r --contains ${{ github.ref }}" and it outputs nothing. I have also tried to run "git branch -r" and it outputs nothing too. Is this solution no longer available? – Yuk Chan Feb 02 '23 at 22:28
6

Building on Edward's answer, here is a modern script which allows getting branch name and passing it between jobs, including allowing jobs to be conditionally run based on branch

# This only runs for tags, and the job only processes for "master" branch
name: Example

on:
  push:
    tags:
      - "*"

jobs:
  check:
    runs-on: ubuntu-latest
    outputs:
      branch: ${{ steps.check_step.outputs.branch }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Get current branch
        id: check_step
        run: |
          raw=$(git branch -r --contains ${{ github.ref }})
          branch=${raw##*/}
          echo "{name}=branch" >> $GITHUB_OUTPUT
          echo "Branch is $branch."

  job2:
    runs-on: ubuntu-latest
    needs: check # Wait for check step to finish
    if: ${{ needs.check.outputs.branch == 'master' }} # only run if branch is master

    steps:
      - run: echo "Running task..."
Ron S.
  • 563
  • 5
  • 13
  • 1
    FYI, the output capture method will change soon: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ – pixelou Aug 11 '23 at 08:02
1

You can get it from the github.event variable:

on:
  push:
    tags:
      - '*'

jobs:
  main:
    runs-on: ubuntu-latest
    steps:
      - name: Echo branch
        run: echo "${{ github.event.base_ref }}"

The code results with:

enter image description here

egvo
  • 1,493
  • 18
  • 26
  • 3
    That's only for pull_request https://docs.github.com/en/actions/learn-github-actions/contexts#steps-context – kraggle Mar 07 '22 at 21:56
  • 1
    @user2863275 You mean `github.base_ref` and I wrote `github.event.base_ref`. I used it with the `push`, for sure. It is a working solution, it's in my CI/CD. – egvo Jul 20 '22 at 10:04
  • I have it null on a tag push trigger – challet Feb 21 '23 at 15:02
  • @challet Are you sure you use `github.event.base_ref` and not `github.base_ref`? Because they differ. In my case that worked. But I can't assure you for now, it was long ago. – egvo Feb 23 '23 at 07:55