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

rfgamaral
  • 16,546
  • 57
  • 163
  • 275

3 Answers3

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]
}
Benjamin Muschko
  • 32,442
  • 9
  • 61
  • 82
  • 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? – rfgamaral Jul 29 '13 at 11:31
  • 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](https://help.github.com/categories/56/articles). If that doesn't help I'd send a quick email to the plugin author. He's very helpful and responsive. – Benjamin Muschko Jul 29 '13 at 11:42
  • 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. – rfgamaral Jul 29 '13 at 11:45
  • 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. – ajoberstar Jul 30 '13 at 00:14
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.

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

robd
  • 9,646
  • 5
  • 40
  • 59
forvaidya
  • 3,041
  • 3
  • 26
  • 33
  • 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. – rfgamaral Jul 29 '13 at 17:17