Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit 0f77c15

Browse files
committed
Append retry function
1 parent 90091d7 commit 0f77c15

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

action.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ runs:
4040
- shell: bash
4141
if: ${{ !env.ACT }}
4242
run: |
43-
set -xe
43+
set -x
44+
45+
source ./try.sh
46+
47+
try_till_success curl -s 'https://github.com' > /dev/null
4448
4549
export __SHA=${{ inputs.sha || github.sha }}
4650
if [ ! -z "${{ inputs.branch_name }}" ]; then
@@ -49,7 +53,7 @@ runs:
4953
__BRANCH=${__BRANCH#refs/heads/}
5054
5155
# Override sha value if branch_name is presented.
52-
__SHA=$(/usr/bin/git ls-remote https://${{ inputs.github_actor }}:${{ inputs.github_token }}@github.com/${{ inputs.github_repository }}.git | grep -E "\srefs/heads/${__BRANCH}$" | cut -f 1 )
56+
__SHA=$(try_till_success /usr/bin/git ls-remote https://${{ inputs.github_actor }}:${{ inputs.github_token }}@github.com/${{ inputs.github_repository }}.git | grep -E "\srefs/heads/${__BRANCH}$" | cut -f 1 )
5357
fi
5458
5559
if ! ${{ inputs.fetch_lfs }}; then
@@ -61,11 +65,12 @@ runs:
6165
DEPTH=--depth=${{ inputs.depth }}
6266
fi
6367
64-
/usr/bin/git clone \
65-
$DEPTH \
66-
--reference-if-able=${{ inputs.reference_dir }} \
67-
https://${{ inputs.github_actor }}:${{ inputs.github_token }}@github.com/${{ inputs.github_repository }}.git \
68-
${{ inputs.checkout_dir }}
68+
try_till_success \
69+
/usr/bin/git clone \
70+
$DEPTH \
71+
--reference-if-able=${{ inputs.reference_dir }} \
72+
https://${{ inputs.github_actor }}:${{ inputs.github_token }}@github.com/${{ inputs.github_repository }}.git \
73+
${{ inputs.checkout_dir }}
6974
7075
if [ $(git config --global --get-all safe.directory | grep -c "$(pwd)$") -eq 0 ]; then
7176
/usr/bin/git config --global --add safe.directory $(pwd)
@@ -75,7 +80,7 @@ runs:
7580
if [ ! -z "$__BRANCH" ]; then
7681
/usr/bin/git checkout -B $__BRANCH origin/$__BRANCH
7782
else
78-
/usr/bin/git fetch $DEPTH origin $__SHA
83+
try_till_success /usr/bin/git fetch $DEPTH origin $__SHA
7984
/usr/bin/git reset --hard FETCH_HEAD
8085
fi
8186

try.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# https://news.ycombinator.com/item?id=30713151
2+
# Retries a command a with backoff.
3+
#
4+
# The retry count is given by ATTEMPTS (default 100), the
5+
# initial backoff timeout is given by TIMEOUT in seconds
6+
# (default 5.)
7+
#
8+
# Successive backoffs increase the timeout by ~33%.
9+
#
10+
# Beware of set -e killing your whole script!
11+
function try_till_success {
12+
local max_attempts=${ATTEMPTS-2}
13+
local timeout=${TIMEOUT-5}
14+
local attempt=0
15+
local exitCode=0
16+
17+
while [[ $attempt < $max_attempts ]]
18+
do
19+
"$@"
20+
exitCode=$?
21+
22+
if [[ $exitCode == 0 ]]
23+
then
24+
break
25+
fi
26+
27+
echo "Failure! Retrying in $timeout.." 1>&2
28+
sleep "$timeout"
29+
attempt=$(( attempt + 1 ))
30+
timeout=$(( timeout * 40 / 30 ))
31+
done
32+
33+
if [[ $exitCode != 0 ]]
34+
then
35+
echo "You've failed me for the last time! ($*)" 1>&2
36+
fi
37+
38+
return $exitCode
39+
}

0 commit comments

Comments
 (0)