Skip to content

Commit 54da470

Browse files
authored
Support multiple values where the HCloud API supports it (#27)
* Support multiple values where the HCloud API supports it Changed inputs: * `network` => `networks` * `ssh_key` => `ssh_keys` * `volume` => `volumes` * Keep the old input names
1 parent b05d7d5 commit 54da470

3 files changed

Lines changed: 54 additions & 39 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ jobs:
159159
| `location` | | Name of Location to create Server in. | `nbg1` (Nürnberg 1) |
160160
| `mode` | ✓ (always) | Choose either `create` to create a new GitHub Actions Runner or `delete` to delete a previously created one. | |
161161
| `name` | ✓ (mode `delete`, optional for mode `create`) | The name for the server and label for the GitHub Actions Runner (must be unique within the project and conform to hostname rules: `^[a-zA-Z0-9_-]{1,64}`). | `gh-runner-[RANDOM-INT]` |
162-
| `network` | | Network ID (integer) which should be attached to the Server private network interface at the creation time. | `null` |
162+
| `network` | | Comma separated Network IDs (integer) which should be attached to the Server private network interface at the creation time. | `null` |
163163
| `pre_runner_script` | | Specifies bash commands to run before the GitHub Actions Runner starts. It's useful for installing dependencies with apt-get, dnf, zypper etc. | |
164164
| `primary_ipv4` | | ID (integer) of the IPv4 Primary IP to use. If omitted and `enable_ipv4` is true, a new IPv4 Primary IP will automatically be created. | `null` |
165165
| `primary_ipv6` | | ID (integer) of the IPv6 Primary IP to use. If omitted and `enable_ipv6` is true, a new IPv6 Primary IP will automatically be created. | `null` |
@@ -169,8 +169,8 @@ jobs:
169169
| `server_id` | ✓ (mode `stop`) | ID (integer) of Hetzner Cloud Server to delete. | |
170170
| `server_type` | | Name of the Server type this Server should be created with. | `cx23` (Intel x86, 2 vCPU, 4GB RAM, 40GB SSD) |
171171
| `server_wait` | | Wait up to `server_wait` retries (10 sec each) for the Hetzner Cloud Server to start. | `30` (5 min) |
172-
| `ssh_key` | | SSH key ID (integer) which should be injected into the Server at creation time. | `null` |
173-
| `volume` | | Specify a Volume ID (integer) to attach and mount to the Server during creation. The volume will be automatically mounted at `/mnt/HC_Volume_[VOLUME-ID]`. The volume must be in the same location as the Server. More details in [Volumes section](#Volumes). | `null` |
172+
| `ssh_key` | | Comma separated SSH key IDs (integer) which should be injected into the Server at creation time. | `null` |
173+
| `volume` | | Comma separated Volume IDs (integer) to attach and mount to the Server during creation. Volumes will be automatically mounted at `/mnt/HC_Volume_[VOLUME-ID]`. Volumes must be in the same location as the Server. More details in [Volumes section](#Volumes). | `null` |
174174

175175
## Outputs
176176

action.sh

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ function exit_with_failure() {
2323
exit 1
2424
}
2525

26+
# Function to check all values of a comma separated list are integers
27+
function check_all_integers() {
28+
IFS=',' read -ra _values <<< "$1"
29+
for value in "${_values[@]}"; do
30+
if [[ ! "$value" =~ ^[0-9]+$ ]]; then
31+
echo "$value"
32+
return 1
33+
fi
34+
done
35+
return 0
36+
}
37+
2638
# Define required commands
2739
MY_COMMANDS=(
2840
base64
@@ -150,11 +162,12 @@ if [[ "$MY_NAME" == "hetzner" ]]; then
150162
fi
151163

152164
# Set the network for the instance (default: null)
153-
# If INPUT_NETWORK is set, use its value; otherwise, use "null".
154-
MY_NETWORK=${INPUT_NETWORK:-"null"}
155-
# Check if MY_NETWORK is an integer
156-
if [[ "$MY_NETWORK" != "null" && ! "$MY_NETWORK" =~ ^[0-9]+$ ]]; then
157-
exit_with_failure "The network ID must be 'null' or an integer!"
165+
# If INPUT_NETWORKS is set, use its value; otherwise, use "null".
166+
MY_NETWORKS=${INPUT_NETWORKS:-"null"}
167+
if [[ "$MY_NETWORKS" != "null" ]]; then
168+
invalid_value=$(check_all_integers "$MY_NETWORKS") || {
169+
exit_with_failure "Invalid network ID: $invalid_value (must be 'null' or an integer)"
170+
}
158171
fi
159172

160173
# Set bash commands to run before the runner starts.
@@ -219,19 +232,21 @@ if [[ ! "$MY_SERVER_WAIT" =~ ^[0-9]+$ ]]; then
219232
fi
220233

221234
# Set the SSH key to use for the instance (default: null)
222-
# If INPUT_SSH_KEY is set, use its value; otherwise, use "null".
223-
MY_SSH_KEY=${INPUT_SSH_KEY:-"null"}
224-
# Check if MY_SSH_KEY is an integer
225-
if [[ "$MY_SSH_KEY" != "null" && ! "$MY_SSH_KEY" =~ ^[0-9]+$ ]]; then
226-
exit_with_failure "The SSH key ID must be 'null' or an integer!"
235+
# If INPUT_SSH_KEYS is set, use its value; otherwise, use "null".
236+
MY_SSH_KEYS=${INPUT_SSH_KEYS:-"null"}
237+
if [[ "$MY_SSH_KEYS" != "null" ]]; then
238+
invalid_value=$(check_all_integers "$MY_SSH_KEYS") || {
239+
exit_with_failure "Invalid SSH key ID: $invalid_value (must be 'null' or an integer)"
240+
}
227241
fi
228242

229243
# Set the volume ID which should be attached to the instance at the creation time (default: null)
230-
# If INPUT_VOLUME is set, use its value; otherwise, use "null".
231-
MY_VOLUME=${INPUT_VOLUME:-"null"}
232-
# Check if MY_VOLUME is an integer
233-
if [[ "$MY_VOLUME" != "null" && ! "$MY_VOLUME" =~ ^[0-9]+$ ]]; then
234-
exit_with_failure "The volume ID must be 'null' or an integer!"
244+
# If INPUT_VOLUMES is set, use its value; otherwise, use "null".
245+
MY_VOLUMES=${INPUT_VOLUMES:-"null"}
246+
if [[ "$MY_VOLUMES" != "null" ]]; then
247+
invalid_value=$(check_all_integers "$MY_VOLUMES") || {
248+
exit_with_failure "Invalid volume ID: $invalid_value (must be 'null' or an integer)"
249+
}
235250
fi
236251

237252
#
@@ -292,7 +307,7 @@ if [[ "$MY_MODE" == "delete" ]]; then
292307
echo "GitHub Actions Runner deleted successfully."
293308
echo
294309
echo "The Hetzner Cloud Server and its associated GitHub Actions Runner have been deleted successfully."
295-
# Add GitHub Action job summary
310+
# Add GitHub Action job summary
296311
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#adding-a-job-summary
297312
echo "The Hetzner Cloud Server and its associated GitHub Actions Runner have been deleted successfully 🗑️" >> "$GITHUB_STEP_SUMMARY"
298313
exit 0
@@ -377,23 +392,23 @@ if [[ "$MY_PRIMARY_IPV6" != "null" ]]; then
377392
jq ".public_net.ipv6 = $MY_PRIMARY_IPV6" < create-server-ipv6.json > create-server.json && \
378393
echo "Primary IPv6 ID added to create-server.json."
379394
fi
380-
# Add network configuration to the create-server.json file if MY_NETWORK is not "null".
381-
if [[ "$MY_NETWORK" != "null" ]]; then
395+
# Add network configuration to the create-server.json file if MY_NETWORKS is not "null".
396+
if [[ "$MY_NETWORKS" != "null" ]]; then
382397
cp create-server.json create-server-network.json && \
383-
jq ".networks += [$MY_NETWORK]" < create-server-network.json > create-server.json && \
384-
echo "Network added to create-server.json."
398+
jq ".networks += [$MY_NETWORKS]" < create-server-network.json > create-server.json && \
399+
echo "Networks added to create-server.json."
385400
fi
386-
# Add SSH key configuration to the create-server.json file if MY_SSH_KEY is not "null".
387-
if [[ "$MY_SSH_KEY" != "null" ]]; then
401+
# Add SSH key configuration to the create-server.json file if MY_SSH_KEYS is not "null".
402+
if [[ "$MY_SSH_KEYS" != "null" ]]; then
388403
cp create-server.json create-server-ssh.json && \
389-
jq ".ssh_keys += [$MY_SSH_KEY]" < create-server-ssh.json > create-server.json && \
390-
echo "SSH key added to create-server.json."
404+
jq ".ssh_keys += [$MY_SSH_KEYS]" < create-server-ssh.json > create-server.json && \
405+
echo "SSH keys added to create-server.json."
391406
fi
392-
# Add volume configuration to the create-server.json file if MY_VOLUME is not "null".
393-
if [[ "$MY_VOLUME" != "null" ]]; then
407+
# Add volume configuration to the create-server.json file if MY_VOLUMES is not "null".
408+
if [[ "$MY_VOLUMES" != "null" ]]; then
394409
cp create-server.json create-server-volume.json && \
395-
jq ".volumes += [$MY_VOLUME]" < create-server-volume.json > create-server.json && \
396-
echo "Volume added to create-server.json."
410+
jq ".volumes += [$MY_VOLUMES]" < create-server-volume.json > create-server.json && \
411+
echo "Volumes added to create-server.json."
397412
fi
398413

399414
# Send a POST request to the Hetzner Cloud API to create a server.
@@ -508,9 +523,9 @@ if [[ ! "$MY_GITHUB_RUNNER_ID" =~ ^[0-9]+$ ]]; then
508523
fi
509524

510525
echo
511-
echo "The Hetzner Cloud Server and its associated GitHub Actions Runner are ready for use."
526+
echo "The Hetzner Cloud Server and its associated GitHub Actions Runner are ready for use."
512527
echo "Runner: https://github.com/${MY_GITHUB_REPOSITORY}/settings/actions/runners/${MY_GITHUB_RUNNER_ID}"
513-
# Add GitHub Action job summary
528+
# Add GitHub Action job summary
514529
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#adding-a-job-summary
515530
echo "The Hetzner Cloud Server and its associated [GitHub Actions Runner](https://github.com/${MY_GITHUB_REPOSITORY}/settings/actions/runners/${MY_GITHUB_RUNNER_ID}) are ready for use 🚀" >> "$GITHUB_STEP_SUMMARY"
516531
exit 0

action.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ inputs:
5656
required: false
5757
network:
5858
description: >-
59-
Network ID (integer) which should be attached to the Server private network interface at the creation time.
59+
Comma separated Network IDs (integer) which should be attached to the Server private network interface at the creation time.
6060
required: false
6161
default: 'null'
6262
pre_runner_script:
@@ -109,12 +109,12 @@ inputs:
109109
default: '30'
110110
ssh_key:
111111
description: >-
112-
SSH key ID (integer) or name which should be injected into the Server at creation time.
112+
Comma separated SSH key IDs (integer) which should be injected into the Server at creation time.
113113
required: false
114114
default: 'null'
115115
volume:
116116
description: >-
117-
Volume ID (integer) which should be attached to the Server at the creation time.
117+
Comma separated Volume IDs (integer) which should be attached to the Server at the creation time.
118118
required: false
119119
default: 'null'
120120

@@ -148,7 +148,7 @@ runs:
148148
INPUT_LOCATION: ${{ inputs.location }}
149149
INPUT_MODE: ${{ inputs.mode }}
150150
INPUT_NAME: ${{ inputs.name }}
151-
INPUT_NETWORK: ${{ inputs.network }}
151+
INPUT_NETWORKS: ${{ inputs.network }}
152152
INPUT_PRE_RUNNER_SCRIPT: ${{ inputs.pre_runner_script }}
153153
INPUT_PRIMARY_IPV4: ${{ inputs.primary_ipv4 }}
154154
INPUT_PRIMARY_IPV6: ${{ inputs.primary_ipv6 }}
@@ -158,5 +158,5 @@ runs:
158158
INPUT_SERVER_ID: ${{ inputs.server_id }}
159159
INPUT_SERVER_TYPE: ${{ inputs.server_type }}
160160
INPUT_SERVER_WAIT: ${{ inputs.server_wait }}
161-
INPUT_SSH_KEY: ${{ inputs.ssh_key }}
162-
INPUT_VOLUME: ${{ inputs.volume }}
161+
INPUT_SSH_KEYS: ${{ inputs.ssh_key }}
162+
INPUT_VOLUMES: ${{ inputs.volume }}

0 commit comments

Comments
 (0)