Skip to content

建议优化 Docker 构建耗时:拆分日常单架构构建和发布多架构构建 #9

@Zker67

Description

@Zker67

问题

当前 Docker 镜像构建比较重,主要原因是镜像内同时包含 Python/uv、Next 前端构建、Browser Bridge、Chromium 和 Node/npm 运行依赖。如果 CI 默认每次都构建 linux/amd64,linux/arm64,在普通 amd64 GitHub hosted runner 上,linux/arm64 通常会通过 QEMU 模拟执行,系统依赖安装、Chromium 相关依赖处理、镜像导出和 cache 导出都会明显变慢。

这会导致普通提交也承担完整多架构发布成本。对日常开发来说,很多场景只需要快速验证并发布 linux/amd64;多架构镜像更适合放在 tag/release 或手动触发流程中。

建议改动

  1. 默认分离日常构建和正式发布构建:

    • main 分支 push 默认只构建 linux/amd64
    • v* tag 或 release 场景构建 linux/amd64,linux/arm64
    • workflow_dispatch 增加 platforms 输入,默认 linux/amd64,需要时手动填写 linux/amd64,linux/arm64
  2. cache 策略按场景区分:

    • 单架构日常构建可以使用 cache-to=type=gha,mode=min,减少 cache 导出时间。
    • 多架构发布构建继续使用 cache-to=type=gha,mode=max
  3. 后续可以考虑维护基础 runtime 镜像:

    • 把 Chromium、系统依赖、uv/npm 基础环境前置到基础镜像中。
    • 应用镜像只处理源码、依赖锁定和启动文件。
    • 这是维护成本更高的改法,可以作为后续优化,不必阻塞前两个低风险改动。

参考 workflow 片段

on:
  push:
    branches:
      - main
    tags:
      - v*
  workflow_dispatch:
    inputs:
      platforms:
        description: 构建平台列表
        required: false
        default: linux/amd64
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" && -n "${REQUESTED_PLATFORMS}" ]]; then
  platforms="${REQUESTED_PLATFORMS}"
elif [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
  platforms="linux/amd64,linux/arm64"
else
  platforms="linux/amd64"
fi

建议验证

  • 对比 linux/amd64 单架构构建和 linux/amd64,linux/arm64 多架构构建的总耗时。
  • 确认 main 分支普通提交不会触发 QEMU arm64 构建。
  • 确认 tag 或手动指定多架构时仍能发布 multi-platform manifest。
  • 镜像启动后检查 /health、Browser Bridge 相关路径和 Grok app-chat 路径。

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions