diff --git a/deploy/scripts/deploy-built-service.sh b/deploy/scripts/deploy-built-service.sh index 860474778..f47f8f2c9 100644 --- a/deploy/scripts/deploy-built-service.sh +++ b/deploy/scripts/deploy-built-service.sh @@ -273,6 +273,13 @@ deploy_local() { fi echo "新镜像健康检查失败: $IMAGE" >&2 + failed_container_id="$(local_container_id || true)" + if [ -n "$failed_container_id" ]; then + echo "新镜像容器最近日志(最多 200 行):" >&2 + docker logs --tail 200 "$failed_container_id" 2>&1 || true + else + echo "新镜像容器不存在,无法读取启动日志" >&2 + fi expected_prefix="$IMAGE_REPOSITORY/$SERVICE:" case "$previous_image" in "$expected_prefix"*) @@ -476,6 +483,13 @@ if [ "$new_started" = "true" ] && wait_for_health; then fi echo "新镜像健康检查失败: $new_image" >&2 +failed_container_id="$(remote_container_id || true)" +if [ -n "$failed_container_id" ]; then + echo "新镜像容器最近日志(最多 200 行):" >&2 + docker logs --tail 200 "$failed_container_id" 2>&1 || true +else + echo "新镜像容器不存在,无法读取启动日志" >&2 +fi expected_prefix="$image_repository/$service:" case "$previous_image" in "$expected_prefix"*) previous_tag="${previous_image#"$expected_prefix"}" ;; diff --git a/scripts/deploy-built-service-rollback.test.mjs b/scripts/deploy-built-service-rollback.test.mjs index e8103388e..84906b7b8 100644 --- a/scripts/deploy-built-service-rollback.test.mjs +++ b/scripts/deploy-built-service-rollback.test.mjs @@ -54,6 +54,9 @@ case "$command" in *) exit 2 ;; esac ;; + logs) + echo 'mock startup failure' + ;; *) echo "unexpected docker command: $command $*" >&2 exit 2 @@ -129,6 +132,9 @@ test('unhealthy deployment restores the previous healthy image and still fails t assert.equal(result.status, 1, output) assert.match(output, /新镜像健康检查失败: registry\.local\/group\/firstleg\/audit-service:newbad/) + assert.match(output, /新镜像容器最近日志(最多 200 行):/) + assert.match(output, /mock startup failure/) + assert.match(result.dockerLog, /logs --tail 200 mock-container/) assert.match(output, /开始自动回滚到: registry\.local\/group\/firstleg\/audit-service:oldgood/) assert.match(output, /自动回滚成功: registry\.local\/group\/firstleg\/audit-service:oldgood/) assert.match(result.dockerLog, /pull registry\.local\/group\/firstleg\/audit-service:oldgood/)