| 1 | #!/usr/bin/env bash |
| 2 | # Finds g1t-runner for the g1t/deploy and g1t/detect actions and prints its |
| 3 | # path: G1T_RUNNER if it names one, else the one on PATH (g1t's machines |
| 4 | # have it at /usr/local/bin/g1t-runner), else the newest release from |
| 5 | # g1t.sh/downloads/runner, checked against that release's SHA256SUMS, as |
| 6 | # docs.g1t.sh/guides/self-hosted-runners/ downloads it. |
| 7 | # |
| 8 | # actions/deploy/runner.sh and actions/detect/runner.sh are the same file: |
| 9 | # each action is published as a repository of its own. |
| 10 | set -euo pipefail |
| 11 | |
| 12 | if [ -n "${G1T_RUNNER:-}" ] && [ -x "${G1T_RUNNER}" ]; then |
| 13 | echo "${G1T_RUNNER}" |
| 14 | exit 0 |
| 15 | fi |
| 16 | if found="$(command -v g1t-runner 2>/dev/null)"; then |
| 17 | echo "${found}" |
| 18 | exit 0 |
| 19 | fi |
| 20 | |
| 21 | case "$(uname -s)-$(uname -m)" in |
| 22 | Linux-x86_64 | Linux-amd64) platform=linux-x64 ;; |
| 23 | Linux-aarch64 | Linux-arm64) platform=linux-arm64 ;; |
| 24 | Darwin-arm64) platform=macos-arm64 ;; |
| 25 | Darwin-x86_64) platform=macos-x64 ;; |
| 26 | *) |
| 27 | echo "::error::g1t-runner is not on PATH, and g1t has no release of it for $(uname -s) $(uname -m)." >&2 |
| 28 | exit 1 |
| 29 | ;; |
| 30 | esac |
| 31 | |
| 32 | downloads="${G1T_RUNNER_DOWNLOADS:-https://g1t.sh/downloads/runner/latest}" |
| 33 | dir="${RUNNER_TEMP:-${TMPDIR:-/tmp}}/g1t-runner-${platform}" |
| 34 | file="g1t-runner-${platform}" |
| 35 | if [ ! -x "${dir}/g1t-runner" ]; then |
| 36 | mkdir -p "${dir}" |
| 37 | echo "Downloading ${file} from ${downloads}" >&2 |
| 38 | curl -fsSL --retry 3 -o "${dir}/${file}" "${downloads}/${file}" |
| 39 | curl -fsSL --retry 3 -o "${dir}/SHA256SUMS" "${downloads}/SHA256SUMS" |
| 40 | expected="$(awk -v name="${file}" '$2 == name { print $1 }' "${dir}/SHA256SUMS")" |
| 41 | if command -v sha256sum >/dev/null 2>&1; then |
| 42 | actual="$(sha256sum "${dir}/${file}" | awk '{ print $1 }')" |
| 43 | else |
| 44 | actual="$(shasum -a 256 "${dir}/${file}" | awk '{ print $1 }')" |
| 45 | fi |
| 46 | if [ -z "${expected}" ] || [ "${expected}" != "${actual}" ]; then |
| 47 | echo "::error::The downloaded ${file} does not match the release's SHA256SUMS." >&2 |
| 48 | rm -f "${dir}/${file}" |
| 49 | exit 1 |
| 50 | fi |
| 51 | chmod +x "${dir}/${file}" |
| 52 | mv "${dir}/${file}" "${dir}/g1t-runner" |
| 53 | fi |
| 54 | echo "${dir}/g1t-runner" |