如何使用Gradle提交/推送Git标签?

Ric*_*ral 6 git tagging gradle

我创建了一个特定的Gradle任务,只能在Jenkins构建系统中调用.我需要让这个任务依赖于另一个任务,它应该在成功编译项目后标记主分支的HEAD.

我不知道如何使用Gradle将标签提交/推送/添加到远程存储库中的特定分支.实现这一目标的最简单方法是什么?

任何帮助真的很感激......

Ben*_*hko 15

以下是使用Gradle Git插件实现场景的方法.关键是要查看插件提供的Javadoc.

buildscript {
   repositories { 
      mavenCentral() 
   }

   dependencies { 
      classpath 'org.ajoberstar:gradle-git:0.6.1'
   }
}

import org.ajoberstar.gradle.git.tasks.GitTag
import org.ajoberstar.gradle.git.tasks.GitPush

ext.yourTag = "REL-${project.version.toString()}"

task createTag(type: GitTag) {
   repoPath = rootDir
   tagName = yourTag
   message = "Application release ${project.version.toString()}"
}

task pushTag(type: GitPush, dependsOn: createTag) {
   namesOrSpecs = [yourTag]
}
Run Code Online (Sandbox Code Playgroud)


Ole*_*huk 7

我喜欢这个:

private void createReleaseTag() {
    def tagName = "release/${project.version}"
    ("git tag $tagName").execute()
    ("git push --tags").execute()
}
Run Code Online (Sandbox Code Playgroud)

编辑:更广泛的版本

private void createReleaseTag() {
    def tagName = "release/${version}"
    try {
        runCommands("git", "tag", "-d", tagName)
    } catch (Exception e) {
        println(e.message)
    }
    runCommands("git", "status")
    runCommands("git", "tag", tagName)
}

private String runCommands(String... commands) {
    def process = new ProcessBuilder(commands).redirectErrorStream(true).start()
    process.waitFor()
    def result = ''
    process.inputStream.eachLine { result += it + '\n' }
    def errorResult = process.exitValue() == 0
    if (!errorResult) {
        throw new IllegalStateException(result)
    }
    return result
}
Run Code Online (Sandbox Code Playgroud)

你可以处理异常.


for*_*dya 3

您可以使用上面评论中指出的 Exec 或使用 JGit 来推送标签。在java中创建一个插件/类并在gradle中使用它