Install gitea-runner on a VM as a systemd service
One hosts/<host>.env per runner (name, user, labels, capacity) and an idempotent install.sh: pinned VERSION with sha256 check, minimal config.yaml and .runner in /var/lib/gitea-runner, upstream's systemd unit. Replaces stpweb's gitea-runner/ and stpbuild's untracked copy, both started by @reboot cron. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/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
|
||||
|
||||
# --- 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" "$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}"
|
||||
Reference in New Issue
Block a user