Why CI jobs start cold
A CI runner starts empty. Before it can build, each job clones the repository, works out a cache key, downloads the build cache and unpacks it. Your source and your build cache are saved in two places, by two different steps, under two different names. When they don’t match, after a dependency update say, the cache is useless and the job builds the whole project from nothing. The build is rarely the slow part. Getting ready to build is.
The usual fixes help for a while. Looser cache keys hit more often, but then a job builds on top of files from some other branch. A cache shared by all jobs gets messy when two jobs write to it at once. Runners that keep their disk between jobs are fast, until one job leaves something behind that breaks the next.
The usual job
What we’ll build
In this guide we’ll do it another way. We save the source and its build cache together, as one version, once a night. Each job takes its own copy of that version, adds the change it is testing, and builds. The cache always matches the source, because they were saved at the same moment.
What we’re building
By the end, you’ll have:
- A nightly job that saves your project and its build cache as one commit.
- A private copy of that commit for every CI job, made without copying any files.
- Jobs that rebuild only the files their pull request changed.
- A record of exactly what each job changed, which you can open later.
- A way to rebuild from any older night when something breaks.
You’ll need a Skywake API key and Node in your CI. Set SKYWAKE_API_KEY as a CI secret, then install the SDK with npm i skywake.
Step 1
Save a warm workspace every night
Our example is a small web app. Its build keeps a cache in .cache: one compiled file per source file, named by a hash of what the source says. Yours might be called target or build; the idea is the same.
web-app/
- src/cart.ts# the file our pull request will change
- src/checkout.ts
- src/prices.ts
- src/search.ts
- .cache/# the build cache
- dist/# the build output
- package.json
First, a one-time setup. We make a disk, which is a set of files with branches and history, and fork its main branch into one called warm. A branch works like a Git branch for files: each save is a commit, and older commits stay.
import { Skywake } from "skywake";
const sky = new Skywake();
const app = await sky.createDisk("web-app");
await app.fork("warm");Then, every night, a scheduled job checks out main the way you do today, builds it, and uploads the whole folder, cache and all:
import { execSync } from "node:child_process";
const warm = sky.disk("web-app", { branch: "warm" });
// Build main first, so the cache is full when we save it.
execSync("npm run build", { cwd: "./web-app", stdio: "inherit" });
await warm.upload("./web-app", { message: "Nightly build" });Now warm holds last night’s source and the cache built from it, in one commit. The .git folder is left out; the branch keeps its own history.
Step 2
Give each job its own copy
When a CI job starts, it forks the warm branch. A fork is a new branch that starts at the same commit. It copies no files, so it is instant however big the cache is.
const id = process.env.CI_JOB_ID;
const job = await warm.fork(`jobs/${id}`);Each job now has a branch of its own, like jobs/4812. Whatever it writes stays there, so jobs never see each other’s files, and warm does not change.
Step 3
Add the change and build
The warm branch is last night’s main. The job is testing a pull request, so we put the pull request’s files on top. Your CI already fetches the pull request, and git diff --name-only lists what it changed. The source is usually the small part; the cache is the big one.
import { readFileSync } from "node:fs";
// The files this pull request changed, from its shallow fetch.
const files = ["src/cart.ts"];
await job.write(
Object.fromEntries(
files.map((f) => [f, readFileSync(`./pr/${f}`, "utf8")]),
),
{ message: `Job ${id}: pull request files` },
);Now download the job’s branch into a normal folder and run your build, exactly as you would on a laptop:
await job.download("./work");
const log = execSync("npm run build", {
cwd: "./work",
encoding: "utf8",
});
console.log(log);The build finds last night’s cache next to the source it came from. Only the file the pull request touched is compiled again:
$ npm run buildcompiled 4, cached 0
$ npm run buildcompiled 1, cached 3
Tip: use a cache that checks contents
download writes every file fresh, so every file has a new modified time. Build tools that decide what to rebuild by comparing file contents, or hashes of them, reuse the cache well. Tools that only compare times may rebuild more than they need.
Step 4
Keep what the job made
When the build is done, upload the folder back to the job’s branch. Then ask what changed compared to warm. Nothing writes to warm while the job runs, so the diff shows only what this job did.
await job.upload("./work", { message: `Job ${id}: output` });
const changed = await job.diff("warm");For our pull request, changed holds three paths and nothing else:
jobs/4812/
- src/cart.ts# modified: the pull request
- dist/cart.js# modified: its build output
- .cache/832110c92aba50b7.js# added: one new cache entry
This is handy when a job fails. The branch is the exact workspace the build saw, so anyone can download it later and look, instead of guessing from a log.
Step 5
Clean up, and go back in time
When the job passes and you no longer need its workspace, delete its branch:
await job.destroy();The nightly job runs nightly.ts again, and each night that changed something becomes a new commit on warm. A night with no changes adds nothing. Old nights stay in the history. So when a build that passed yesterday fails today, you can fork yesterday’s workspace and build it again:
const [, lastNight] = await Array.fromAsync(warm.log({ limit: 2 }));
const old = await warm.fork("jobs/yesterday", { at: lastNight.id });
await old.download("./old");log lists commits newest first, so the second one is the build before the latest. The folder you get has that night’s source and that night’s cache, and it builds warm, with nothing to compile.
Running it for real
A few things to know once every pipeline uses it:
- Forks stay instant. A fork copies no data. Forking a workspace of 20,000 files takes about 10 ms.
- Always clean up. Put
job.destroy()in afinallyblock, or delete oldjobs/branches on a schedule. Keep the ones that failed until someone has looked. - One warm branch per kind of runner. A cache built on one operating system or toolchain may not help another. Name them clearly, like
warm-linux. - Refresh as often as main moves. Once a night suits most teams. If
mainchanges a lot during the day, run the same nightly script every few hours, so pull requests have less to rebuild.