Skip to content

Commit 4d3661c

Browse files
committed
Add retry logic for server creation with configurable attempts and delay
Introduce create_retries and create_retry_delay inputs to control the number of retry attempts and delay between attempts when creating a Hetzner Cloud server. Update action.sh to implement the retry loop and improve error handling for transient failures. Document new options in README.md and action.yml to enhance reliability.
1 parent 273c9a2 commit 4d3661c

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ jobs:
160160
| `server_type` | | Name of the Server type this Server should be created with. | `cx22` (Intel x86, 2 vCPU, 4GB RAM, 40GB SSD) |
161161
| `server_wait` | | Wait up to `server_wait` retries (10 sec each) for the Hetzner Cloud Server to start. | `30` (5 min) |
162162
| `ssh_key` | | SSH key ID (integer) which should be injected into the Server at creation time. | `null` |
163+
| `create_retries` | | Number of retry attempts for runner creation if it fails. | `1` |
164+
| `create_retry_delay`| | Delay (in seconds) between retry attempts for runner creation. | `10` |
165+
166+
### Retry Logic
167+
168+
If runner creation fails due to a transient error, the action will retry up to `create_retries` times, waiting `create_retry_delay` seconds between attempts. All attempts and errors are logged.
163169

164170
## Outputs
165171

action.sh

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,17 @@ fi
202202
MY_RUNNER_WAIT=${INPUT_RUNNER_WAIT:-"60"}
203203
# Check if MY_RUNNER_WAIT is an integer
204204
if [[ ! "$MY_RUNNER_WAIT" =~ ^[0-9]+$ ]]; then
205-
exit_with_failure "The maximum wait time (reties) for GitHub Action Runner registration must be an integer!"
205+
exit_with_failure "The maximum wait time (retries) for GitHub Action Runner registration must be an integer!"
206+
fi
207+
208+
# Set create retry logic inputs
209+
MY_CREATE_RETRIES=${INPUT_CREATE_RETRIES:-1}
210+
MY_CREATE_RETRY_DELAY=${INPUT_CREATE_RETRY_DELAY:-10}
211+
if [[ ! "$MY_CREATE_RETRIES" =~ ^[0-9]+$ ]]; then
212+
exit_with_failure "The create_retries value must be an integer!"
213+
fi
214+
if [[ ! "$MY_CREATE_RETRY_DELAY" =~ ^[0-9]+$ ]]; then
215+
exit_with_failure "The create_retry_delay value must be an integer!"
206216
fi
207217

208218
# Set Hetzner Cloud Server ID
@@ -363,18 +373,31 @@ fi
363373

364374
# Send a POST request to the Hetzner Cloud API to create a server.
365375
# https://docs.hetzner.cloud/#servers-create-a-server
366-
echo "Create server..."
367-
if ! curl \
376+
MY_CREATE_ATTEMPT=1
377+
while [[ $MY_CREATE_ATTEMPT -le $MY_CREATE_RETRIES ]]; do
378+
echo "Create server (attempt $MY_CREATE_ATTEMPT of $MY_CREATE_RETRIES)..."
379+
if curl \
368380
-X POST \
369381
--fail-with-body \
370382
-o "servers.json" \
371383
-H "Content-Type: application/json" \
372384
-H "Authorization: Bearer ${MY_HETZNER_TOKEN}" \
373385
-d @create-server.json \
374386
"https://api.hetzner.cloud/v1/servers"; then
375-
cat "servers.json"
376-
exit_with_failure "Failed to create Server in Hetzner Cloud!"
377-
fi
387+
echo "Server created successfully."
388+
break
389+
else
390+
echo "Server creation failed (attempt $MY_CREATE_ATTEMPT)."
391+
if [[ $MY_CREATE_ATTEMPT -lt $MY_CREATE_RETRIES ]]; then
392+
echo "Retrying in $MY_CREATE_RETRY_DELAY seconds..."
393+
sleep "$MY_CREATE_RETRY_DELAY"
394+
else
395+
cat "servers.json"
396+
exit_with_failure "Failed to create Server in Hetzner Cloud after $MY_CREATE_RETRIES attempts!"
397+
fi
398+
fi
399+
MY_CREATE_ATTEMPT=$((MY_CREATE_ATTEMPT + 1))
400+
done
378401

379402
# Get the Hetzner Server ID from the JSON response (assuming valid JSON)
380403
MY_HETZNER_SERVER_ID=$(jq -er '.server.id' < "servers.json")

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ inputs:
102102
Specifies bash commands to run before the GitHub Actions Runner starts.
103103
It's useful for installing dependencies with apt-get, dnf, zypper etc.
104104
required: false
105+
create_retries:
106+
description: >-
107+
Number of retry attempts for runner creation if it fails.
108+
required: false
109+
default: '1'
110+
create_retry_delay:
111+
description: >-
112+
Delay (in seconds) between retry attempts for runner creation.
113+
required: false
114+
default: '10'
105115

106116
outputs:
107117
label:
@@ -142,3 +152,5 @@ runs:
142152
INPUT_SERVER_TYPE: ${{ inputs.server_type }}
143153
INPUT_SERVER_WAIT: ${{ inputs.server_wait }}
144154
INPUT_SSH_KEY: ${{ inputs.ssh_key }}
155+
INPUT_CREATE_RETRIES: ${{ inputs.create_retries }}
156+
INPUT_CREATE_RETRY_DELAY: ${{ inputs.create_retry_delay }}

0 commit comments

Comments
 (0)