From 872111f2a1f22f035a312e5bbe5fa01151011e90 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 9 Jun 2022 14:59:07 +0200 Subject: [PATCH] chore: retry git push sync During the release it can happen, that two pushes are executed fast one after another. This could lead to gh action jobs overlapping while trying to push to sourceforge leading to errors like "remote: error: cannot lock ref 'refs/heads/master': is at XXX but expected YYY". --- .ci/git-repo-sync.sh | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/.ci/git-repo-sync.sh b/.ci/git-repo-sync.sh index a32d6c9b03..af0635d9ba 100755 --- a/.ci/git-repo-sync.sh +++ b/.ci/git-repo-sync.sh @@ -27,7 +27,7 @@ function git_repo_sync() { pmd_ci_log_group_start "Git Sync" git remote add pmd-sf "${PMD_SF_USER}@git.code.sf.net:/p/pmd/code" if [ -n "${PMD_CI_BRANCH}" ]; then - git push pmd-sf "${PMD_CI_BRANCH}:${PMD_CI_BRANCH}" + retry 5 git push pmd-sf "${PMD_CI_BRANCH}:${PMD_CI_BRANCH}" pmd_ci_log_success "Successfully pushed ${PMD_CI_BRANCH} to sourceforge" elif [ -n "${PMD_CI_TAG}" ]; then git push pmd-sf tag "${PMD_CI_TAG}" @@ -39,6 +39,43 @@ function git_repo_sync() { pmd_ci_log_group_end } + +# +# From: https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746 +# +# Retry a command up to a specific number of times until it exits successfully, +# with exponential back off. +# +# $ retry 5 echo Hello +# Hello +# +# $ retry 5 false +# Retry 1/5 exited 1, retrying in 1 seconds... +# Retry 2/5 exited 1, retrying in 2 seconds... +# Retry 3/5 exited 1, retrying in 4 seconds... +# Retry 4/5 exited 1, retrying in 8 seconds... +# Retry 5/5 exited 1, no more retries left. +# +function retry { + local retries=$1 + shift + + local count=0 + until "$@"; do + exit=$? + wait=$((2 ** $count)) + count=$(($count + 1)) + if [ $count -lt $retries ]; then + echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." + sleep $wait + else + echo "Retry $count/$retries exited $exit, no more retries left." + return $exit + fi + done + return 0 +} + git_repo_sync exit 0