推送标签时如何在GitHub动作上获取分支名称? [英] How to get a branch name on GitHub action when push on a tag?

查看:58
本文介绍了推送标签时如何在GitHub动作上获取分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用触发我的工作流程

 上:推:标签: 

GITHUB_REF 在这种情况下将不包含分支名称,我怎么能得到它?

解决方案

您将需要进行一些字符串操作才能实现此目的.基本上,在创建标签的过程中,就像您要在本地执行 git checkout v< tag> 一样,但是没有引用原始分支.这就是为什么您需要在git分支包含命令中使用 -r 标志.

我们通过以下两个命令获得clean分支.

  raw = $(git branch -r-包含$ {{github.ref}})分支= $ {原始/来源\/} 

这里是创建分支环境的管道

 名称:标记上:创造:标签:-v *职位:工作1:运行:ubuntu-latest脚步:-名称:结帐源代码用途:actions/checkout @ v1-名称:获取分支运行:raw = $(git branch -r-包含$ {{github.ref}})分支= $ {原始/来源\/}echo :: set-env name = BRANCH :: $ branch-运行:echo $ {{env.BRANCH}} 

工作示例

注意::我通过创建标签并将其推送到原始位置触发了上述管道

I trigger my workflow using

on:
  push:
    tags:

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

解决方案

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

这篇关于推送标签时如何在GitHub动作上获取分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆