You've already forked github-action
mirror of
https://github.com/zerotier/github-action.git
synced 2026-05-22 16:27:40 -07:00
43a2884a2e
Probably resolve https://github.com/zerotier/github-action/issues/9
106 lines
3.8 KiB
YAML
106 lines
3.8 KiB
YAML
name: "ZeroTier"
|
|
description: "Connect your Github Action workflow to ZeroTier"
|
|
branding:
|
|
icon: 'globe'
|
|
color: 'gray-dark'
|
|
|
|
on:
|
|
workflow_call:
|
|
|
|
inputs:
|
|
network_id:
|
|
description: "The ZeroTier network ID to connect to"
|
|
required: true
|
|
auth_token:
|
|
description: "Your ZeroTier Central API Access Token"
|
|
required: true
|
|
api_url:
|
|
description: "ZeroTier Central API URL"
|
|
required: false
|
|
default: "https://my.zerotier.com/api/v1"
|
|
|
|
runs:
|
|
using: 'composite'
|
|
steps:
|
|
- name: zerotier
|
|
uses: zerotier/github-action/util/post@main
|
|
with:
|
|
main: |
|
|
set -euo pipefail
|
|
IFS=$'\n\t'
|
|
|
|
echo "⏁ Installing ZeroTier"
|
|
|
|
case $(uname -s) in
|
|
MINGW64_NT?*)
|
|
pwsh "${{ github.action_path }}/util/install.ps1"
|
|
ztcli="/c/Program Files (x86)/ZeroTier/One/zerotier-cli.bat"
|
|
member_id=$("${ztcli}" info | awk '{ print $3 }')
|
|
;;
|
|
*)
|
|
. ${{ github.action_path }}/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 ${{ inputs.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}}' \
|
|
"${{ inputs.api_url }}/network/${{ inputs.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: ${{ inputs.network_id }}"
|
|
case $(uname -s) in
|
|
MINGW64_NT?*)
|
|
"${ztcli}" join ${{ inputs.network_id }}
|
|
while ! "${ztcli}" listnetworks | grep ${{ inputs.network_id }} | grep OK ; do sleep 0.5 ; done
|
|
;;
|
|
*)
|
|
sudo zerotier-cli join ${{ inputs.network_id }}
|
|
while ! sudo zerotier-cli listnetworks | grep ${{ inputs.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 ${{ inputs.auth_token }}" "${{ inputs.api_url }}/network/${{ inputs.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
|