在提示或Android gradle中检索git分支名称:在Jenkins上,它返回HEAD [英] Retrieving git branch name in prompt or Android gradle: on Jenkins it returns HEAD

查看:297
本文介绍了在提示或Android gradle中检索git分支名称:在Jenkins上,它返回HEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gradle中,我想将当前的分支名称和提交编号作为后缀添加到我的versionName中. (为什么?因为当我在Jenkins中构建我的应用程序以在HockeyApp中发布它时,显示该应用程序是从哪个分支提交的是很有用的!)

In gradle I'd like to add both the current branch-name and commit-number as suffix to my versionName. (Why? Because when I build my app in Jenkins to release it in HockeyApp, it's useful to show what branch & commit that app was built from!)

因此,当我在命令提示符下输入此命令时,将返回当前的分支名称:

So when I enter this in command prompt, my current branch name is returned:

git rev-parse --abbrev-ref HEAD

当我在Android gradle中使用此行,使用答案中的代码或如下所示时,也会发生同样的情况这段gradle代码:

Same happens when I use this line in Android gradle, using the code in either this answer, or as shown in this piece of gradle code:

def getVersionNameSuffix = { ->

    def branch = new ByteArrayOutputStream()
    exec {
        // The command line to request the current branch:
        commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
        standardOutput = branch
    }
    println "My current branch: " + branch
    def versionNameSuffix = "-" + branch

    // ... some other suffix additions ...

    return versionNameSuffix
}

buildTypes {
    debug {
        applicationIdSuffix ".test"
        versionNameSuffix getVersionNameSuffix()
    }
}

结果日志(这正是我想要的):

Resulting log (this is exactly what I want):

我当前的分支:功能/我的功能"

"My current branch: feature/MyFeature"

但是,当我在Jenkins作业中构建我的应用程序时,它将输出不同的结果:

However, when I build my app in a Jenkins job, it will output a different result:

我当前的分支:HEAD"

"My current branch: HEAD"

为什么会发生这种情况,以及如何在Jenkins中正确检索我当前的分支名称?

Why does this happen, and how to correctly retrieve my current branch name in Jenkins?

我使用了另一种方法,在大多数情况下,即使在詹金斯上,该方法也可以正确返回branchName:

I've used a different approach, which returns the branchName correctly in most cases, also on Jenkins:

git name-rev --name-only HEAD

提示中的示例输出:

我当前的分支:功能/我的功能"

"My current branch: feature/MyFeature"

Jenkins中的示例输出:

Example output in Jenkins:

我当前的分支:remotes/origin/feature/MyFeature"

"My current branch: remotes/origin/feature/MyFeature"

如果愿意,我可以删除"remots/origin/",所以没关系!

I can remove "remotes/origin/" if i like, so that's okay!

但是这种方法会导致不同的麻烦(在提示,gradle和在Jenkins上).当我标记了最后的提交时,它不会输出分支名称,但是:

But this approach causes different trouble (both in prompt, gradle and on Jenkins). When I have tagged the last commit, it won't output the branch-name, but this:

我当前的分支:标签/MyTag ^ 0"

"My current branch: tags/MyTag^0"

可以在此处找到第三种方法.

A third approach can be found here.

包括答案下面的注释,我可以使用 grep * 在提示符下检索分支.但是,我不能在gradle代码中使用反斜杠.失败:

Including the comments below the answer, I could use grep * to retrieve the branch in prompt. However, I cannot use the backslash in the gradle code. This fails:

commandLine 'git', 'branch', '|', 'grep', '\\*'

有什么建议吗?

推荐答案

尝试使用环境:BRANCH_NAME

Try the env: BRANCH_NAME

BRANCH_NAME 
    For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches.

使用env.BRANCH_NAME进行访问

Access it with env.BRANCH_NAME

这篇关于在提示或Android gradle中检索git分支名称:在Jenkins上,它返回HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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