《游戏编辑器技术原理与架构》12.3 云端协作与 CI 集成

流水线按阶段推进:阶段内可并行,阶段间串行;某阶段有失败,后续阶段全部跳过并记为失败。每个任务按输入清单算缓存键,命中缓存直接跳过。报告给出执行、跳过、失败三张清单与最大并行宽度。本节从零实现并真跑 6 条用例,对标 Unity / Unreal / Godot / Cocos Creator 的云端 CI 流水线。

12.3 云端协作与 CI 集成

流水线按阶段推进:阶段内可并行,阶段间串行;某阶段有失败,后续阶段全部跳过并记为失败。每个任务按输入清单算缓存键,命中缓存直接跳过。报告给出执行、跳过、失败三张清单与最大并行宽度。

本章与站内既有内容的分工

主题站内既有内容本节做什么
构建发布本卷 13.213.2 管配置;本节管流水线编排与缓存
资产管线《资产管线》 第 12 章卷二做导出;本节做多阶段流水线
全卷脉络本卷目录本节收口云编辑器章

从零实现:云端协作与 CI 集成

任务按阶段名分组,阶段名排序决定执行顺序。阶段内任务逐个检查缓存键:命中记入跳过,未命中加入缓存并执行。任一任务输入里带失败标记就让该阶段失败,后续阶段整体记失败。并行宽度取所有阶段的组内任务数最大值。

实现与交付边界

Unreal Horde 可运行 BuildGraph 构建。helio 模拟阶段执行和缓存命中。实际产物缓存还需纳入任务身份、工具版本和输入内容摘要,并检查产物是否存在。 Unreal Horde Build Automation

可运行产物:6 条用例

下面是 src/cloud-ci.ts,层级 L1,完整可编译。运行命令 node --test test/12-3-cloud-collab-ci.test.ts:

// file: src/cloud-ci.ts
// tier: L1
// verify: node --test test/12-3-cloud-collab-ci.test.ts
//
// 卷四(第十二章):云端协作与 CI 集成。
// 流水线按阶段推进:阶段内可并行,阶段间串行;某阶段有失败,后续阶段全部
// 跳过并记为 failed。每个任务按输入清单算缓存键,命中缓存直接跳过。报告
// 给出实际执行、跳过、失败三张清单与最大并行宽度。

export interface CiJob {
  name: string;
  stage: string;
  inputs: string[];
}

export interface RunReport {
  executed: string[];
  skipped: string[];
  failed: string[];
  parallelWidth: number;
}

/** FNV-1a 32 位哈希,返回 8 位十六进制。 */
export function cacheKey(inputs: string[]): string {
  let hash = 0x811c9dc5;
  const bytes = new TextEncoder().encode(JSON.stringify(inputs));
  for (const byte of bytes) {
    hash ^= byte;
    hash = Math.imul(hash, 0x01000193) >>> 0;
  }
  return hash.toString(16).padStart(8, '0');
}

export class Pipeline {
  #jobs: CiJob[] = [];

  add(job: CiJob): void {
    if (this.#jobs.some((j) => j.name === job.name)) {
      throw new Error(`任务 ${job.name} 已存在`);
    }
    this.#jobs.push({ ...job, inputs: [...job.inputs] });
  }

  run(cache: Set<string>): RunReport {
    const stages = new Map<string, CiJob[]>();
    for (const job of this.#jobs) {
      const list = stages.get(job.stage) ?? [];
      list.push(job);
      stages.set(job.stage, list);
    }
    const report: RunReport = {
      executed: [],
      skipped: [],
      failed: [],
      parallelWidth: 0,
    };
    let broken = false;
    for (const stage of [...stages.keys()].sort()) {
      const jobs = stages.get(stage) ?? [];
      report.parallelWidth = Math.max(report.parallelWidth, jobs.length);
      if (broken) {
        report.failed.push(...jobs.map((j) => j.name));
        continue;
      }
      let stageBroken = false;
      for (const job of jobs) {
        const key = cacheKey(job.inputs);
        if (cache.has(key)) {
          report.skipped.push(job.name);
          continue;
        }
        cache.add(key);
        if (job.inputs.includes('!fail')) {
          report.failed.push(job.name);
          stageBroken = true;
        } else {
          report.executed.push(job.name);
        }
      }
      if (stageBroken) broken = true;
    }
    return report;
  }

  count(): number {
    return this.#jobs.length;
  }
}

阶段名排序是刻意选择:用名字而不是插入顺序,意味着流水线在配置文件重排后行为不变。缓存键用 FNV-1a 而不是完整输入串,是因为缓存集合只需要短键;数组编码保留输入边界,32 位哈希仍可能碰撞。

下面是 test/12-3-cloud-collab-ci.test.ts,层级 L1:

// file: test/12-3-cloud-collab-ci.test.ts
// tier: L1
// verify: node --test test/12-3-cloud-collab-ci.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { cacheKey, Pipeline } from '../src/cloud-ci.ts';

const job = (name: string, stage: string, inputs: string[] = [name]) => ({ name, stage, inputs });

test('缓存键稳定且区分输入', () => {
  const a = cacheKey(['a.txt', 'b.txt']);
  assert.equal(a, cacheKey(['a.txt', 'b.txt']));
  assert.notEqual(a, cacheKey(['a.txt', 'c.txt']));
  assert.notEqual(cacheKey(['a|b']), cacheKey(['a', 'b']));
  assert.match(a, /^[0-9a-f]{8}$/);
});

test('阶段按名称顺序串行推进', () => {
  const p = new Pipeline();
  p.add(job('package', 'c'));
  p.add(job('compile', 'b'));
  p.add(job('lint', 'a'));
  const report = p.run(new Set());
  assert.deepEqual(report.executed, ['lint', 'compile', 'package']);
});

test('缓存命中跳过任务', () => {
  const p = new Pipeline();
  p.add(job('compile', 'a', ['src.ts']));
  const cache = new Set([cacheKey(['src.ts'])]);
  const report = p.run(cache);
  assert.deepEqual(report.skipped, ['compile']);
  assert.deepEqual(report.executed, []);
});

test('阶段失败后后续阶段全部失败', () => {
  const p = new Pipeline();
  p.add(job('compile', 'a', ['src.ts', '!fail']));
  p.add(job('package', 'b'));
  const report = p.run(new Set());
  assert.deepEqual(report.failed, ['compile', 'package']);
  assert.deepEqual(report.executed, []);
});

test('并行宽度取最宽阶段', () => {
  const p = new Pipeline();
  p.add(job('a1', 'a'));
  p.add(job('a2', 'a'));
  p.add(job('a3', 'a'));
  p.add(job('b1', 'b'));
  assert.equal(p.run(new Set()).parallelWidth, 3);
});

test('任务重名报错,空流水线宽度为零', () => {
  const p = new Pipeline();
  p.add(job('a', 's'));
  assert.throws(() => p.add(job('a', 's')), /已存在/);
  assert.equal(new Pipeline().run(new Set()).parallelWidth, 0);
});

本机实测(Node v26.5.0,node --test test/12-3-cloud-collab-ci.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。

前两条钉缓存键与阶段顺序;第三条钉缓存跳过;第四条钉失败传播;后两条钉并行宽度与重名。

小结

  • 流水线按阶段串行、阶段内并行。
  • 缓存键由输入清单决定,命中即跳过。
  • 阶段失败后,后续阶段整体记为失败。
  • 并行宽度取最宽阶段,作为调度参考。

阅读导航:上一节:12.2 远程桌面与云端构建 · 下一节:13.1 从编辑器触发构建 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路