一分钟内的持续集成
Subminute CI

原始链接: https://engineering.moab.com/posts/subminute-ci/

Moab 是一个作为财务数据可信来源的 ERP 系统,它将其庞大的代码库迁移到单一代码仓库;尽管测试覆盖率快速增长,仍将 CI 的中位耗时降至一分钟以内。它旧的 pytest/xdist 配置在三小时后就会超时。如今,快速 CI 缩短了迭代周期,帮助工程师完成许多并行工作流,也成为工程纪律和团队协同一致情况的间接指标。 这些收益来自四个方面:并行化、缓存、硬件和消除浪费。测试会依据历史耗时数据分发到不同运行器,并跨多个 vCPU 派生测试进程;CI 任务尽可能独立运行。持久磁盘用于缓存 Git 数据、Docker 镜像和数据库快照。16 vCPU 的快速运行器启动很快。团队还通过延迟导入、跳过未受影响的检查、更快的工具、原生 fork、延迟编译和精简报告流程,消除了无效工作。大语言模型帮助发现了许多优化点。重要的是,他们仍然运行每一个单元测试,把正确性看得比进一步提速更重要。

Hacker News 上关于“不到一分钟的 CI”的讨论集中在大幅缩短 CI 流水线耗时上。一位评论者介绍了通过跳过步骤、分片、智能任务加载,以及利用 CI 积压估算来给编码智能体限流的优化方式。另一位则认为 CI 不必充当编排器:只要智能体的操作经过策略和供应链证明的验证,它们就可以管理整个生命周期;据称这使算力用量减少了一半。其他人也分享了类似成果,其中一个 Rails 团队将构建时间从 20 多分钟缩短到 5 分钟以内,不过 factory_bot 仍是瓶颈。技术要点包括 fork 与线程的对比、Rust 对缓存和增量测试的需求,以及对 AI 智能体编写低价值测试的质疑。
相关文章

原文

Over a weekend in August, we migrated our entire codebase to a monorepo and brought its median CI time to under 1 minute.

We've held it there, while significantly increasing our test count.

Datadog dashboard showing a 58.6 second median CI execution time, with weekly medians under one minute from August 29 to November 26 Datadog chart from September 8 to November 24: test count grew to about 280 percent of its starting level while CI runtime stayed between about 90 and 130 percent

Moab's product is an enterprise resource planning (ERP) system for equipment businesses. Our customers rely on it as their financial source of truth.

In the early days of Moab, fast CI was easy. With a small codebase using pytest and xdist on a GitHub Actions runner, CI can complete in under a minute. The hard part is keeping it fast while scaling. Moab now powers some very large businesses. Our codebase contains millions of lines of code, and the quantum of tests we had added to ensure correctness at scale considerably slowed down CI. In advance of publishing this post, we tried running our CI on GitHub Actions using our initial pytest and xdist setup. We'd like to tell you how long it took to complete, but the initial run timed out after 3 hours.

Fast CI shortens our iteration loop, especially for our agents. Most engineers are now juggling tens of project threads, and CI is the bottleneck on closing each one out. With fast CI, we can respond to customer requests, fix bugs, and generally ship the broad range of capabilities required for an ERP extremely quickly.

Apart from the obvious benefits, fast CI is meaningful as it's a sort of internal diagnostic test for technical understanding and team alignment. Lowering CI time requires understanding every layer of the stack. Any team can claim they do, but this exercise shows some hard proof. Keeping it low requires total team alignment, since any one engineer can degrade the objective by pushing aberrantly slow tests. We think CI time is one of the best proxies for engineering culture.

Our stack is AWS, Terraform, Python, Flask, SQLAlchemy, Postgres, and React.js with TypeScript. We achieved subminute CI by collectively sweating every second of wasted time at every layer of the stack.

Inspired by Boris Cherny's claimed use of Fable to reduce CI time, we used LLMs to uncover many of these optimization opportunities.

The changes we made follow four themes: Parallelism, Caching, Hardware, and Elimination.

Parallelism

More than anything, this keeps our CI time flat as our test count rises.

We initialize our app, freeze it, and then fork it to match the number of available vCPUs in the runner pool.

Unit tests are distributed across runners using execution time metrics from previous runs. Within each runner, the tests are distributed among processes using the Longest Processing Time heuristic. Tests without time metrics are deterministically spread out across runners using hash partitioning.

We also minimize interdependence between actions in the CI workflow. We run every check as a separate job. If jobs in the critical path don't have to be sequential, we make sure that they aren't.

Caching

We attach a storage volume to test runners that persists data across executions. We use this scheme to save on both repeated computation and network requests.

We have a git mirror of our application code on the disk, avoiding a full GitHub checkout. Now each run only requires a shallow fetch from GitHub.

Docker images live on the disk as well, avoiding a lot of build time. We have a job to bake new images as needed. We restart Docker with the entire Docker data root directory coming from the disk. We warm the disk by running reads on the Docker images and application source code, so subsequent reads hit loaded pages.

A snapshot of the development database lives in Amazon S3. Pulling the snapshot avoids DDL operations to construct the schema and reduces time spent loading fixtures. Migrations are applied to this snapshot if the PR contains them.

Hardware

We use a vendor to access fast runners.

The time between a code push and the runner starting to execute can add a significant amount of CI time. Most of our jobs use 16 vCPU Ubuntu x64 runners and start within 8 to 12 seconds. We profiled startup time across different machine sizes and in our testing found that two 16 vCPU machines start faster than a single 32 vCPU machine.

Elimination

When striving to measure CI time in seconds, small wins are worth pursuing. Every abstraction and ounce of complexity has to pay its weight. Understanding the layers of abstraction in your stack such that you can delete the ones you don't need is key to eking out speed boosts.

  • We use lazy imports to avoid importing the whole application up front.
  • Garbage collection is skipped whenever possible, such as during the initial application import.
  • We don't run actions that are unaffected by the current changes. For example, the database migration step is skipped for PRs with no schema changes, and Semgrep runs only on diffs instead of the whole codebase.
  • We use the fastest tools available (uv, ruff, ty, bun check, oxlint, oxfmt) in place of slower predecessors.
  • We moved from xdist to native forking.
  • Our pipeline reports its results before exiting the runner to keep cleanup time out of the critical path, and we poll for completed jobs rather than using the GitHub API for dependencies.
  • Telemetry and pytest extensions are disabled during the run.
  • Pydantic model compilation is deferred until the model is used. Flask route building is also deferred.
  • Our FactoryBoy database fixtures persist with a flush instead of a commit.
  • We squashed migrations.

Notably, we intentionally avoided trying to predict which unit tests are in scope based on the PR changes. We always run every single unit test. We're the source of truth for our customers, so we decided this tradeoff wasn't worth the speed boost.


Thank you to Pavel Asparouhov, Shreyan Jain, Yunyu Lin, Jiajun Xu, and Jason Katzer for reading drafts of this.

联系我们 contact @ memedata.com