Commit 08dbed4e authored by wenwen.tang's avatar wenwen.tang 😕

fix(deploy): 修正构建找不到 Dockerfile 与管道吞掉失败两个问题

- Dockerfile 位于 deploy/ 下,docker build 必须用 -f 指定,
  否则报 "failed to read dockerfile: open Dockerfile: no such file or directory"
- 原写法 `docker build … | tail` 的退出码来自 tail,会把构建失败**吞掉**;
  症状是"构建明明报错,脚本却继续用旧镜像重启容器"。
  改为先重定向日志、取真实 rc,再回显尾部
parent 0f4af2e8
Pipeline #835 canceled with stages
...@@ -149,12 +149,21 @@ def pull(cli): ...@@ -149,12 +149,21 @@ def pull(cli):
def build(cli): def build(cli):
D.step("构建镜像") D.step("构建镜像")
log(" 构建上下文:%s(仓库根,.dockerignore 已裁剪)" % REPO_DIR) log(" 构建上下文:%s(仓库根,.dockerignore 已裁剪)" % REPO_DIR)
log(" Dockerfile :deploy/Dockerfile")
log(" 基础镜像:python:3.11.3-slim(服务器本地已有,无需拉取)") log(" 基础镜像:python:3.11.3-slim(服务器本地已有,无需拉取)")
rc, o, e = sh(cli, # 两个坑,都踩过:
"cd %s && docker build -t %s . 2>&1 | tail -28" % (REPO_DIR, D.IMAGE), # 1) Dockerfile 在 deploy/ 下,必须用 -f 指定,否则报
timeout=1800) # "failed to read dockerfile: open Dockerfile: no such file or directory"
# 2) 不能写成 `docker build … | tail`:管道的退出码来自 tail,
# 会把构建失败吞掉(症状是"构建报错却仍用旧镜像重启容器")。
# 这里先重定向到日志、取真实 rc,再回显尾部。
cmd = ("cd %s && docker build -f deploy/Dockerfile -t %s . "
"> /tmp/rentos-build.log 2>&1; rc=$?; "
"tail -28 /tmp/rentos-build.log; echo \"---- build rc=$rc ----\"; exit $rc"
% (REPO_DIR, D.IMAGE))
rc, o, e = sh(cli, cmd, timeout=1800)
if rc != 0: if rc != 0:
log("!! 构建失败(rc=%d)" % rc) log("!! 构建失败(rc=%d)—— 完整日志在服务器 /tmp/rentos-build.log" % rc)
return False return False
rc2, o2, _ = sh(cli, "docker images %s --format '{{.Repository}}:{{.Tag}} {{.Size}} {{.CreatedSince}}'" rc2, o2, _ = sh(cli, "docker images %s --format '{{.Repository}}:{{.Tag}} {{.Size}} {{.CreatedSince}}'"
% D.IMAGE, quiet=True) % D.IMAGE, quiet=True)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment