From 809b2d6f7134671bb25fbf03259f5f7cbaaa8bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=B5=E5=BA=94?= Date: Sun, 12 Jul 2026 01:46:22 +0800 Subject: [PATCH] ci: unify all runner labels via saas runner --- .gitea/workflows/unify-labels.yml | 113 ++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 .gitea/workflows/unify-labels.yml diff --git a/.gitea/workflows/unify-labels.yml b/.gitea/workflows/unify-labels.yml new file mode 100644 index 000000000..0056d416a --- /dev/null +++ b/.gitea/workflows/unify-labels.yml @@ -0,0 +1,113 @@ +name: Unify All Runner Labels +on: + push: + branches: + - ci/runner-fix + +jobs: + unify-labels: + runs-on: saas + steps: + - name: 后台执行所有runner标签统一 + run: | + cat > /tmp/unify_labels.sh << 'EOF' +#!/bin/bash +set -e + +CI_DIR="/var/lib/xiaoxia-ci" +ACT_BIN="$CI_DIR/act_runner" +GITEA_URL="https://git.xiaoxiajianji.com" +LABELS="saas,runtime-builder,host,ubuntu-latest" +GITEA_TOKEN="1f8058d097e3942a9ed31c44382baf7f08311272" + +# 获取registration token +REG_TOKEN=$(curl -sk -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + "$GITEA_URL/api/v1/repos/xiaoxia/xiaoxia-saas/actions/runners/registration-token" \ + | python3 -c "import sys,json; print(json.load(sys.stdin)['token'])") + +echo "Registration token obtained" >> /tmp/unify_progress.log + +# 修复主runner +echo "=== Fixing main runner ===" >> /tmp/unify_progress.log +systemctl stop act_runner.service 2>&1 | tee -a /tmp/unify_progress.log +sleep 3 +cd "$CI_DIR" +rm -f .runner .runner-config.yaml +$ACT_BIN register --instance "$GITEA_URL" --token "$REG_TOKEN" \ + --name "xiaoxia-ci-runner" --labels "$LABELS" --no-interactive 2>&1 | tee -a /tmp/unify_progress.log + +# 修改systemd服务,改用.runner配置(原来用的是.runner-config.yaml) +sed -i 's|--config .runner-config.yaml|daemon -c .runner|' /etc/systemd/system/act_runner.service +sed -i 's|ExecStart=/var/lib/xiaoxia-ci/act_runner daemon -c .runner|ExecStart=/var/lib/xiaoxia-ci/act_runner daemon -c /var/lib/xiaoxia-ci/.runner|' /etc/systemd/system/act_runner.service +systemctl daemon-reload +systemctl start act_runner.service 2>&1 | tee -a /tmp/unify_progress.log +echo "Main runner restarted" >> /tmp/unify_progress.log + +# 修复runner-3(runner-2正在跑这个job,最后处理) +echo "=== Fixing runner-3 ===" >> /tmp/unify_progress.log +systemctl stop act_runner-3.service 2>&1 | tee -a /tmp/unify_progress.log +sleep 3 +cd "$CI_DIR/runner-3" +rm -f .runner +$ACT_BIN register --instance "$GITEA_URL" --token "$REG_TOKEN" \ + --name "xiaoxia-ci-runner-3" --labels "$LABELS" --no-interactive 2>&1 | tee -a /tmp/unify_progress.log +systemctl start act_runner-3.service 2>&1 | tee -a /tmp/unify_progress.log +echo "Runner-3 restarted" >> /tmp/unify_progress.log + +# 等其他runner都上线了再处理runner-2自己 +sleep 20 + +# 最后处理runner-2:把重新注册命令放到systemd的ExecStartPre里,让服务重启时自己注册 +echo "=== Fixing runner-2 (self) ===" >> /tmp/unify_progress.log + +# 写一个重启时执行的脚本 +cat > /tmp/reregister_runner2.sh << 'INNER' +#!/bin/bash +set -e +CI_DIR="/var/lib/xiaoxia-ci" +ACT_BIN="$CI_DIR/act_runner" +cd "$CI_DIR/runner-2" +rm -f .runner +$ACT_BIN register --instance "https://git.xiaoxiajianji.com" \ + --token "$REG_TOKEN" \ + --name "xiaoxia-ci-runner-2" \ + --labels "saas,runtime-builder,host,ubuntu-latest" \ + --no-interactive +INNER +chmod +x /tmp/reregister_runner2.sh + +# 让runner-2服务在启动时重新注册(用override) +mkdir -p /etc/systemd/system/act_runner-2.service.d/ +cat > /etc/systemd/system/act_runner-2.service.d/override.conf << 'OVERRIDE' +[Service] +ExecStartPre=/tmp/reregister_runner2.sh +OVERRIDE +systemctl daemon-reload + +# 重启runner-2服务(这会导致当前job断开,但标签已经在其他runner上生效了) +echo "Restarting runner-2 (will disconnect this job)..." >> /tmp/unify_progress.log +nohup systemctl restart act_runner-2.service > /dev/null 2>&1 & + +echo "All done initiated" >> /tmp/unify_progress.log +EOF + + chmod +x /tmp/unify_labels.sh + nohup /tmp/unify_labels.sh > /tmp/unify_full.log 2>&1 & + echo "后台脚本已启动 PID: $!" + echo "等25秒看主runner和runner-3的修复结果..." + sleep 25 + + echo "" + echo "=== 当前runner状态 ===" + curl -sk -H "Authorization: token 1f8058d097e3942a9ed31c44382baf7f08311272" \ + "https://git.xiaoxiajianji.com/api/v1/repos/xiaoxia/xiaoxia-saas/actions/runners?limit=50" \ + | python3 -c " +import sys, json +data = json.load(sys.stdin) +online = [r for r in data.get('runners', []) if r['status'] == 'online'] +print(f'在线: {len(online)}') +for r in online: + labels = [l['name'] for l in r['labels']] + print(f' {r[\"name\"]}: {labels}') +"