我如何获得gradle中的当前git分支? [英] How can I get the current git branch in gradle?
问题描述
我正在编写一个任务将我的应用程序部署到服务器。不过,我希望只有当我的当前git分支是主分支时才能运行此任务。
如何获得当前的git分支?
I'm writing a task to deploy my application to the server. However, I'd like for this task to run only if my current git branch is the master branch. How can I get the current git branch?
我知道有一个 gradle-git插件,它的方法<$在任务 GitBranchList
下执行c $ c> getWorkingBranch(),但任何时候我尝试执行
I know there is a gradle-git plugin that has a method getWorkingBranch()
under the task GitBranchList
, but anytime I try to execute
task getBranchName(type: GitBranchList) {
print getWorkingBranch().name
}
我得到一个任务尚未执行的错误。我查看了源,并且在没有分支集时抛出该错误。这是否意味着这种方法并没有达到我认为的那样?我需要设置分支吗?
I get a "Task has not executed yet" error. I looked at the source and it throws that error when there is no branch set. Does that mean this method doesn't do quite what I think it does? That I need to set the branch somewhere?
推荐答案
不,这并不意味着分支没有设置。这意味着该任务还没有真正执行。你要做的是在配置闭包中调用一个方法,而你可能想在任务执行后调用它。尝试将您的任务更改为:
No this doesn't mean that the branch is not set. It means that the task hasn't really executed yet. What you're trying to do is calling a method in the configuration closure, whereas you probably want to call it after task execution. Try to change your task to:
task getBranchName(type: GitBranchList) << {
print getWorkingBranch().name
}
使用<<
您正在添加一个doLast,该任务将在任务执行完成后执行。
With the <<
you're adding a doLast, which will be executed after the task has been executed.
这篇关于我如何获得gradle中的当前git分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!