Skip to content

Commit

Permalink
add short sleep time after executing commands
Browse files Browse the repository at this point in the history
I could not find out why this happens, but for example if we call `git checkout -b` immediately followed by `git rev-parse` from Gradle the output will be empty. I tried to reproduce it locally and with plain Git but couldn't. It only happens during the release job when it runs as GitHub action, so it is quite hard to debug. By my understanding the process should be finished if `waitFor()` returns (and the exit code is even 0), but somehow this does not work reliably. So for now I decided to go the quick and dirty way and simply add a short sleep time after executing shell commands.

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric committed Oct 28, 2021
1 parent 88d8fcd commit 18bdaae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
14 changes: 14 additions & 0 deletions build-steps/release/process-utils.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ext.executeCommand = { List<String> command ->
new StringBuilder().with { output ->
def error = new StringBuilder()
def exitCode = command.execute()
.with {
it.consumeProcessOutputStream(output)
it.consumeProcessErrorStream(error)
it
}.waitFor()
Thread.sleep(100) // For some reason a call right after `waitFor` might fail, so we wait a little bit :-(
assert exitCode == 0: "Command '$command' failed with exit code $exitCode: $error"
output.toString().trim()
}
}
19 changes: 3 additions & 16 deletions build-steps/release/publish.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ext.isReleaseVersion = !project.version.endsWith("-SNAPSHOT")

apply plugin: "io.github.gradle-nexus.publish-plugin"
apply from: scriptRelativePath(this, 'process-utils.gradle')

nexusPublishing {
packageGroup = 'com.tngtech'
Expand Down Expand Up @@ -124,27 +125,13 @@ def updateVersion = { Closure<String> calculateNewVersion ->
rootProject.allprojects { it.version = newVersion }
}

def executeCommand = { String command ->
new StringBuilder().with { output ->
def error = new StringBuilder()
def exitCode = command.split(' ').execute()
.with {
it.consumeProcessOutputStream(output)
it.consumeProcessErrorStream(error)
it
}.waitFor()
assert exitCode == 0: "Command '$command' failed with exit code $exitCode: $error"
output.toString().trim()
}
}

task prepareRelease() {
doFirst {
String releaseVersion = withCurrentVersion { int oldMajor, int oldMinor, int oldPatch, String oldSuffix -> "$oldMajor.$oldMinor.$oldPatch" }

def releaseBranch = "release-$releaseVersion"
executeCommand("git checkout -b $releaseBranch")
String currentBranch = executeCommand('git rev-parse --abbrev-ref HEAD')
executeCommand(['git', 'checkout', '-b', "$releaseBranch"])
String currentBranch = executeCommand(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
assert currentBranch == releaseBranch: "Mismatch: Should be on branch $releaseBranch but current branch is $currentBranch"

updateVersion { major, minor, patch, suffix -> releaseVersion }
Expand Down
4 changes: 3 additions & 1 deletion build-steps/release/vcs-utils.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apply from: scriptRelativePath(this, 'process-utils.gradle')

ext.gitCheckOut = { String url, File targetDir, String branch = null ->
def branchSuffix = branch ? " --branch ${branch}" : ''
targetDir.deleteDir()
targetDir.mkdirs()
['sh', '-c', "git clone ${url}${branchSuffix} ${targetDir.absolutePath}"].execute().waitFor()
executeCommand(['sh', '-c', "git clone ${url}${branchSuffix} ${targetDir.absolutePath}"])
}

0 comments on commit 18bdaae

Please sign in to comment.