Reputation: 16842
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...
Upvotes: 9
Views: 26077
Reputation: 1836
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.
Upvotes: 0
Reputation: 1053
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.
Upvotes: 10
Reputation: 3315
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
Upvotes: 3
Reputation: 33436
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]
}
Upvotes: 16