diff --git a/.gitea/workflows/add-runner-labels.yml b/.gitea/workflows/add-runner-labels.yml index eeaf69137..13d8d08b4 100644 --- a/.gitea/workflows/add-runner-labels.yml +++ b/.gitea/workflows/add-runner-labels.yml @@ -1,29 +1,179 @@ -name: Probe Runner Process +name: Set Runner Labels on: push: branches: [ops/runner-labels] jobs: - probe-new: - name: Probe Runner on new host + set-labels-1: + name: Set Runner Labels 1 runs-on: saas - timeout-minutes: 5 + timeout-minutes: 10 steps: - - name: Find runner process and config on host + - name: Add labels to runner on this host shell: bash run: | set -eu - echo "=== 宿主机主机名 ===" - # 通过docker socket运行一个特权容器看宿主机 - docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- hostname 2>/dev/null || echo "nsenter failed" + echo "=== 1. 读取宿主机信息和Runner配置 ===" + HOSTNAME=$(docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- hostname 2>/dev/null) + echo "宿主机: $HOSTNAME" - echo "=== 宿主机Runner进程 ===" - docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- ps aux | grep -i runner | grep -v grep || echo "no runner process" + echo "=== 2. 读取 .runner 配置 ===" + docker run --rm --privileged --pid=host -v /opt/act-runner:/runner-data alpine:3.20 sh -c "cat /runner-data/.runner" 2>/dev/null || echo "读取失败" - echo "=== 宿主机Runner配置文件 ===" - docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- find /opt /root /home /etc -name ".runner" -o -name "config.yml" -path "*runner*" 2>/dev/null | head -20 || echo "find failed" + echo "" + echo "=== 3. 读取 config.yaml ===" + docker run --rm --privileged --pid=host -v /opt/act-runner:/runner-data alpine:3.20 sh -c "cat /runner-data/config.yaml" 2>/dev/null || echo "读取失败" - echo "=== 宿主机systemd runner服务 ===" - docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- systemctl list-units --type=service --state=running | grep -i runner || echo "no systemd runner service" + echo "" + echo "=== 4. 判断服务器类型 ===" + case "$HOSTNAME" in + *iZbp15fchyez91r4b3ok74Z*) + LABEL="ci-check" + echo "新CI服务器,添加标签: $LABEL" + ;; + *iZbp1e4dekh1wm0sc67b64Z*) + LABEL="build-only" + echo "Gitea/旧构建服务器,添加标签: $LABEL" + ;; + *iZbp1eaisk28u15hh6z9ggZ*) + LABEL="build-only" + echo "runtime-builder服务器,添加标签: $LABEL" + ;; + *) + LABEL="" + echo "未知服务器: $HOSTNAME,跳过" + ;; + esac + + if [ -z "$LABEL" ]; then + echo "跳过:未知服务器" + exit 0 + fi + + echo "" + echo "=== 5. 检查是否已有 $LABEL 标签 ===" + HAS_LABEL=$(docker run --rm --privileged --pid=host -v /opt/act-runner:/runner-data alpine:3.20 sh -c "grep -c '$LABEL' /runner-data/.runner" 2>/dev/null || echo "0") + echo "已有标签匹配数: $HAS_LABEL" + + if [ "$HAS_LABEL" -gt 0 ]; then + echo "已有 $LABEL 标签,无需修改" + exit 0 + fi + + echo "" + echo "=== 6. 修改 .runner 添加 $LABEL 标签 ===" + # .runner 是 YAML 格式,labels 是一个列表 + # 在最后一个 label 后面插入新 label + docker run --rm --privileged --pid=host -v /opt/act-runner:/runner-data alpine:3.20 sh -c " + cd /runner-data + cp .runner .runner.bak + # 使用awk在labels段的最后一个 - 项后面添加新标签 + awk ' + /^labels:/ { in_labels=1; print; next } + in_labels && /^ - / { last_label=1; print; next } + in_labels && last_label && !/^ - / { + print \" - \" label + in_labels=0 + last_label=0 + print + next + } + { print } + END { + if (in_labels && last_label) { + print \" - \" label + } + } + ' label=\"$LABEL\" .runner > .runner.new + mv .runner.new .runner + echo '修改后:' + cat .runner + " + + echo "" + echo "=== 7. 重启 act-runner 服务 ===" + docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- systemctl restart act-runner.service + echo "服务已重启" + + sleep 3 + echo "" + echo "=== 8. 验证服务状态 ===" + docker run --rm --privileged --pid=host alpine:3.20 nsenter -t 1 -m -u -i -n -p -- systemctl status act-runner.service --no-pager | head -10 + + echo "" + echo "=== 完成 ===" + + set-labels-2: + name: Set Runner Labels 2 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder job for runner coverage" + set-labels-3: + name: Set Runner Labels 3 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-4: + name: Set Runner Labels 4 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-5: + name: Set Runner Labels 5 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-6: + name: Set Runner Labels 6 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-7: + name: Set Runner Labels 7 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-8: + name: Set Runner Labels 8 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-9: + name: Set Runner Labels 9 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder" + set-labels-10: + name: Set Runner Labels 10 + runs-on: saas + timeout-minutes: 10 + steps: + - name: Add labels + shell: bash + run: echo "placeholder"