From afb4cd3669bf0a2f1c9d906238efa007c706a61b Mon Sep 17 00:00:00 2001 From: HenryDu8133 <813367384@qq.com> Date: Fri, 11 Sep 2026 22:28:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(ci):=20=E4=BF=AE=E5=A4=8D=20deploy.yml=20YA?= =?UTF-8?q?ML=20=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF=EF=BC=88line=20177=20?= =?UTF-8?q?could=20not=20find=20expected=20':'=EF=BC=89=EF=BC=8C=E6=B6=88?= =?UTF-8?q?=E9=99=A4=20heredoc=20=E4=B8=8E=E5=9D=97=E6=A0=87=E9=87=8F?= =?UTF-8?q?=E7=BC=A9=E8=BF=9B=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 根因:build-image job 注入 Docker daemon.json 时用了 bash heredoc <<'PY'...PY;heredoc 内部 Python 代码在 YAML 文件里缩进为 0,触发 run:| 字面块标量提前结束;Gitea YAML 解析器尝试把下一行解析为新的 key 却找不到冒号 → 抛 line 177 could not find expected ':',整个 workflow 无法触发 - 修复:移除 heredoc 结构,改为 PY_CODE 单引号字符串 + printf '%s' | python3 - stdin 管道传递;单引号内所有 Python 行统一保持与外层 shell 语句一致的 12 spaces 前导缩进(满足 YAML 块标量 ≥ 10 spaces 基础缩进要求);Python 顶级代码统一缩进 12 spaces,块内再缩进 4 spaces,避免 IndentationError - 验证:IDE YAML 诊断 0 错误;Gitea Actions 页左侧 deploy.yml 红色感叹号应立即消失,可正常触发 run --- .gitea/workflows/deploy.yml | 40 ++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 4077b1c..d803c3a 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -163,6 +163,8 @@ jobs: # ========== 关键修复:宿主机 Docker daemon 先注入国内 registry mirrors ========== # 原因:Runner 任何 docker pull / buildx create(moby/buildkit 拉基础镜像)都要先经过 daemon # 如果直接连 docker.io,国内会被限速到 <200KB/s,导致 Setup / BuildKit 拉镜像一直卡。 + # 注意:此处用 PY_CODE 字符串 + printf 管道传 Python 代码给 stdin, + # 避免 <<'PY' heredoc 结构与 YAML run:| 块标量缩进冲突导致的 could not find expected ':' 报错。 DOCKER_DAEMON_CONF="/etc/docker/daemon.json" if command -v docker >/dev/null 2>&1 && command -v sudo >/dev/null 2>&1; then sudo mkdir -p /etc/docker 2>/dev/null || true @@ -170,27 +172,23 @@ jobs: if [ -f "${DOCKER_DAEMON_CONF}" ]; then OLD_MIRRORS_JSON="$(sudo cat "${DOCKER_DAEMON_CONF}" 2>/dev/null || echo "{}")" fi - # 用 python3 合成合法 JSON(避免 bash 手搓 JSON 转义坑) - NEW_DAEMON_JSON="$( - python3 - "$OLD_MIRRORS_JSON" \ - "${DOCKER_REGISTRY_MIRROR_1}" "${DOCKER_REGISTRY_MIRROR_2}" "${DOCKER_REGISTRY_MIRROR_3}" <<'PY' -import json, sys -try: - data = json.loads(sys.argv[1] or "{}") -except Exception: - data = {} -mirrors_envs = [m for m in sys.argv[2:5] if m] -existing = data.get("registry-mirrors") or [] -if isinstance(existing, list): - combined = list(dict.fromkeys([*mirrors_envs, *existing])) -else: - combined = mirrors_envs -if combined: - data["registry-mirrors"] = combined -data.setdefault("log-driver", "json-file") -print(json.dumps(data, ensure_ascii=False, indent=2)) -PY - )" + PY_CODE=' import json, sys + try: + data = json.loads(sys.argv[1] or "{}") + except Exception: + data = {} + mirrors_envs = [m for m in sys.argv[2:5] if m] + existing = data.get("registry-mirrors") or [] + if isinstance(existing, list): + combined = list(dict.fromkeys([*mirrors_envs, *existing])) + else: + combined = mirrors_envs + if combined: + data["registry-mirrors"] = combined + data.setdefault("log-driver", "json-file") + print(json.dumps(data, ensure_ascii=False, indent=2))' + NEW_DAEMON_JSON="$(printf '%s\n' "$PY_CODE" | python3 - "$OLD_MIRRORS_JSON" \ + "${DOCKER_REGISTRY_MIRROR_1}" "${DOCKER_REGISTRY_MIRROR_2}" "${DOCKER_REGISTRY_MIRROR_3}")" echo "${NEW_DAEMON_JSON}" | sudo tee "${DOCKER_DAEMON_CONF}" >/dev/null echo "[Docker] daemon.json 已注入 registry-mirrors:" sudo cat "${DOCKER_DAEMON_CONF}" 2>/dev/null || true