How to get a branch name on GitHub action when push on a tag?

南笙酒味 提交于 2020-12-13 04:07:27

问题


I trigger my workflow using

on:
  push:
    tags:

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


回答1:


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



来源:https://stackoverflow.com/questions/63745613/how-to-get-a-branch-name-on-github-action-when-push-on-a-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!