Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to commit/push a Git tag with Gradle?

I have created a specific Gradle task that should only be called in the Jenkins build system. I need to make this task depend on another one which should tag the HEAD of the master branch after a successful compilation of the project.

I have no idea how can I commit/push/add tags to a specific branch in remote repository using Gradle. What's the easiest way to achieve this?

Any help is really appreciated...

like image 364
rfgamaral Avatar asked Jul 28 '13 16:07

rfgamaral


People also ask

How do I push a git tag?

You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run git push origin <tagname> . If you have a lot of tags that you want to push up at once, you can also use the --tags option to the git push command.

How do you attach a tag to a commit?

In order to create a Git tag for the last commit of your current checked out branch, use the “git tag” command with the tag name and specify “HEAD” as the commit to create the tag from. Similarly, if you want your tag to be annotated, you can still use the “-a” and “-m” options to annotate your tag.

Can we commit in tag in Git?

Well, in git, a tag is unique to a commit. However, if you really want to use a tag you already used before you will need to delete it from remote and local and recreate it.


2 Answers

Here's how you can implement your scenario with the Gradle Git plugin. The key is to look at the provided Javadocs of the plugin.

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]
}
like image 135
Benjamin Muschko Avatar answered Sep 21 '22 12:09

Benjamin Muschko


I love this:

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

EDIT: A more extensive version

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
}

You can handle Exception.

like image 21
Oleksandr Bondarchuk Avatar answered Sep 21 '22 12:09

Oleksandr Bondarchuk