Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Support git tag based release (#363)
Browse files Browse the repository at this point in the history
- version calculation is automated based on git commits and tags
  • Loading branch information
usmansaleem authored Mar 11, 2021
1 parent 53e6ec5 commit 2c78c84
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 70 deletions.
111 changes: 44 additions & 67 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* specific language governing permissions and limitations under the License.
*/


import groovy.transform.Memoized
import net.ltgt.gradle.errorprone.CheckSeverity

import java.text.SimpleDateFormat
Expand All @@ -22,7 +24,7 @@ plugins {
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'me.champeau.gradle.jmh' version '0.5.0' apply false
id 'net.ltgt.errorprone' version '1.1.1'
id 'net.researchgate.release' version '2.8.1'
id 'org.ajoberstar.grgit' version '4.1.0'
}

String projectName = "EthSigner"
Expand All @@ -35,6 +37,10 @@ if (!JavaVersion.current().java11Compatible) {
" Detected version ${JavaVersion.current()}")
}

rootProject.version = calculatePublishVersion()
def specificVersion = calculateVersion()
def isDevelopBuild = rootProject.version.contains('develop')

def cloudsmithUser = project.hasProperty('cloudsmithUser') ? project.property('cloudsmithUser') : System.getenv('CLOUDSMITH_USER')
def cloudsmithKey = project.hasProperty('cloudsmithApiKey') ? project.property('cloudsmithApiKey') : System.getenv('CLOUDSMITH_API_KEY')

Expand Down Expand Up @@ -510,7 +516,7 @@ task dockerUpload(type: Exec) {
additionalTags.add('develop')
}

if (!(version ==~ /.*-SNAPSHOT/)) {
if (!isDevelopBuild) {
additionalTags.add('latest')
additionalTags.add(version.split(/\./)[0..1].join('.'))
}
Expand Down Expand Up @@ -540,17 +546,6 @@ task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {

configurations { annotationProcessor }

// Prevent errorprone-checks being dependent upon errorprone-checks!
// However, ensure all subprojects comply with the custom rules.
/*
configure(subprojects.findAll {it.name != 'errorprone-checks'}) {
dependencies { annotationProcessor project(":errorprone-checks") }
tasks.withType(JavaCompile) {
options.annotationProcessorPath = configurations.annotationProcessor
}
}
*/

// http://label-schema.org/rc1/
// using the RFC3339 format "2016-04-12T23:20:50.52Z"
def buildTime() {
Expand All @@ -559,67 +554,51 @@ def buildTime() {
return df.format(new Date())
}

// Takes the version, and if -SNAPSHOT is part of it replaces SNAPSHOT
// with the git commit version.
def calculateVersion() {
String version = rootProject.version
if (version.endsWith("-SNAPSHOT")) {
version = version.replace("-SNAPSHOT", "-dev-" + getCheckedOutGitCommitHash())
// Calculate the version that this build would be published under (if it is published)
// If this exact commit is tagged, use the tag
// If this is on a release-* branch, use the most recent tag appended with +develop (e.g. 0.1.1-RC1+develop)
// Otherwise, use develop
def calculatePublishVersion() {
if (!grgit) {
return 'UNKNOWN'
}
return version
def specificVersion = calculateVersion()
def isReleaseBranch = grgit.branch.current().name.startsWith('release-')
if (specificVersion.contains('+')) {
return isReleaseBranch ? "${specificVersion.substring(0, specificVersion.indexOf('+'))}+develop" : "develop"
}
return specificVersion
}

def getCheckedOutGitCommitHash() {
def gitFolder = "$projectDir/.git/"
if (!file(gitFolder).isDirectory()) {
// We are in a submodule. The file's contents are `gitdir: <gitFolder>\n`.
// Read the file, cut off the front, and trim the whitespace.
gitFolder = file(gitFolder).text.substring(8).trim() + "/"
// Calculate the version that `product --version` will report (among other places)
// If this exact commit is tagged, use the tag
// Otherwise use git describe --tags and replace the - after the tag with a +
@Memoized
def calculateVersion() {
if (!grgit) {
return 'UNKNOWN'
}
def takeFromHash = 8
/*
* '.git/HEAD' contains either
* in case of detached head: the currently checked out commit hash
* otherwise: a reference to a file containing the current commit hash
*/
def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd

if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb

def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
refHead.text.trim().take takeFromHash
String version = grgit.describe(tags: true)
if (version == null) {
return "UNKNOWN+g${grgit.head().abbreviatedId}"
}
def versionPattern = ~/^(?<lastVersion>.*)-(?<devVersion>[0-9]+-g[a-z0-9]+)$/
def matcher = version =~ versionPattern
if (matcher.find()) {
return "${matcher.group("lastVersion")}+${matcher.group("devVersion")}"
}
return version
}

apply plugin: 'net.researchgate.release'

task releaseIntegrationTest(type: Test) {
for (TaskContainer taskList : subprojects.tasks) {
def subProjectIntegrationTask = taskList.findByName('integrationTest')

if (subProjectIntegrationTask != null) {
dependsOn subProjectIntegrationTask
}
task printVersion() {
doFirst {
print "Specific version: ${specificVersion} Publish version: ${project.version}"
}
}

task releaseAcceptanceTest(type: Test, dependsOn: ':acceptance-tests:acceptanceTest') {}

release {
preTagCommitMessage = '[Gradle Release Plugin] - pre tag commit: '
tagCommitMessage = '[Gradle Release Plugin] - creating tag: '
newVersionCommitMessage = '[Gradle Release Plugin] - new version commit: '
buildTasks = [
'build',
'releaseIntegrationTest',
'releaseAcceptanceTest',
'checkLicenses',
'javadoc'
]

git {
requireBranch = project.hasProperty('release.branch') ? project.property('release.branch') : 'master'
}
def getCheckedOutGitCommitHash() {
def takeFromHash = 8
grgit ? grgit.head().id.take(takeFromHash) : 'UNKNOWN'
}

task cloudsmithUpload {
Expand All @@ -635,8 +614,6 @@ task cloudsmithUpload {
}
}

afterReleaseBuild.dependsOn cloudsmithUpload

def calculateJarName(Project project) {
def jarName = project.name
def parent = project.parent
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
org.gradle.jvmargs=-Xmx1g
version=21.1.1-SNAPSHOT
besuVersion=1.5.0
besuDistroUrl=https://bintray.com/hyperledger-org/besu-repo/download_file?file_path=besu-${besuVersion}.tar.gz
besuDistroUrl=https://hyperledger.jfrog.io/artifactory/besu-binaries/besu/${besuVersion}/besu-${besuVersion}.tar.gz
hashicorpVaultVersion=1.4.3
hashicorpVaultUrl=https://releases.hashicorp.com/vault
2 changes: 1 addition & 1 deletion scripts/cloudsmith-upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TAR_DIST="${DIST}/${DIST_IDENTIFIER}-${VERSION}.tar.gz"
TAR_NAME="${DIST_IDENTIFIER}.tar.gz"

REPUBLISH=""
if [[ $VERSION == *"SNAPSHOT"* ]]; then
if [[ $VERSION == *"develop"* ]]; then
REPUBLISH="--republish"
fi

Expand Down

0 comments on commit 2c78c84

Please sign in to comment.