在gradle中,我想将当前的分支名称和提交号作为后缀添加到我的versionName中。(为什么?因为当我在Jenkins中构建我的应用程序以在HockeyApp中发布它时,展示该应用程序是从哪个分支构建的是有用的!)
因此,当我在命令提示符中输入此命令时,将返回我的当前分支名称:
git rev-parse --abbrev-ref HEAD
当我在Android中使用这一行时,或者使用this答案中的代码,或者如下面的gradle代码所示,也会发生同样的情况:
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()
}
}
结果日志(这正是我想要的):
“我现在的部门:特写/我的特征”
然而,当我在Jenkins作业中构建我的应用程序时,它会输出一个不同的结果:
“我现在的部门:头”
为什么会发生这种情况,以及如何正确检索Jenkins中的当前分支名称?
编辑:
我使用了一种不同的方法,在大多数情况下正确返回branchName,在Jenkins上也是如此:
git name-rev --name-only HEAD
示例输出提示符:
“我现在的部门:特写/我的特征”
Jenkins中的示例输出:
我目前的分支:远程/起源/特征/特征
我可以删除“远程/起源/”,如果我愿意,所以这是可以的!
但是这种方法会引起不同的麻烦(无论是提示符、gradle还是Jenkins)。在标记上一次提交时,它不会输出分支名称,但如下所示:
“我的当前分支:标记/MyTag^0”
编辑2:
第三种方法可以找到here。
包括答案下面的注释,我可以使用grep *在提示中检索分支。但是,我不能在gradle代码中使用反斜杠。这是失败的:
commandLine 'git', 'branch', '|', 'grep', '\\*'
有什么建议吗?
发布于 2016-12-08 12:32:19
尝试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访问它
https://stackoverflow.com/questions/41044807
复制相似问题