Skip to content

Commit

Permalink
feat: using the generic retry method
Browse files Browse the repository at this point in the history
  • Loading branch information
ttionya committed Aug 22, 2022
1 parent c2704f5 commit 4cb0947
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 50 deletions.
6 changes: 3 additions & 3 deletions actions_delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function delete_refs() {
local TARGET_REFS_FILE="/tmp/targetRefs.txt"

# list remote (origin) refs
ORIGIN_REFS="$(git_retry ls-remote --refs --heads --tags origin)"
ORIGIN_REFS="$(retry git ls-remote --refs --heads --tags origin)"
if [[ $? -ne 0 ]]; then
color red "failed to fetch remote (origin) refs"

Expand All @@ -33,7 +33,7 @@ function delete_refs() {
cat "${ORIGIN_REFS_FILE}"

# list remote (target) refs
TARGET_REFS="$(git_retry ls-remote --refs --heads --tags target)"
TARGET_REFS="$(retry git ls-remote --refs --heads --tags target)"
if [[ $? -ne 0 ]]; then
color red "failed to fetch remote (target) refs"

Expand All @@ -45,5 +45,5 @@ function delete_refs() {
cat "${TARGET_REFS_FILE}"

# diff
grep -Fvxf "${ORIGIN_REFS_FILE}" "${TARGET_REFS_FILE}" | xargs -I {} bash -c 'git_retry push target -f --delete "{}"'
grep -Fvxf "${ORIGIN_REFS_FILE}" "${TARGET_REFS_FILE}" | xargs -I {} bash -c 'retry git push target -f --delete "{}"'
}
53 changes: 11 additions & 42 deletions actions_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,40 @@ function color() {
export -f color

########################################
# Command git with retry.
# Command with retry.
# Arguments:
# git command options
# command options
# Returns:
# command return code
# Outputs:
# git command output
# command output
########################################
function git_retry() {
function retry() {
local RETRY_COUNT=3
local RETRY_DELAY=2
local COUNT=0
local RETURN_CODE=0

while [[ "${COUNT}" -lt "${RETRY_COUNT}" ]]; do
if [[ "${COUNT}" -gt 0 ]]; then
color yellow "retry after ${RETRY_DELAY} seconds" 1>&2
sleep "${RETRY_DELAY}"
fi

git $*
$*
RETURN_CODE=$?

if [[ "${RETURN_CODE}" -eq 0 ]]; then
break
fi

((COUNT++))
done

return "${RETURN_CODE}"
}

export -f git_retry

########################################
# Command ssh-keyscan with retry.
# Arguments:
# ssh-keyscan command options
# Returns:
# command return code
# Outputs:
# ssh-keyscan command output
########################################
function ssh_keyscan_retry() {
local RETRY_COUNT=5
local RETRY_DELAY=1
local COUNT=0
local RETURN_CODE=0
if [[ "${COUNT}" -eq "${RETRY_COUNT}" ]]; then
color yellow "retried ${RETRY_COUNT} times, no more retries left" 1>&2
else
color yellow "retry after ${RETRY_DELAY} seconds" 1>&2

while [[ "${COUNT}" -lt "${RETRY_COUNT}" ]]; do
if [[ "${COUNT}" -gt 0 ]]; then
color yellow "retry after ${RETRY_DELAY} second" 1>&2
sleep "${RETRY_DELAY}"
fi

ssh-keyscan $*
RETURN_CODE=$?

if [[ "${RETURN_CODE}" -eq 0 ]]; then
break
fi

((COUNT++))
done

return "${RETURN_CODE}"
}

export -f retry
8 changes: 4 additions & 4 deletions actions_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# None
########################################
function push_current_ref() {
git_retry push target "${GITHUB_REF}:${GITHUB_REF}" -f
retry git push target "${GITHUB_REF}:${GITHUB_REF}" -f
}

########################################
Expand All @@ -29,7 +29,7 @@ function push_refs() {
git branch --all

# fetch all branches and tags from origin
git_retry fetch origin
retry git fetch origin
if [[ $? -ne 0 ]]; then
color red "failed to fetch remote (origin) refs"

Expand All @@ -40,15 +40,15 @@ function push_refs() {
git branch --remotes --list "origin/*" | sed 's|[ \t]||g' | grep -v "HEAD" | xargs -I {} git checkout --track {}

color blue "push all branches"
git_retry push -u target -f --all
retry git push -u target -f --all
if [[ $? -ne 0 ]]; then
color red "failed to push branches to remote (target)"

exit 1
fi

color blue "push all tags"
git_retry push -u target -f --tags
retry git push -u target -f --tags
if [[ $? -ne 0 ]]; then
color red "failed to push tags to remote (target)"

Expand Down
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function configure_ssh() {
# ~ is /github/home/, not /root/
mkdir -p /root/.ssh
local KNOWN_HOSTS_FILE="/root/.ssh/known_hosts"
ssh_keyscan_retry "${TARGET_REPOSITORY_HOST}" > "${KNOWN_HOSTS_FILE}"
retry ssh-keyscan "${TARGET_REPOSITORY_HOST}" > "${KNOWN_HOSTS_FILE}"
chmod 644 "${KNOWN_HOSTS_FILE}"
if [[ ! -s "${KNOWN_HOSTS_FILE}" ]]; then
color red "ssh-keyscan failed"
Expand Down

0 comments on commit 4cb0947

Please sign in to comment.