diff --git a/README.md b/README.md index 2335cd3..212b55a 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,7 @@ jobs: | Name | Required | Description | Default | |---------------------|----------|-------------|---------| +| `create_wait` | | Wait up to 'create_wait' retries (10 sec each) to create the Hetzner Cloud Server resource. | `360` (1 hour) | | `enable_ipv4` | | Attach an IPv4 on the public NIC (true/false). If false, no IPv4 address will be attached. Warning: The GitHub API requires IPv4. Disabling it will result in connection failures. | `true` | | `enable_ipv6` | | Attach an IPv6 on the public NIC (true/false). If false, no IPv6 address will be attached. | `true` | | `github_token` | ✓ (always) | Fine-grained GitHub Personal Access Token (PAT) with 'Read and write' access to 'Administration' assigned. | | diff --git a/action.sh b/action.sh index b8cf28e..4947fdb 100644 --- a/action.sh +++ b/action.sh @@ -197,12 +197,19 @@ if [[ "$MY_RUNNER_VERSION" != "latest" && "$MY_RUNNER_VERSION" != "skip" && ! "$ exit_with_failure "'$MY_RUNNER_VERSION' is not a valid GitHub Actions Runner version! Enter 'latest', 'skip' or the version without 'v'." fi -# Set maximal wait time (retries * 10 sec) for GitHub Actions Runner registration (default: 30 [5 min]) -# If MY_RUNNER_WAIT is set, use its value; otherwise, use "30". +# Set maximal wait time (retries * 10 sec) for GitHub Actions Runner registration (default: 60 [10 min]) +# If INPUT_RUNNER_WAIT is set, use its value; otherwise, use "60". MY_RUNNER_WAIT=${INPUT_RUNNER_WAIT:-"60"} # Check if MY_RUNNER_WAIT is an integer if [[ ! "$MY_RUNNER_WAIT" =~ ^[0-9]+$ ]]; then - exit_with_failure "The maximum wait time (reties) for GitHub Action Runner registration must be an integer!" + exit_with_failure "The maximum wait time (retries) for GitHub Action Runner registration must be an integer!" +fi + +# Set maximal wait time (retries * 10 sec) for Hetzner Server creation (default: 360 [1 hour]) +# If INPUT_CREATE_WAIT is set, use its value; otherwise, use "360". +MY_CREATE_WAIT=${INPUT_CREATE_WAIT:-360} +if [[ ! "$MY_CREATE_RETRIES" =~ ^[0-9]+$ ]]; then + exit_with_failure "The maximum wait time (retries) for Hetzner Server creation must be an integer!" fi # Set Hetzner Cloud Server ID @@ -363,8 +370,12 @@ fi # Send a POST request to the Hetzner Cloud API to create a server. # https://docs.hetzner.cloud/#servers-create-a-server -echo "Create server..." -if ! curl \ +MAX_RETRIES=$MY_CREATE_WAIT +RETRY_COUNT=0 +WAIT_SEC=10 +while [[ $RETRY_COUNT -lt $MAX_RETRIES ]]; do + echo "Create Server..." + if curl \ -X POST \ --fail-with-body \ -o "servers.json" \ @@ -372,9 +383,24 @@ if ! curl \ -H "Authorization: Bearer ${MY_HETZNER_TOKEN}" \ -d @create-server.json \ "https://api.hetzner.cloud/v1/servers"; then - cat "servers.json" - exit_with_failure "Failed to create Server in Hetzner Cloud!" -fi + echo "Server created successfully." + break + else + # Check if the error is related to resource unavailability + if grep -q -E "resource_unavailable|resource_limit_exceeded" "servers.json"; then + echo "Resource limitation detected." + # If error is not resource-related, don't retry + else + cat "servers.json" + exit_with_failure "Failed to create Server in Hetzner Cloud!" + fi + fi + + RETRY_COUNT=$((RETRY_COUNT + 1)) # Increment retry counter + + echo "Failed to create Server. Wait $WAIT_SEC seconds... (Attempt $RETRY_COUNT/$MAX_RETRIES)" + sleep "$WAIT_SEC" +done # Get the Hetzner Server ID from the JSON response (assuming valid JSON) MY_HETZNER_SERVER_ID=$(jq -er '.server.id' < "servers.json") @@ -393,7 +419,6 @@ echo "server_id=$MY_HETZNER_SERVER_ID" >> "$GITHUB_OUTPUT" # Wait for server MAX_RETRIES=$MY_SERVER_WAIT -WAIT_SEC=10 RETRY_COUNT=0 echo "Wait for server..." while [[ $RETRY_COUNT -lt $MAX_RETRIES ]]; do diff --git a/action.yml b/action.yml index fe2fdeb..effcc64 100644 --- a/action.yml +++ b/action.yml @@ -73,6 +73,11 @@ inputs: 'skip' will skip the installation. A working installation is expected in the 'runner_dir'. required: false default: 'latest' + create_wait: + description: >- + Wait up to 'create_wait' retries (10 sec each) to create the Hetzner Cloud Server resource (default: 360 = 1 hour). + required: false + default: '360' runner_wait: description: >- Wait up to 'runner_wait' retries (10 sec each) for runner registration (default: 10 minutes). @@ -142,3 +147,4 @@ runs: INPUT_SERVER_TYPE: ${{ inputs.server_type }} INPUT_SERVER_WAIT: ${{ inputs.server_wait }} INPUT_SSH_KEY: ${{ inputs.ssh_key }} + INPUT_CREATE_WAIT: ${{ inputs.create_wait }}