How to commit/push a Git tag with Gradle?
Asked Answered
M

4

9

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...

Marquise answered 28/7, 2013 at 16:44 Comment(4)
Using Exec? gradle.org/docs/current/dsl/org.gradle.api.tasks.Exec.htmlDreadfully
gradle-git plugin looks good.Ryon
@JBNizet Hum, maybe... I'll have to look into that.Marquise
@PeterNiederwieser I've looked into that before but I find the documentation/examples quite lacking and can't make it work properly.Marquise
D
3

You can use Exec as pointed in above comment or use JGit to push tag. Create a plugin / class in java and use it gradle

Deferment answered 28/7, 2013 at 17:3 Comment(1)
Both solutions are nice but I ended up using this one... Can't make SSH work with the gradle-git plugin and it just worked calling the command line. Don't have any more time to waste on this and I need it working.Marquise
S
16

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]
}
Soble answered 29/7, 2013 at 0:0 Comment(4)
This seems to be the way to go and it's working up to a point... I can't seem to authenticate and push to the server. I'm asked for a password (no username) so I try to use the passphrase for my SSH key but it's not working. Any help getting SSH authentication working?Marquise
Are you using GitHub? If yes, did you register your SSH key with your account on the page? I'd check with GitHub's FAQ section. If that doesn't help I'd send a quick email to the plugin author. He's very helpful and responsive.Soble
No, not using GitHub for this? It's an internal installation of GitLab. My SSH key is working with my Git client, only with this plugin is not.Marquise
JGit is the underlying core for the behavior of gradle-git. If you have more than one SSH key and think it could be picking up the wrong one, you could try adding the right one to ssh-agent or Pageant.Gustie
C
10

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.

Conjugated answered 9/3, 2017 at 9:10 Comment(3)
how would you handle the error when a tag already exists or can't be pushed?Santosantonica
@Santosantonica I modified my response. Now you can handle Exceptions.Conjugated
awesome, i will give it a trySantosantonica
D
3

You can use Exec as pointed in above comment or use JGit to push tag. Create a plugin / class in java and use it gradle

Deferment answered 28/7, 2013 at 17:3 Comment(1)
Both solutions are nice but I ended up using this one... Can't make SSH work with the gradle-git plugin and it just worked calling the command line. Don't have any more time to waste on this and I need it working.Marquise
B
0

You can easily achieve this with exec.

val myTag = "foo"
tasks.register("gitTag") {
    doLast {
        exec { commandLine("git", "tag", myTag) }
        exec { commandLine("git", "push", "origin", "tag", myTag) }
    }
}

The gitTag task then only needs to be called in your pipeline.

Biisk answered 15/9 at 19:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.