HHH-14083 Add more precondition checks to release tasks

This commit is contained in:
Yoann Rodière 2020-06-03 10:52:16 +02:00 committed by Andrea Boriero
parent b56ea9f140
commit 55d3aa3a5d
1 changed files with 50 additions and 26 deletions

View File

@ -13,11 +13,52 @@ apply from: rootProject.file( 'gradle/base-information.gradle' )
apply plugin: 'idea' apply plugin: 'idea'
apply plugin: 'distribution' apply plugin: 'distribution'
ext {
if ( !project.hasProperty( 'gitRemote' ) ) {
gitRemote = 'origin'
}
}
idea.module { idea.module {
} }
final File documentationDir = mkdir( "${project.buildDir}/documentation" ); final File documentationDir = mkdir( "${project.buildDir}/documentation" );
task releaseChecks() {
doFirst {
if ( !project.hasProperty('releaseVersion') || !project.hasProperty('developmentVersion')
|| !project.hasProperty('gitRemote') ||!project.hasProperty('gitBranch') ) {
throw new GradleException(
"Release tasks require all of the following properties to be set:"
+ "'releaseVersion', 'developmentVersion', 'gitRemote', 'gitBranch'."
)
}
logger.lifecycle( "Checking that the working tree is clean..." )
String uncommittedFiles = executeGitCommand( 'status', '--porcelain' )
if ( !uncommittedFiles.isEmpty() ) {
throw new GradleException(
"Cannot release because there are uncommitted or untracked files in the working tree."
+ "\nCommit or stash your changes first."
+ "\nUncommitted files:\n" + uncommittedFiles
);
}
logger.lifecycle( "Switching to branch '${project.gitBranch}'..." )
executeGitCommand( 'switch', project.gitBranch )
logger.lifecycle( "Checking that all commits are pushed..." )
String diffWithUpstream = executeGitCommand( 'diff', '@{u}' )
if ( !diffWithUpstream.isEmpty() ) {
throw new GradleException(
"Cannot release because there are commits on the branch to release that haven't been pushed yet."
+ "\nPush your commits to the branch to release first."
);
}
}
}
/** /**
* Assembles all documentation into the {buildDir}/documentation directory. * Assembles all documentation into the {buildDir}/documentation directory.
* *
@ -246,23 +287,12 @@ artifacts {
bundles distZip bundles distZip
} }
task release( dependsOn: [uploadDocumentation, uploadBundlesSourceForge] ) task release( dependsOn: [releaseChecks, uploadDocumentation, uploadBundlesSourceForge] )
def gitRemote = 'origin' task addVersionCommit( dependsOn: [releaseChecks] ) {
if ( project.hasProperty( 'gitRemote' ) ) {
gitRemote = project.property( 'gitRemote' )
}
task addVersionCommit {
group = "Release" group = "Release"
description = "Updates the changelog.txt file, adds a commit for the released version and push the changes to github" description = "Updates the changelog.txt file, adds a commit for the released version and push the changes to github"
doFirst{ doFirst{
if ( !project.hasProperty( 'releaseVersion' ) ) {
throw new GradleException(
"The releaseVersion parameter is necessary to create the version commit."
)
}
logger.lifecycle( "Updating version to '${project.releaseVersion}'..." ) logger.lifecycle( "Updating version to '${project.releaseVersion}'..." )
project.ormVersionFile.text = "hibernateVersion=${project.releaseVersion}" project.ormVersionFile.text = "hibernateVersion=${project.releaseVersion}"
@ -272,19 +302,15 @@ task addVersionCommit {
logger.lifecycle( "Adding commit to update version to '${project.releaseVersion}'..." ) logger.lifecycle( "Adding commit to update version to '${project.releaseVersion}'..." )
executeGitCommand( 'add', '.' ) executeGitCommand( 'add', '.' )
executeGitCommand( 'commit', '-m', project.ormVersion.fullName ) executeGitCommand( 'commit', '-m', project.ormVersion.fullName )
executeGitCommand( 'push', gitRemote ) executeGitCommand( 'push', project.gitRemote )
} }
} }
release.mustRunAfter addVersionCommit
task ciRelease() { task ciRelease( dependsOn: [releaseChecks, addVersionCommit, release] ) {
group = "Release" group = "Release"
description = "Performs a release: the hibernate version is set and the changelog.txt file updated, the changes are pushed to github, then the release is performed, tagged and the hibernate version is set to the development one." description = "Performs a release: the hibernate version is set and the changelog.txt file updated, the changes are pushed to github, then the release is performed, tagged and the hibernate version is set to the development one."
doLast { doLast {
if ( !project.hasProperty( 'developmentVersion' ) ) {
throw new GradleException(
"When the releaseVersion parameter is specified, the parameter developmentVersion is required as well."
)
}
if ( !project.hasProperty( 'noTag' ) ) { if ( !project.hasProperty( 'noTag' ) ) {
String tag = project.ormVersion.fullName String tag = project.ormVersion.fullName
// the release is tagged and the tag is pushed to github // the release is tagged and the tag is pushed to github
@ -293,21 +319,18 @@ task ciRelease() {
} }
logger.lifecycle( "Tagging '${tag}'..." ) logger.lifecycle( "Tagging '${tag}'..." )
executeGitCommand( 'tag', tag ) executeGitCommand( 'tag', tag )
executeGitCommand( 'push', gitRemote, tag ) executeGitCommand( 'push', project.gitRemote, tag )
logger.lifecycle( "Adding commit to update version to '${project.developmentVersion}'..." ) logger.lifecycle( "Adding commit to update version to '${project.developmentVersion}'..." )
project.ormVersionFile.text = "hibernateVersion=${project.developmentVersion}" project.ormVersionFile.text = "hibernateVersion=${project.developmentVersion}"
executeGitCommand( 'add', '.') executeGitCommand( 'add', '.')
executeGitCommand( 'commit', '-m', project.developmentVersion ) executeGitCommand( 'commit', '-m', project.developmentVersion )
executeGitCommand( 'push', gitRemote ) executeGitCommand( 'push', project.gitRemote )
} }
} }
} }
ciRelease.dependsOn addVersionCommit, release static String executeGitCommand(Object ... subcommand){
release.mustRunAfter addVersionCommit
static void executeGitCommand(Object ... subcommand){
List<Object> command = ['git'] List<Object> command = ['git']
Collections.addAll( command, subcommand ) Collections.addAll( command, subcommand )
def proc = command.execute() def proc = command.execute()
@ -317,6 +340,7 @@ static void executeGitCommand(Object ... subcommand){
if ( code != 0 ) { if ( code != 0 ) {
throw new GradleException( "An error occurred while executing " + command + "\n\nstdout:\n" + stdout + "\n\nstderr:\n" + stderr ) throw new GradleException( "An error occurred while executing " + command + "\n\nstdout:\n" + stdout + "\n\nstderr:\n" + stderr )
} }
return stdout
} }
static String inputStreamToString(InputStream inputStream) { static String inputStreamToString(InputStream inputStream) {