graylog-deploy/setup-forgejo-runner.sh
byrsapty 458aada5a5 Add setup-forgejo-runner.sh and document the full from-scratch deployment
The CI runner setup was done by hand this session (download, verify,
register, systemd unit) - script it the same idempotent way as
install-graylog.sh so a fresh deployment can reproduce it instead of
requiring manual SSH archaeology. Wire it into create-graylog-lxc.sh's
payload copy, and add step 9 to both READMEs covering the one-time setup.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-22 21:55:29 +03:00

110 lines
4 KiB
Bash

#!/usr/bin/env bash
# Runs INSIDE the Graylog LXC container as root (invoked via `pct exec <vmid> -- bash setup-forgejo-runner.sh`).
# Idempotent: safe to re-run after a partial failure.
#
# Registers a Forgejo Actions self-hosted runner in "host" mode (no Docker -
# it runs shell steps directly on this container, which already has
# everything install-graylog.sh needs) and wires it up as a systemd service
# so it survives reboots.
#
# The registration token is a one-time credential from Forgejo itself, not
# something this script invents - get it from:
# Repo -> Settings -> Actions -> Runners -> "Create new Runner", or
# GET /api/v1/repos/<owner>/<repo>/actions/runners/registration-token
# (requires a personal access token with repo admin rights)
set -euo pipefail
FORGEJO_URL="${FORGEJO_URL:?Set FORGEJO_URL, e.g. https://git.zotac.keenetic.link}"
FORGEJO_RUNNER_TOKEN="${FORGEJO_RUNNER_TOKEN:?Set FORGEJO_RUNNER_TOKEN (registration token from the repo's Actions -> Runners page)}"
RUNNER_NAME="${RUNNER_NAME:-graylog-container-$(hostname)}"
RUNNER_VERSION="12.13.1"
RUNNER_DIR="/opt/forgejo-runner"
if [ -t 2 ]; then
C_RESET=$'\033[0m'; C_CYAN=$'\033[36m'; C_GREEN=$'\033[32m'; C_YELLOW=$'\033[33m'; C_RED=$'\033[1;31m'
else
C_RESET=''; C_CYAN=''; C_GREEN=''; C_YELLOW=''; C_RED=''
fi
log() { echo "${C_CYAN}[setup-forgejo-runner]${C_RESET} $*" >&2; }
ok() { echo "${C_GREEN}[setup-forgejo-runner] ✓${C_RESET} $*" >&2; }
skip() { echo "${C_YELLOW}[setup-forgejo-runner] ⏭${C_RESET} $*" >&2; }
die() { echo "${C_RED}[setup-forgejo-runner] ✗ ERROR:${C_RESET} $*" >&2; exit 1; }
[ "$(id -u)" -eq 0 ] || die "must run as root inside the container (use pct exec)"
step_install_binary() {
if [ -x /usr/local/bin/forgejo-runner ] && /usr/local/bin/forgejo-runner --version 2>/dev/null | grep -q "$RUNNER_VERSION"; then
skip "forgejo-runner $RUNNER_VERSION already installed"
return
fi
log "Downloading and verifying forgejo-runner $RUNNER_VERSION..."
local base="https://code.forgejo.org/forgejo/runner/releases/download/v${RUNNER_VERSION}"
local tmp; tmp="$(mktemp -d)"
( cd "$tmp" \
&& curl -fsSL -o "forgejo-runner-${RUNNER_VERSION}-linux-amd64" "$base/forgejo-runner-${RUNNER_VERSION}-linux-amd64" \
&& curl -fsSL -o "forgejo-runner-${RUNNER_VERSION}-linux-amd64.sha256" "$base/forgejo-runner-${RUNNER_VERSION}-linux-amd64.sha256" \
&& sha256sum -c "forgejo-runner-${RUNNER_VERSION}-linux-amd64.sha256" )
chmod +x "$tmp/forgejo-runner-${RUNNER_VERSION}-linux-amd64"
mv "$tmp/forgejo-runner-${RUNNER_VERSION}-linux-amd64" /usr/local/bin/forgejo-runner
rm -rf "$tmp"
ok "installed forgejo-runner $RUNNER_VERSION"
}
step_register() {
mkdir -p "$RUNNER_DIR"
if [ -f "$RUNNER_DIR/.runner" ]; then
skip "runner already registered ($RUNNER_DIR/.runner exists)"
return
fi
log "Registering runner '$RUNNER_NAME' with $FORGEJO_URL (host mode, label self-hosted:host)..."
( cd "$RUNNER_DIR" && /usr/local/bin/forgejo-runner register --no-interactive \
--instance "$FORGEJO_URL" \
--token "$FORGEJO_RUNNER_TOKEN" \
--name "$RUNNER_NAME" \
--labels self-hosted:host )
ok "runner registered"
}
step_config() {
if [ -f "$RUNNER_DIR/config.yaml" ]; then
skip "config.yaml already exists"
return
fi
( cd "$RUNNER_DIR" && /usr/local/bin/forgejo-runner generate-config > config.yaml )
ok "generated config.yaml"
}
step_service() {
if systemctl is-active --quiet forgejo-runner; then
skip "forgejo-runner service already running"
return
fi
cat > /etc/systemd/system/forgejo-runner.service <<EOF
[Unit]
Description=Forgejo Actions Runner
After=network.target docker.service
[Service]
Type=simple
WorkingDirectory=$RUNNER_DIR
ExecStart=/usr/local/bin/forgejo-runner daemon --config $RUNNER_DIR/config.yaml
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now forgejo-runner
ok "forgejo-runner service started and enabled"
}
main() {
step_install_binary
step_register
step_config
step_service
}
main