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:
@@ -0,0 +1,71 @@
|
||||
# stprunner
|
||||
|
||||
Installs [gitea-runner](https://gitea.com/gitea/runner) (formerly act_runner)
|
||||
on a VM as a systemd service, for the Gitea at `git.adam-french.co.uk`. One
|
||||
`hosts/<host>.env` per machine holds what differs between them: the runner's
|
||||
name, user, labels and capacity.
|
||||
|
||||
| Host | Runner | Labels | What runs there |
|
||||
|---|---|---|---|
|
||||
| `stpvm` (VM 106) | `stpvm` | `self-hosted` (host), `ubuntu-*` (docker) | Jobs that touch the stpweb stack: deploys, maintenance |
|
||||
| `stpbuild` (VM 123) | `stpbuild-runner` | `build` (host) | Tests, image builds and Renovate for every repo |
|
||||
|
||||
Both run in **host mode** as `adamf`, not in Docker: stpvm's deploy job runs
|
||||
`docker compose up` against the host's own stack, and build jobs use the
|
||||
host's Docker daemon and `sudo apt`. That is why this isn't upstream's
|
||||
`gitea/runner` image.
|
||||
|
||||
## New VM
|
||||
|
||||
```sh
|
||||
git clone http://git.adam-french.co.uk:3000/adamf/stprunner.git ~/deploy/stprunner
|
||||
cd ~/deploy/stprunner
|
||||
cp hosts/stpbuild.env hosts/<host>.env # then edit, and commit it
|
||||
sudo GITEA_RUNNER_REGISTRATION_TOKEN=<token> ./install.sh <host>
|
||||
```
|
||||
|
||||
Mint the token in Site Administration → Actions → Runners, or on stpgit with
|
||||
`docker exec gitea gitea actions generate-runner-token`. It is single-use:
|
||||
registration turns it into `/var/lib/gitea-runner/.runner`, which is the
|
||||
lasting credential. Gitea's runner list then shows the new runner.
|
||||
|
||||
The VM needs `curl`, `git` and, for container or host jobs that use it,
|
||||
Docker with `RUNNER_USER` in the `docker` group. A VM without Tailscale can
|
||||
still be set up from the Proxmox host: `qm guest exec <vmid> -- bash -c '...'`.
|
||||
|
||||
## Upgrading
|
||||
|
||||
Renovate opens a PR when gitea-runner releases (pinned as `VERSION` in
|
||||
`install.sh`). After merging, on each VM:
|
||||
|
||||
```sh
|
||||
cd ~/deploy/stprunner && git pull && sudo ./install.sh <host>
|
||||
```
|
||||
|
||||
`install.sh` is idempotent: it downloads only a new `VERSION` (checking its
|
||||
sha256), re-applies the host file's labels and capacity, and restarts the
|
||||
service only if something changed. The restart waits up to 3 h for running
|
||||
jobs. A major release can raise the minimum Gitea version (v6 needs Gitea
|
||||
1.26.2+), so upgrade stpgit first.
|
||||
|
||||
## Layout on a VM
|
||||
|
||||
| Path | What |
|
||||
|---|---|
|
||||
| `/usr/local/bin/gitea-runner` | The binary |
|
||||
| `/var/lib/gitea-runner/config.yaml` | Minimal config written by `install.sh`; every other option keeps its default (`gitea-runner config generate` lists them) |
|
||||
| `/var/lib/gitea-runner/.runner` | Registration credential (0600). Back it up; losing it means re-registering |
|
||||
| `/etc/systemd/system/gitea-runner.service` | From `gitea-runner.service` here |
|
||||
|
||||
Logs: `journalctl -u gitea-runner -f`.
|
||||
|
||||
## Changing a runner
|
||||
|
||||
- **Labels or capacity:** edit `hosts/<host>.env`, commit, then `git pull &&
|
||||
sudo ./install.sh <host>`. The config's labels override the registered ones.
|
||||
- **Name:** Gitea keeps the name from registration. Remove
|
||||
`/var/lib/gitea-runner/.runner`, re-run with a fresh token, then delete the
|
||||
old offline entry in Gitea's runner list.
|
||||
- **Adopting an existing registration** (moving a runner here from elsewhere
|
||||
keeps its ID): stop the old daemon, then
|
||||
`sudo ./install.sh <host> --from <dir containing .runner>`.
|
||||
@@ -0,0 +1,25 @@
|
||||
# Installed by install.sh, which fills in the @...@ values. Based on
|
||||
# upstream's examples/systemd/gitea-runner.service.
|
||||
[Unit]
|
||||
Description=Gitea Actions runner (stprunner)
|
||||
Documentation=https://gitea.com/gitea/runner
|
||||
After=network-online.target docker.service
|
||||
Wants=network-online.target
|
||||
# Wants, not Requires: a no-op on hosts without Docker.
|
||||
Wants=docker.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
# Host-mode jobs run as this user with its groups (docker), HOME and SSH key.
|
||||
User=@USER@
|
||||
WorkingDirectory=@STATE@
|
||||
ExecStart=@BIN@ daemon --config @STATE@/config.yaml
|
||||
# Also covers Gitea being unreachable at boot, so no wait loop is needed.
|
||||
Restart=always
|
||||
RestartSec=10s
|
||||
# Let running jobs finish; keep in sync with runner.shutdown_timeout.
|
||||
KillMode=mixed
|
||||
TimeoutStopSec=3h
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,8 @@
|
||||
# stpbuild (Proxmox VM 123, 192.168.178.240): the build VM. Instance-wide;
|
||||
# tests, image builds and Renovate for every repo (`runs-on: build`). Host
|
||||
# mode as adamf (docker group, passwordless sudo for apt in jobs). No SSH key:
|
||||
# jobs fetch over HTTP with their own GITEA_TOKEN.
|
||||
RUNNER_NAME=stpbuild-runner
|
||||
RUNNER_USER=adamf
|
||||
RUNNER_CAPACITY=2
|
||||
RUNNER_LABELS="build:host"
|
||||
@@ -0,0 +1,8 @@
|
||||
# stpvm (Proxmox VM 106): the stpweb host. Takes only the jobs that touch that
|
||||
# stack (`self-hosted`: deploys, maintenance). Host mode as adamf, who owns
|
||||
# ~/deploy/stpweb and the SSH key the deploy job fetches with. The ubuntu-*
|
||||
# labels run jobs in containers. Registered as `stppi` until 2026-11-27.
|
||||
RUNNER_NAME=stpvm
|
||||
RUNNER_USER=adamf
|
||||
RUNNER_CAPACITY=1
|
||||
RUNNER_LABELS="self-hosted:host,ubuntu-latest:docker://docker.gitea.com/runner-images:ubuntu-latest,ubuntu-24.04:docker://docker.gitea.com/runner-images:ubuntu-24.04,ubuntu-22.04:docker://docker.gitea.com/runner-images:ubuntu-22.04"
|
||||
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}"
|
||||
@@ -0,0 +1,33 @@
|
||||
// What Renovate updates here; it runs from stpweb's .gitea/workflows/renovate.yaml
|
||||
// (this repo is in its RENOVATE_REPOSITORIES). Merging a bump changes nothing
|
||||
// until `sudo ./install.sh <host>` is re-run on each runner VM.
|
||||
{
|
||||
$schema: "https://docs.renovatebot.com/renovate-schema.json",
|
||||
extends: ["config:recommended"],
|
||||
timezone: "Europe/London",
|
||||
|
||||
minimumReleaseAge: "3 days",
|
||||
|
||||
customManagers: [
|
||||
{
|
||||
// The pinned gitea-runner release in install.sh.
|
||||
customType: "regex",
|
||||
managerFilePatterns: ["/^install\\.sh$/"],
|
||||
matchStrings: ['VERSION="(?<currentValue>[0-9][\\w.-]*)"'],
|
||||
depNameTemplate: "gitea/runner",
|
||||
datasourceTemplate: "gitea-releases",
|
||||
registryUrlTemplate: "https://gitea.com",
|
||||
extractVersionTemplate: "^v(?<version>.+)$",
|
||||
},
|
||||
],
|
||||
|
||||
packageRules: [
|
||||
// A major can raise the minimum Gitea version (v6 needed 1.26.2), so
|
||||
// check its notes against stpgit before merging.
|
||||
{
|
||||
matchPackageNames: ["gitea/runner"],
|
||||
matchUpdateTypes: ["major"],
|
||||
prBodyNotes: ["Major release: check the minimum Gitea version in the release notes against stpgit before merging."],
|
||||
},
|
||||
],
|
||||
}
|
||||
Reference in New Issue
Block a user