Skip to content

Instantly share code, notes, and snippets.

@rkawajiri
Created April 30, 2015 14:20
Show Gist options
  • Save rkawajiri/964ee6a780d4d7e6d981 to your computer and use it in GitHub Desktop.
Save rkawajiri/964ee6a780d4d7e6d981 to your computer and use it in GitHub Desktop.
gradleでgitのrepositoryの状態をチェックして,git-flowからversionを読み取ってpublish
import org.ajoberstar.grgit.Grgit
def getVersionName = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
return stdout.toString().trim()
}
def extractVersion(str) {
def lsts = str.split("-").toList()
lsts.remove(lsts.size()-1)
lsts.remove(lsts.size()-1)
return lsts.join()
}
task checkGitAndVersioningBeforePublish << {
def repo = Grgit.open(project.file('.'))
def status = repo.status()
if (!status.isClean()) {
throw new GradleException("There are uncommitted files");
}
def currentBranch = repo.branch.getCurrent()
def currentBranchName = currentBranch.getName()
println "Current buranch is ${currentBranchName}"
def trackingBranch = currentBranch.trackingBranch
if (trackingBranch == null) {
throw new GradleException("Push current branch ${currentBranchName}\nor try below command\n\$ git branch --set-upstream-to=origin/${currentBranchName} ${currentBranchName}");
}
def trackingBranchName = trackingBranch.getName()
def log = repo.log {
range trackingBranchName, currentBranchName
}
if (log.size() > 0) {
throw new GradleException("There are ${log.size()} unpushed commits")
}
def repoKeyTemp = null
if (currentBranchName == "master") {
repoKeyTemp = "libs-releases-local"
project.version = "${getVersionName()}"
} else {
repoKeyTemp = "libs-snapshots-local"
project.version = "${extractVersion(getVersionName())}-${currentBranchName.replaceAll('/', '-')}-SNAPSHOT"
}
println "project version is ${project.version}"
artifactory {
publish {
repository {
repoKey = repoKeyTemp
}
}
}
artifactoryPublish {
properties = ['git-hash': "${repo.head().id}"]
}
}
artifactoryPublish.dependsOn checkGitAndVersioningBeforePublish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment