Introduce retry option for git push.

This commit is contained in:
Greg L. Turnquist
2022-08-29 10:49:30 -05:00
parent 8acc42f658
commit b9f6fae8ef
3 changed files with 37 additions and 4 deletions

1
Jenkinsfile vendored
View File

@@ -40,6 +40,7 @@ pipeline {
PASSPHRASE = credentials('spring-gpg-passphrase')
KEYRING = credentials('spring-signing-secring.gpg')
SONATYPE = credentials('oss-login')
GPG_KEYNAME = credentials('spring-data-release-gpg-keyname')
}
steps {

View File

@@ -65,4 +65,35 @@ public abstract class TimedCommand implements ExecutionProcessor, CommandMarker
watch.stop();
System.out.println(String.format("Took: %.2f sec.", watch.getTotalTimeSeconds()));
}
/**
* Means to retry commands more than once to ensure they are completed. If any exception is thrown at any time, it
* will fall out and try again.
*
* @param action command(s) that need to be run
* @param maxTimes number of times to attempt
*/
protected void retry(Runnable action, int maxTimes) {
int maxAttempts = maxTimes;
Exception lastException = null;
while (maxAttempts > 0) {
try {
action.run();
return;
} catch (Exception e) {
lastException = e;
maxAttempts--;
}
}
if (lastException != null) {
throw new RuntimeException(lastException);
} else {
throw new RuntimeException("Unable to complete the action within " + maxTimes + " attempts!");
}
}
}

View File

@@ -61,10 +61,11 @@ public class GitHubCommands extends TimedCommand {
@CliCommand(value = "github push")
public void push(@CliOption(key = "", mandatory = true) TrainIteration iteration) {
git.push(iteration);
git.pushTags(iteration.getTrain());
createOrUpdateRelease(iteration);
retry(() -> {
git.push(iteration);
git.pushTags(iteration.getTrain());
createOrUpdateRelease(iteration);
}, 2);
}
@CliCommand(value = "github create release")