简体   繁体   English

如何在gradle中获取当前的git分支?

[英]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.

相关问题 如何在 Git 的当前分支中获取最新的标签名称? - How can I get the latest tag name in current branch in Git? 如何将当前分支与Git中的另一个远程分支进行比较? 不是主人 - How can I get my current branch compared with another remote branch in Git? not master 如何获得另一个git分支到当前分支? - How do I get another git branch up to current branch? 如何使用 git 将 URL 获取到我当前分支的合并请求或拉取请求? - How can I get the URL to a merge request or a pull request of my current branch using git? 如何获取 Git 中的当前分支名称? - How do I get the current branch name in Git? 如何在Xcode9上看到当前的git分支? - How can I see the current git branch on Xcode9? 如何将当前的git分支标记为ok,然后继续 - How can I label my current git branch as ok then continue 如何将当前分支中的 GIT 标记读取到变量中? - How can I read a GIT tag from the current branch into a variable? 我怎样才能找到我当前分支所基于的git分支? - How can I find out the git branch my current branch is based on? 如何使用Git根据同一分支中的先前提交将新的提交添加到当前分支? - How can I use Git to add new commits to the current branch based on a previous commit in that same branch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM