diff --git a/action.yml b/action.yml index 8b9571a..bd606a6 100644 --- a/action.yml +++ b/action.yml @@ -29,5 +29,79 @@ runs: AUTH_TOKEN: ${{ inputs.auth_token }} NETWORK_ID: ${{ inputs.network_id }} with: - main: ./main.sh - post: ./post.sh + main: | + set -euo pipefail + IFS=$'\n\t' + + echo "⏁ Installing ZeroTier" + + case $(uname -s) in + MINGW64_NT?*) + pwsh "./util/install.ps1" + ztcli="/c/Program Files (x86)/ZeroTier/One/zerotier-cli.bat" + member_id=$("${ztcli}" info | awk '{ print $3 }') + ;; + *) + . ./util/install.sh &>/dev/null + member_id=$(sudo zerotier-cli info | awk '{ print $3 }') + ;; + esac + + echo "⏁ Authorizing Runner to ZeroTier network" + MAX_RETRIES=10 + RETRY_COUNT=0 + + while ! curl -s -X POST \ + -H "Authorization: token $AUTH_TOKEN" \ + -H "Content-Type: application/json" \ + -d '{"name":"Zerotier GitHub Member '"${GITHUB_SHA::7}"'", "description": "Member created by '"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"'", "config":{"authorized":true}}' \ + "$API_URL/network/$NETWORK_ID/member/${member_id}" | grep '"authorized":true'; do + RETRY_COUNT=$((RETRY_COUNT + 1)) + + if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then + echo "Reached maximum number of retries ($MAX_RETRIES). Exiting..." + exit 1 + fi + + echo "Authorization failed. Retrying in 2 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)" + sleep 2 + done + + echo "Member authorized successfully." + echo "⏁ Joining ZeroTier Network ID: $NETWORK_ID" + case $(uname -s) in + MINGW64_NT?*) + "${ztcli}" join $NETWORK_ID + while ! "${ztcli}" listnetworks | grep $NETWORK_ID | grep OK; do sleep 0.5; done + ;; + *) + sudo zerotier-cli join $NETWORK_ID + while ! sudo zerotier-cli listnetworks | grep $NETWORK_ID | grep OK; do sleep 0.5; done + ;; + esac + post: | + set -euo pipefail + IFS=$'\n\t' + + case $(uname -s) in + MINGW64_NT?*) + ztcli="/c/Program Files (x86)/ZeroTier/One/zerotier-cli.bat" + member_id=$("${ztcli}" info | awk '{ print $3 }') + ;; + *) + member_id=$(sudo zerotier-cli info | awk '{ print $3 }') + ;; + esac + + echo "⏁ Removing Runner from ZeroTier network" + curl -i -s -X DELETE -H "Authorization: token $AUTH_TOKEN" "$API_URL/network/$NETWORK_ID/member/${member_id}" >/tmp/api_delete_output.txt + STATUS_CODE=$(cat /tmp/api_delete_output.txt | grep 'HTTP/' | awk '{print $2}') + + if [[ $STATUS_CODE -ge 400 && $STATUS_CODE -le 599 ]]; then + echo "ERROR: Status code: $STATUS_CODE" + echo -e "Complete server response:\n$(cat /tmp/api_delete_output.txt)\n" + exit $STATUS_CODE + else + echo "Success! Status code: $STATUS_CODE" + exit 0 + fi diff --git a/main.sh b/main.sh deleted file mode 100755 index 16a9d80..0000000 --- a/main.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -set -euo pipefail -IFS=$'\n\t' - -echo "⏁ Installing ZeroTier" - -case $(uname -s) in -MINGW64_NT?*) - pwsh "./util/install.ps1" - ztcli="/c/Program Files (x86)/ZeroTier/One/zerotier-cli.bat" - member_id=$("${ztcli}" info | awk '{ print $3 }') - ;; -*) - . ./util/install.sh &>/dev/null - member_id=$(sudo zerotier-cli info | awk '{ print $3 }') - ;; -esac - -echo "⏁ Authorizing Runner to ZeroTier network" -MAX_RETRIES=10 -RETRY_COUNT=0 - -while ! curl -s -X POST \ - -H "Authorization: token $AUTH_TOKEN" \ - -H "Content-Type: application/json" \ - -d '{"name":"Zerotier GitHub Member '"${GITHUB_SHA::7}"'", "description": "Member created by '"${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"'", "config":{"authorized":true}}' \ - "$API_URL/network/$NETWORK_ID/member/${member_id}" | grep '"authorized":true'; do - RETRY_COUNT=$((RETRY_COUNT + 1)) - - if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then - echo "Reached maximum number of retries ($MAX_RETRIES). Exiting..." - exit 1 - fi - - echo "Authorization failed. Retrying in 2 seconds... (Attempt $RETRY_COUNT of $MAX_RETRIES)" - sleep 2 -done - -echo "Member authorized successfully." -echo "⏁ Joining ZeroTier Network ID: $NETWORK_ID" -case $(uname -s) in -MINGW64_NT?*) - "${ztcli}" join $NETWORK_ID - while ! "${ztcli}" listnetworks | grep $NETWORK_ID | grep OK; do sleep 0.5; done - ;; -*) - sudo zerotier-cli join $NETWORK_ID - while ! sudo zerotier-cli listnetworks | grep $NETWORK_ID | grep OK; do sleep 0.5; done - ;; -esac diff --git a/post.sh b/post.sh deleted file mode 100755 index 339ca05..0000000 --- a/post.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -set -euo pipefail -IFS=$'\n\t' - -case $(uname -s) in -MINGW64_NT?*) - ztcli="/c/Program Files (x86)/ZeroTier/One/zerotier-cli.bat" - member_id=$("${ztcli}" info | awk '{ print $3 }') - ;; -*) - member_id=$(sudo zerotier-cli info | awk '{ print $3 }') - ;; -esac - -echo "⏁ Removing Runner from ZeroTier network" -curl -i -s -X DELETE -H "Authorization: token $AUTH_TOKEN" "$API_URL/network/$NETWORK_ID/member/${member_id}" >/tmp/api_delete_output.txt -STATUS_CODE=$(cat /tmp/api_delete_output.txt | grep 'HTTP/' | awk '{print $2}') - -if [[ $STATUS_CODE -ge 400 && $STATUS_CODE -le 599 ]]; then - echo "ERROR: Status code: $STATUS_CODE" - echo -e "Complete server response:\n$(cat /tmp/api_delete_output.txt)\n" - exit $STATUS_CODE -else - echo "Success! Status code: $STATUS_CODE" - exit 0 -fi