[英]How can I get the current git branch in gradle?
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.
但是,我希望此任务仅在我当前的git分支是主分支时运行。 How can I get the current git branch?
我怎样才能获得当前的git分支?
gradle-git
approach: gradle-git
方法: I know there is a gradle-git plugin that has a method getWorkingBranch()
under the task GitBranchList
, but anytime I try to execute 我知道有一个gradle-git插件在任务
GitBranchList
下有一个getWorkingBranch()
方法,但是我试着执行
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?
我需要在某个地方设置分支?
You also are able to get git branch name
without the plug-in. 你也可以在没有插件的情况下获得
git branch name
。
def gitBranch() {
def branch = ""
def proc = "git rev-parse --abbrev-ref HEAD".execute()
proc.in.eachLine { line -> branch = line }
proc.err.eachLine { line -> println line }
proc.waitFor()
branch
}
Refer to: Gradle & GIT : How to map your branch to a deployment profile 请参阅: Gradle和GIT:如何将分支映射到部署配置文件
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
}
With the <<
you're adding a doLast, which will be executed after the task has been executed. 使用
<<
你正在添加一个doLast,它将在任务执行后执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.