Files
persionalBrand/deploy/runner/install-runner.sh
T
que01 1001c43af6
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Static checks (push) Has been cancelled
chore: 提供可复现的 CI runner 安装脚本
2026-10-21 14:41:47 +08:00

129 lines
4.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 在 CI 主机上安装并注册 Gitea Actions runner(Ubuntu/arm64 或 amd64)。
#
# 用法(在 runner 主机上、仓库之外执行):
# GITEA_URL=https://git.example.com RUNNER_TOKEN=xxx ./deploy/runner/install-runner.sh
# DRY_RUN=1 ... ./deploy/runner/install-runner.sh # 只打印将执行的命令
#
# 需要的环境变量:
# GITEA_URL Gitea 站点地址
# RUNNER_TOKEN 仓库 Settings → Actions → Runners 里生成的注册 token
# 可选:
# RUNNER_NAME runner 名称,默认取 hostname
# RUNNER_LABELS 默认 ubuntu-latest,oracle-arm
# RUNNER_VERSION 默认 5.2.0
# RUNNER_ROOT 默认 $HOME/gitea-runner
# CACHE_PORT 默认 8193
# RUNNER_CACHE_HOST 默认 172.17.0.1(Docker 默认 bridge 网关)
set -euo pipefail
GITEA_URL=${GITEA_URL:?需要设置 GITEA_URL}
RUNNER_TOKEN=${RUNNER_TOKEN:?需要设置 RUNNER_TOKEN}
RUNNER_NAME=${RUNNER_NAME:-$(hostname -s)}
RUNNER_LABELS=${RUNNER_LABELS:-ubuntu-latest,oracle-arm}
RUNNER_VERSION=${RUNNER_VERSION:-5.2.0}
RUNNER_ROOT=${RUNNER_ROOT:-$HOME/gitea-runner}
CACHE_PORT=${CACHE_PORT:-8193}
RUNNER_CACHE_HOST=${RUNNER_CACHE_HOST:-172.17.0.1}
CACHE_DIR=${CACHE_DIR:-$RUNNER_ROOT/cache-data}
DRY_RUN=${DRY_RUN:-0}
command -v docker >/dev/null 2>&1 || { echo "缺少 docker,请先安装并启动 Docker" >&2; exit 1; }
command -v systemctl >/dev/null 2>&1 || { echo "缺少 systemd,当前脚本只支持 systemd 主机" >&2; exit 1; }
case "$(uname -m)" in
aarch64 | arm64) ARCH=arm64 ;;
x86_64 | amd64) ARCH=amd64 ;;
*) echo "不支持的架构: $(uname -m)" >&2; exit 1 ;;
esac
run() {
if [ "$DRY_RUN" = "1" ]; then
echo "+ ${*//$RUNNER_TOKEN/***}"
else
"$@"
fi
}
archive="gitea-runner-${RUNNER_VERSION}-linux-${ARCH}.xz"
base="https://dl.gitea.com/gitea-runner/${RUNNER_VERSION}"
run mkdir -p "$RUNNER_ROOT" "$CACHE_DIR"
if [ ! -x "$RUNNER_ROOT/gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" ]; then
run curl -fsSL -o "$RUNNER_ROOT/$archive" "$base/$archive"
run curl -fsSL -o "$RUNNER_ROOT/$archive.sha256" "$base/$archive.sha256"
run env -C "$RUNNER_ROOT" sha256sum -c "$archive.sha256"
run xz -d -f "$RUNNER_ROOT/$archive"
fi
run ln -sf "gitea-runner-${RUNNER_VERSION}-linux-${ARCH}" "$RUNNER_ROOT/gitea-runner"
if [ ! -f "$RUNNER_ROOT/.runner" ]; then
run env -C "$RUNNER_ROOT" ./gitea-runner register \
--no-interactive \
--instance "$GITEA_URL" \
--token "$RUNNER_TOKEN" \
--name "$RUNNER_NAME" \
--labels "$RUNNER_LABELS"
run chmod 600 "$RUNNER_ROOT/.runner"
fi
run tee "$RUNNER_ROOT/config.yaml" >/dev/null <<CONFIG
log:
level: debug
runner:
file: .runner
capacity: 1
timeout: 2h
fetch_timeout: 10s
fetch_interval: 2s
labels:
$(printf '%s\n' "$RUNNER_LABELS" | tr ',' '\n' | sed 's|^\(.*\)$| - "\1:docker://node:24-bookworm"|')
cache:
enabled: true
dir: ${CACHE_DIR}
host: ${RUNNER_CACHE_HOST}
port: ${CACHE_PORT}
container:
options: "--cpus=1.5 --memory=4g"
valid_volumes: []
docker_host: "-"
CONFIG
run mkdir -p "$HOME/.config/systemd/user"
run tee "$HOME/.config/systemd/user/gitea-runner.service" >/dev/null <<UNIT
[Unit]
Description=Gitea Actions Runner (${RUNNER_NAME})
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=${RUNNER_ROOT}
ExecStart=${RUNNER_ROOT}/gitea-runner daemon -c ${RUNNER_ROOT}/config.yaml
Restart=always
RestartSec=5
[Install]
WantedBy=default.target
UNIT
# 用户级 systemd 服务默认在最后一个会话退出后停止;开启 linger 才能在重启后自动拉起 runner。
if command -v loginctl >/dev/null 2>&1; then
run sudo loginctl enable-linger "$USER"
fi
run systemctl --user daemon-reload
run systemctl --user enable --now gitea-runner.service
# 任务容器经 Docker bridge 访问 runner 内置 cache server,ufw 默认 INPUT DROP 会拦截。
# Gitea Actions 会给每个 job 建独立 bridge(实测 192.168.x),因此三个私网段都要放行。
if command -v ufw >/dev/null 2>&1; then
for cidr in 172.16.0.0/12 192.168.0.0/16 10.0.0.0/8; do
run sudo ufw allow from "$cidr" to any port "$CACHE_PORT" proto tcp comment "gitea actions cache"
done
fi
echo
echo "runner 已安装:$RUNNER_ROOT"
echo "检查状态:systemctl --user status gitea-runner.service"
echo "检查 linger:loginctl show-user \"$USER\" -p Linger"
echo "检查日志:journalctl --user -u gitea-runner -n 100 --no-pager"