How can I get the current git branch in gradle?

后端 未结 2 350
北荒
北荒 2021-02-01 19:20

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

相关标签:
2条回答
  • 2021-02-01 19:59

    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.

    0 讨论(0)
  • 2021-02-01 20:16

    You also are able to get git branch name without the plug-in.

    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

    0 讨论(0)
提交回复
热议问题