1
+ # Reusable Action for executing commands and retrying them if it fails
2
+ name : Command Retry Logic
3
+
4
+ inputs :
5
+ # (Optional) Command to run before the retry command. To be used for environment setup, etc
6
+ pre-command :
7
+ required : false
8
+ type : string
9
+ # (Optional) Number of retries to perform. Default is 3
10
+ max_retry :
11
+ required : false
12
+ type : number
13
+ default : 3
14
+ # (Required) Command to execute with the retry mechanism
15
+ command :
16
+ required : true
17
+ type : string
18
+ # (Required) Command to clean up resources before retrying the main command
19
+ cleanup :
20
+ required : false
21
+ type : string
22
+ # (Optional) Time to wait between each attempt in seconds. Default is 10 seconds
23
+ sleep_time :
24
+ required : false
25
+ type : number
26
+ default : 10
27
+ # (Optional) Follow-up command after the main command is finished.
28
+ post-command :
29
+ required : false
30
+ type : string
31
+
32
+ runs :
33
+ using : " composite"
34
+ steps :
35
+ - name : Run command
36
+ shell : bash
37
+ env :
38
+ PRE_COMMAND : ${{ inputs.pre-command }}
39
+ MAX_RETRY : ${{ inputs.max_retry }}
40
+ COMMAND : ${{ inputs.command }}
41
+ CLEANUP : ${{ inputs.cleanup }}
42
+ POST_COMMAND : ${{ inputs.post-command }}
43
+ SLEEP_TIME : ${{ inputs.sleep_time }}
44
+ run : |
45
+ echo "Starting the execute_and_retry action for command $COMMAND"
46
+ echo "Executing pre-command for the execute_and_retry action"
47
+ eval "$PRE_COMMAND"
48
+
49
+ retry_counter=0
50
+ while [ $retry_counter -lt $MAX_RETRY ]; do
51
+ echo "Attempt Number $retry_counter for execute_and_retry action"
52
+
53
+ attempt_failed=0
54
+ eval "$COMMAND" || attempt_failed=$?
55
+
56
+ if [ $attempt_failed -ne 0 ]; then
57
+ echo "Command failed for execute_and_retry action, executing cleanup command for another attempt"
58
+
59
+ eval "$CLEANUP" || true
60
+ retry_counter=$(($retry_counter+1))
61
+ sleep "$SLEEP_TIME"
62
+ else
63
+ echo "Command executed successfully for execute_and_retry"
64
+ break
65
+ fi
66
+ if [[ $retry_counter -ge $MAX_RETRY ]]; then
67
+ echo "Max retry reached, command failed to execute properly. Exiting action"
68
+ exit 1
69
+ fi
70
+ done
71
+
72
+ echo "Executing post-command for the execute_and_retry action"
73
+ eval "$POST_COMMAND"
74
+ echo "Exiting execute_and_retry action"
0 commit comments