From d2b78ab6261e85f1a978ad1e15016117dd2b5dd2 Mon Sep 17 00:00:00 2001 From: Sean OMeara Date: Sat, 17 Dec 2022 09:28:06 +0100 Subject: [PATCH] puttin on the bits --- .github/workflows/build.yml | 30 +++++++++++++++ action.yml | 76 +++++++++++++++++++++++++++++++++++++ util/install.ps1 | 10 +++++ util/install.sh | 1 + util/post/action.yml | 42 ++++++++++++++++++++ util/post/main.js | 45 ++++++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 action.yml create mode 100755 util/install.ps1 create mode 100644 util/install.sh create mode 100644 util/post/action.yml create mode 100644 util/post/main.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..b197c4b --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +name: zerotier +on: [ push ] + +jobs: + build: + strategy: + matrix: + os: [ ubuntu-latest, macos-latest, windows-latest ] + # os: [ windows-latest ] + runs-on: ${{ matrix.os }} + steps: + - name: checkout + uses: actions/checkout@v3.2.0 + + - name: zerotier + uses: ./ + with: + network_id: ${{ secrets.ZEROTIER_NETWORK_ID }} + auth_token: ${{ secrets.ZEROTIER_CENTRAL_TOKEN }} + + - name: ping host + shell: bash + run: | + count=10 + while ! ping -c 1 ${{ secrets.ZEROTIER_HOST_IP }} ; do + echo "waiting..." ; + sleep 1 ; + let count=count-1 + done + echo "ping success" diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..ed99d1d --- /dev/null +++ b/action.yml @@ -0,0 +1,76 @@ +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: ./util/post + with: + 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" + curl -s -X POST -H "Authorization: token ${{ inputs.auth_token }}" -d '{"config":{"authorized":true}}' "${{ inputs.api_url }}/network/${{ inputs.network_id }}/member/${member_id}" + + 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 -s -X DELETE -H "Authorization: token ${{ inputs.auth_token }}" "${{ inputs.api_url }}/network/${{ inputs.network_id }}/member/${member_id}" diff --git a/util/install.ps1 b/util/install.ps1 new file mode 100755 index 0000000..3bc72e2 --- /dev/null +++ b/util/install.ps1 @@ -0,0 +1,10 @@ +$uri="https://download.zerotier.com/dist/ZeroTierOne.msi" +$out="ZeroTierOne.msi" +Invoke-WebRequest -uri $uri -OutFile $out + +$file = "ZeroTierOne.msi" +$log = "install.log" +$procMain = Start-Process "msiexec" "/i `"$file`" /qn /l*! `"$log`"" -NoNewWindow -PassThru +# $procLog = Start-Process "powershell" "Get-Content -Path `"$log`" -Wait" -NoNewWindow -PassThru +$procMain.WaitForExit() +# $procLog.Kill() diff --git a/util/install.sh b/util/install.sh new file mode 100644 index 0000000..cda2e98 --- /dev/null +++ b/util/install.sh @@ -0,0 +1 @@ +curl -s https://install.zerotier.com/ | sudo bash diff --git a/util/post/action.yml b/util/post/action.yml new file mode 100644 index 0000000..8e78a8c --- /dev/null +++ b/util/post/action.yml @@ -0,0 +1,42 @@ +# ==================================================================================================================== # +# Authors: # +# Patrick Lehmann # +# Unai Martinez-Corral # +# # +# ==================================================================================================================== # +# Copyright 2020-2022 The pyTooling Authors # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +# # +# SPDX-License-Identifier: Apache-2.0 # +# ==================================================================================================================== # +name: With post step + +description: 'Generic JS Action to execute a main command and set a command as a post step.' + +inputs: + main: + description: 'Main command/script.' + required: true + post: + description: 'Post command/script.' + required: true + key: + description: 'Name of the state variable used to detect the post step.' + required: false + default: POST + +runs: + using: 'node16' + main: 'main.js' + post: 'main.js' diff --git a/util/post/main.js b/util/post/main.js new file mode 100644 index 0000000..7109673 --- /dev/null +++ b/util/post/main.js @@ -0,0 +1,45 @@ +/* ================================================================================================================== * + * Authors: * + * Unai Martinez-Corral * + * * + * ================================================================================================================== * + * Copyright 2021-2022 Unai Martinez-Corral * + * Copyright 2022 Unai Martinez-Corral * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * + * SPDX-License-Identifier: Apache-2.0 * + * ================================================================================================================== * + * * + * Context: * + * * https://github.com/docker/login-action/issues/72 * + * * https://github.com/actions/runner/issues/1478 * + * ================================================================================================================== */ +const { spawn } = require("child_process"); +const fs = require('fs'); + +function run(cmd) { + const subprocess = spawn(cmd, { stdio: "inherit", shell: "bash" }); + subprocess.on("exit", (exitCode) => { + process.exitCode = exitCode; + }); +} + +const key = process.env.INPUT_KEY.toUpperCase(); + +if ( process.env[`STATE_${key}`] !== undefined ) { // Are we in the 'post' step? + run(process.env.INPUT_POST); +} else { // Otherwise, this is the main step + fs.appendFileSync(process.env.GITHUB_STATE, `${key}=true`); + run(process.env.INPUT_MAIN); +}