forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
116 lines (106 loc) · 3.64 KB
/
Copy pathaction.yml
File metadata and controls
116 lines (106 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
name: 'Retry with Backoff'
description: 'Retry a command with exponential/linear/fixed backoff and jitter. Pure Python, no dependencies.'
inputs:
run:
description: 'Command to execute'
required: true
max_attempts:
description: 'Maximum retry attempts'
required: false
default: '3'
backoff:
description: 'Backoff strategy: exponential, linear, or fixed'
required: false
default: 'exponential'
backoff_base_seconds:
description: 'Base wait time in seconds'
required: false
default: '10'
retry_on_exit_code:
description: 'Comma-separated exit codes to retry on. Empty = any non-zero'
required: false
default: ''
shell_override:
description: 'Shell to use for the command (default: bash)'
required: false
default: 'bash'
outputs:
attempts_count:
description: 'Number of attempts made'
value: ${{ steps.retry.outputs.attempts_count }}
final_exit_code:
description: 'Exit code of the final attempt'
value: ${{ steps.retry.outputs.final_exit_code }}
runs:
using: 'composite'
steps:
- name: Retry execution
id: retry
shell: bash
run: |
set +e
MAX_ATTEMPTS="${{ inputs.max_attempts }}"
BACKOFF="${{ inputs.backoff }}"
BASE_SECONDS="${{ inputs.backoff_base_seconds }}"
RETRY_CODES="${{ inputs.retry_on_exit_code }}"
SHELL_CMD="${{ inputs.shell_override }}"
LOG_FILE="${GITHUB_WORKSPACE:-$PWD}/.retry-output.log"
: > "$LOG_FILE"
ATTEMPT=0
FINAL_CODE=0
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
ATTEMPT=$((ATTEMPT + 1))
echo "::group::Attempt $ATTEMPT/$MAX_ATTEMPTS"
# Run the command and preserve its output for downstream classification.
echo "Command: ${{ inputs.run }}" | tee -a "$LOG_FILE"
$SHELL_CMD -c "${{ inputs.run }}" 2>&1 | tee -a "$LOG_FILE"
EXIT_CODE=${PIPESTATUS[0]}
FINAL_CODE=$EXIT_CODE
echo "Exit code: $EXIT_CODE" | tee -a "$LOG_FILE"
echo "::endgroup::"
if [ $EXIT_CODE -eq 0 ]; then
echo "✅ Success on attempt $ATTEMPT"
break
fi
# Check if we should retry this exit code
if [ -n "$RETRY_CODES" ]; then
SHOULD_RETRY=false
IFS=',' read -ra CODES <<< "$RETRY_CODES"
for code in "${CODES[@]}"; do
code=$(echo "$code" | tr -d ' ')
if [ "$EXIT_CODE" -eq "$code" ]; then
SHOULD_RETRY=true
break
fi
done
if [ "$SHOULD_RETRY" = "false" ]; then
echo "❌ Exit code $EXIT_CODE not in retry list ($RETRY_CODES), stopping"
break
fi
fi
if [ $ATTEMPT -ge $MAX_ATTEMPTS ]; then
echo "❌ Max attempts reached"
break
fi
# Calculate wait time
WAIT=$(python3 -c "
import random
base = $BASE_SECONDS
attempt = $ATTEMPT
strategy = '$BACKOFF'
if strategy == 'exponential':
wait = (base ** attempt) * (1 + random.uniform(-0.2, 0.2))
elif strategy == 'linear':
wait = base * attempt * (1 + random.uniform(-0.2, 0.2))
else: # fixed
wait = base * (1 + random.uniform(-0.2, 0.2))
print(f'{wait:.1f}')
")
echo "⏳ Waiting ${WAIT}s before retry (strategy: $BACKOFF)..."
sleep $WAIT
done
echo "attempts_count=$ATTEMPT" >> "$GITHUB_OUTPUT"
echo "final_exit_code=$FINAL_CODE" >> "$GITHUB_OUTPUT"
if [ $FINAL_CODE -ne 0 ]; then
exit $FINAL_CODE
fi