如何为 ARM64 与 AMD64 双架构构建镜像并并行测试?

解读

国内大厂(阿里、腾讯、字节、华为)的云原生交付流程普遍要求“一次构建、到处运行”。ARM64 服务器(鲲鹏、倚天 710)与 AMD64(x86_64)并存,镜像必须双架构兼容,否则线上扩容会失败。面试官真正想考察的是:

  1. 你是否能把“构建”与“测试”两条流水线拆成可并行的阶段,而不是串行等待;
  2. 是否熟悉 pytest 在异构容器里的执行策略(插件、fixture、标记);
  3. 是否能把“镜像构建”与“测试报告”整合到一条 CI 记录里,方便回滚与审计。

知识点

  1. buildx:Docker Desktop 与国内主流 CI(GitLab-CI、阿里云 ACR、腾讯云 TCR)均已内置,支持 docker buildx build --platform linux/amd64,linux/arm64 --push。
  2. manifest list:buildx 会生成一个联合 manifest,tag 相同,运行时自动拉取对应架构层。
  3. pytest 并行:pytest-xdist 的 pytest -n auto --dist=loadfile 可把用例按文件粒度分发到多个容器实例;国内镜像源(清华、中科大)需在 Dockerfile 里提前换源,否则拉包超时。
  4. fixture 感知架构:在 conftest.py 里通过 platform.machine() 返回 aarch64 或 x86_64,再搭配 pytest.mark.skipif() 给特定用例打标,实现“同一代码库、不同架构条件执行”。
  5. 国产芯片坑点:ARM64 容器里安装 grpcio、numpy 必须预装编译工具链(gcc、g++、libffi-dev),否则 pip 轮子会回退到源码编译,CI 时间暴涨;建议在 Dockerfile 里用国内 wheels 仓库或者提前构建 whl 缓存层。
  6. 报告聚合:pytest-html 或 allure 生成的 xml/json 报告需用 actions/upload-artifact@v3(GitHub)或 artifacts:paths:(GitLab)统一收集,再跑 allure generate --clean 做汇总,避免双架构报告互相覆盖。
  7. 资源调度:国内自建 K8s 集群往往把 ARM 节点打上 arch=arm64 污点,CI 里必须 tolerations + nodeSelector 双管齐下,否则调度不到。

答案

  1. 代码层准备

    • conftest.py 里注册 fixture:
      import platform, pytest
      @pytest.fixture(scope="session", autouse=True)
      def arch():
          return platform.machine()
      
    • 给需要区分架构的用例加标记:
      @pytest.mark.skipif(platform.machine() != "aarch64", reason="arm only")
      def test_neon_intrinsic():
          ...
      
  2. Dockerfile 多架构

    # syntax=docker/dockerfile:1
    FROM --platform=$BUILDPLATFORM python:3.11-slim AS builder
    COPY requirements.txt /tmp/
    RUN pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple -r /tmp/requirements.txt
    
    FROM python:3.11-slim
    COPY --from=builder /root/.local /usr/local
    COPY tests /app/tests
    WORKDIR /app
    CMD ["pytest", "-n", "auto", "--html=report.html", "--self-contained-html"]
    
  3. 本地一次性构建并推送

    docker buildx create --use --name mybuilder
    docker buildx build --platform linux/amd64,linux/arm64 \
         -t registry.cn-hangzhou.aliyuncs.com/yourproj/yourapp:1.0.0 \
         --push .
    
  4. CI 并行测试(GitLab-CI 片段)

    stages: [build, test]
    
    build:
      stage: build
      image: docker:latest
      services: [docker:dind]
      script:
        - docker buildx create --use
        - docker login -u $ALIYUN_USER -p $ALIYUN_PASS registry.cn-hangzhou.aliyuncs.com
        - docker buildx build --platform linux/amd64,linux/arm64 -t $IMAGE_TAG --push .
      only: [merge_requests, main]
    
    test:amd64:
      stage: test
      image: $IMAGE_TAG
      tags: [x86_64]
      script:
        - pytest -n auto --html=report_amd64.html --self-contained-html
      artifacts:
        paths: [report_amd64.html]
        expire_in: 7 days
      needs: [build]
    
    test:arm64:
      stage: test
      image: $IMAGE_TAG
      tags: [arm64]
      script:
        - pytest -n auto --html=report_arm64.html --self-contained-html
      artifacts:
        paths: [report_arm64.html]
        expire_in: 7 days
      needs: [build]
    

    两条 test job 完全并行,跑完后可在 GitLab Pages 或自建 Nginx 目录里同时查看两份报告。

  5. 结果回写 在 CI 最后加一段 aggregate job,把 report_amd64.html、report_arm64.html 合并成 index.html,方便 QA 一键确认双架构通过率。

拓展思考

  1. 如果仓库里包含 Cython 扩展,双架构构建时间会被拉长,可引入 cibuildwheel 在 CI 里先打出 manylinux2014_aarch64 与 x86_64 的 whl,再 COPY 进最终镜像,把编译阶段与测试阶段解耦。
  2. 国内私有云往往没有国外那么稳定的 buildx 缓存,建议把 --cache-to type=registry,ref=... 与 --cache-from 指向阿里云 ACR 的缓存仓库,命中率可提升 60% 以上。
  3. 当用例需要启动外围依赖(MySQL、Redis)时,可在 pytest-docker-compose 或 testcontainers-python 里根据 arch fixture 自动选择 mysql:8.0-oraclelinux 的对应架构镜像,避免 x86_64 容器在 ARM 节点上拉不到镜像而失败。
  4. 若项目最终要发布到边缘 K8s(ARM 盒子 + x86 边缘服务器),可在 CI 里加一步“ helm chart 多架构校验”,用 helm template | kubeval --ignore-missing-schemas 检查是否有硬编码的 nodeSelector.kubernetes.io/arch: amd64,提前发现调度隐患。