How to commit / click a Git tag using Gradle?

I created a specific Gradle task that should only be called on the Jenkins build system. I need to make this task depend on another, which should mark the HEAD of the master branch after the project is successfully compiled.

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

Any help really appreciated ...

+6
source share
3 answers

You can use Exec as described above, or use JGit for a push tag. Create a plugin / class in java and use it gradle

+3
source

Here you can implement your script using the Gradle Git plugin . The key is to look at the provided Javadocs 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] } 
+13
source

I like it:

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

EDIT: A Larger 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 the exception.

+2
source

Source: https://habr.com/ru/post/950469/


All Articles