In host mode the runner runs actions/checkout and friends with the host's node, and stpbuild had none: salary-insights' first run there failed with 'Cannot find: node in PATH'. RUNNER_NODE_VERSION in a host file installs that release from nodejs.org (sha256 from SHASUMS256.txt) as /usr/local/bin/node. stpbuild pins Node 24, the actions' runtime; Renovate bumps it within the major. Co-Authored-By: Claude Opus 5.5 <[email protected]>
150 lines
6.6 KiB
Bash
Executable File
150 lines
6.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Installs or upgrades gitea-runner on this machine as the systemd service
|
|
# `gitea-runner`, configured from hosts/<host>.env. Safe to re-run: it
|
|
# downloads only when VERSION changes, re-applies the host's name-independent
|
|
# settings (labels, capacity), and registers only when there is no .runner.
|
|
#
|
|
# sudo GITEA_RUNNER_REGISTRATION_TOKEN=... ./install.sh <host> first install
|
|
# sudo ./install.sh <host> --from <dir> adopt <dir>/.runner (no re-register)
|
|
# sudo ./install.sh <host> upgrade / re-apply after a git pull
|
|
#
|
|
# Mint a registration token in Site Administration -> Actions -> Runners
|
|
# (instance-wide), or on stpgit: docker exec gitea gitea actions grt
|
|
set -euo pipefail
|
|
|
|
# The pinned release. Renovate bumps this line (renovate.json5).
|
|
VERSION="6.0.1"
|
|
INSTANCE="http://git.adam-french.co.uk:3000"
|
|
BIN=/usr/local/bin/gitea-runner
|
|
STATE=/var/lib/gitea-runner
|
|
UNIT=/etc/systemd/system/gitea-runner.service
|
|
|
|
die() { echo "install.sh: $*" >&2; exit 1; }
|
|
|
|
cd "$(dirname "$0")"
|
|
[ "$(id -u)" -eq 0 ] || die "run as root (sudo)"
|
|
host=${1:-}
|
|
[ -n "$host" ] || die "usage: install.sh <host> [--from <dir with .runner>]"
|
|
shift
|
|
from=""
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--from) from=${2:-}; [ -n "$from" ] || die "--from needs a directory"; shift 2 ;;
|
|
*) die "unknown argument: $1" ;;
|
|
esac
|
|
done
|
|
|
|
[ -f "hosts/$host.env" ] || die "no hosts/$host.env (copy one of hosts/*.env)"
|
|
# shellcheck source=/dev/null
|
|
. "hosts/$host.env"
|
|
: "${RUNNER_NAME:?hosts/$host.env must set RUNNER_NAME}"
|
|
: "${RUNNER_USER:?hosts/$host.env must set RUNNER_USER}"
|
|
: "${RUNNER_LABELS:?hosts/$host.env must set RUNNER_LABELS}"
|
|
: "${RUNNER_CAPACITY:=1}"
|
|
id "$RUNNER_USER" >/dev/null 2>&1 || die "user $RUNNER_USER does not exist"
|
|
|
|
as_user() { runuser -u "$RUNNER_USER" -- "$@"; }
|
|
# Empty for a missing file (not an error under set -e / pipefail).
|
|
sum() { { sha256sum "$1" 2>/dev/null || true; } | cut -d' ' -f1; }
|
|
changed=0
|
|
|
|
# --- binary ------------------------------------------------------------------
|
|
case "$(uname -m)" in
|
|
x86_64) asset="gitea-runner-${VERSION}-linux-amd64" ;;
|
|
aarch64) asset="gitea-runner-${VERSION}-linux-arm64" ;;
|
|
armv7l) asset="gitea-runner-${VERSION}-linux-arm-7" ;;
|
|
*) die "unsupported architecture $(uname -m)" ;;
|
|
esac
|
|
current=$("$BIN" --version 2>/dev/null | awk '{print $NF}' || true)
|
|
if [ "$current" != "v${VERSION}" ]; then
|
|
echo "Installing gitea-runner v${VERSION} (was ${current:-none})"
|
|
tmp=$(mktemp -d)
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
base="https://dl.gitea.com/gitea-runner/${VERSION}"
|
|
curl -fsSL "$base/$asset" -o "$tmp/$asset"
|
|
curl -fsSL "$base/$asset.sha256" -o "$tmp/$asset.sha256"
|
|
(cd "$tmp" && sha256sum -c --quiet "$asset.sha256")
|
|
install -m 0755 "$tmp/$asset" "$BIN"
|
|
changed=1
|
|
fi
|
|
|
|
# --- node (host-mode JavaScript actions) -------------------------------------
|
|
# In host mode the runner runs actions such as actions/checkout with the
|
|
# host's `node`. Only `node` goes on PATH: the job's own Node comes from
|
|
# actions/setup-node.
|
|
if [ -n "${RUNNER_NODE_VERSION:-}" ]; then
|
|
nv="v${RUNNER_NODE_VERSION}"
|
|
if [ "$(/usr/local/bin/node --version 2>/dev/null || true)" != "$nv" ]; then
|
|
case "$(uname -m)" in
|
|
x86_64) narch=x64 ;; aarch64) narch=arm64 ;; armv7l) narch=armv7l ;;
|
|
*) die "no Node build for $(uname -m)" ;;
|
|
esac
|
|
echo "Installing node ${nv} for host-mode actions"
|
|
ntmp=$(mktemp -d)
|
|
tarball="node-${nv}-linux-${narch}.tar.gz"
|
|
curl -fsSL "https://nodejs.org/dist/${nv}/${tarball}" -o "$ntmp/$tarball"
|
|
curl -fsSL "https://nodejs.org/dist/${nv}/SHASUMS256.txt" -o "$ntmp/SHASUMS256.txt"
|
|
(cd "$ntmp" && grep " ${tarball}\$" SHASUMS256.txt | sha256sum -c --quiet -)
|
|
rm -rf "/opt/node-${nv}"
|
|
mkdir -p "/opt/node-${nv}"
|
|
tar -xzf "$ntmp/$tarball" -C "/opt/node-${nv}" --strip-components=1 --no-same-owner
|
|
ln -sfn "/opt/node-${nv}/bin/node" /usr/local/bin/node
|
|
rm -rf "$ntmp"
|
|
fi
|
|
fi
|
|
|
|
# --- config ------------------------------------------------------------------
|
|
# State (config.yaml and the .runner credential) lives outside this checkout,
|
|
# so a git clean or re-clone never loses the registration.
|
|
install -d -m 0700 -o "$RUNNER_USER" -g "$(id -gn "$RUNNER_USER")" "$STATE"
|
|
cfg="$STATE/config.yaml"
|
|
before=$(sum "$cfg")
|
|
[ -f "$cfg" ] || as_user "$BIN" config init -c "$cfg" >/dev/null
|
|
IFS=, read -ra labels <<<"$RUNNER_LABELS"
|
|
as_user "$BIN" config set -c "$cfg" runner.file "$STATE/.runner"
|
|
as_user "$BIN" config set -c "$cfg" runner.capacity "$RUNNER_CAPACITY"
|
|
# Lets running jobs finish on stop/upgrade; matches TimeoutStopSec in the unit.
|
|
as_user "$BIN" config set -c "$cfg" runner.shutdown_timeout 3h
|
|
# Labels here override the registered ones, so this file is the one source.
|
|
as_user "$BIN" config set -c "$cfg" runner.labels "${labels[@]}"
|
|
[ "$before" = "$(sum "$cfg")" ] || changed=1
|
|
|
|
# --- registration ------------------------------------------------------------
|
|
if [ ! -f "$STATE/.runner" ]; then
|
|
if [ -n "$from" ]; then
|
|
[ -f "$from/.runner" ] || die "no .runner in $from"
|
|
install -m 0600 -o "$RUNNER_USER" -g "$(id -gn "$RUNNER_USER")" "$from/.runner" "$STATE/.runner"
|
|
echo "Adopted $from/.runner"
|
|
else
|
|
[ -n "${GITEA_RUNNER_REGISTRATION_TOKEN:-}" ] ||
|
|
die "not registered yet: set GITEA_RUNNER_REGISTRATION_TOKEN, or pass --from <dir>"
|
|
export GITEA_RUNNER_REGISTRATION_TOKEN
|
|
(cd "$STATE" && as_user "$BIN" register --no-interactive -c "$cfg" \
|
|
--instance "$INSTANCE" --name "$RUNNER_NAME" --labels "$RUNNER_LABELS")
|
|
fi
|
|
changed=1
|
|
fi
|
|
registered=$(sed -n 's/.*"name": *"\([^"]*\)".*/\1/p' "$STATE/.runner")
|
|
[ "$registered" = "$RUNNER_NAME" ] ||
|
|
echo "warning: .runner is registered as '$registered', not '$RUNNER_NAME'." \
|
|
"Renaming needs a fresh registration: remove $STATE/.runner and re-run with a token." >&2
|
|
|
|
# --- service -----------------------------------------------------------------
|
|
before=$(sum "$UNIT")
|
|
sed -e "s#@USER@#${RUNNER_USER}#g" -e "s#@BIN@#${BIN}#g" -e "s#@STATE@#${STATE}#g" \
|
|
gitea-runner.service >"$UNIT.new"
|
|
mv "$UNIT.new" "$UNIT"
|
|
[ "$before" = "$(sum "$UNIT")" ] || { changed=1; systemctl daemon-reload; }
|
|
systemctl enable --quiet gitea-runner
|
|
if [ "$changed" -eq 1 ] || ! systemctl is-active --quiet gitea-runner; then
|
|
echo "Restarting gitea-runner (waits for running jobs)..."
|
|
systemctl restart gitea-runner
|
|
fi
|
|
|
|
sleep 3
|
|
systemctl is-active --quiet gitea-runner || {
|
|
journalctl -u gitea-runner -n 20 --no-pager
|
|
die "gitea-runner is not running"
|
|
}
|
|
echo "gitea-runner $("$BIN" --version | awk '{print $NF}') is running as '$registered' with labels: ${RUNNER_LABELS}"
|