Speeding up CI, a practical guide
Most slow CI comes from a handful of causes, and each one has a known fix. Here are twelve of them with the snippet to copy, roughly in the order they pay off. The ones land-action already does for you are marked.
Everything here works on GitHub's free runners. The snippets are for GitHub Actions, and the ideas carry over to any CI.
01 Measure where the time goes
Before changing anything, split a run's time into two numbers. Queue time is how long a job waited for a runner, and run time is how long it actually ran. A fast suite that waits five minutes for a machine needs a different fix from a slow suite that starts instantly.
gh api repos/OWNER/REPO/actions/runs/RUN_ID/jobs --jq '.jobs[] | "\(.name) queued \((.started_at|fromdate) - (.created_at|fromdate))s ran \((.completed_at|fromdate) - (.started_at|fromdate))s"'
Then open the slowest job and sort its steps by duration. The usual top three are installing dependencies, the test step, and building.
02 Cancel runs nobody will read
When someone (or an agent) pushes three times in a minute, the first two runs are already out of date. A concurrency group cancels them, which frees runners and cuts queue time for everyone else. It takes two lines and costs nothing.
concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
Leaving main's runs uncancelled means every commit on main still gets a result to compare against.
03 Stop reinstalling dependencies
Downloading the same packages on every run is often the biggest single step. The setup actions can cache them keyed on your lockfile, so a run only downloads when the lockfile changes.
- uses: actions/setup-node@v4 with: { node-version: 22, cache: npm } - run: npm ci - uses: actions/setup-python@v5 with: { python-version: "3.12", cache: pip } - run: pip install -r requirements.txt
For Docker builds the equivalent is layer caching, cache-from: type=gha and cache-to: type=gha,mode=max on docker/build-push-action.
04 Run only the tests a change reaches
land-action does thisMost pull requests touch a small part of the code, yet CI reruns every test. There are three common ways to narrow it, and they trade effort for safety differently.
paths filters skip a whole workflow when nothing under a folder changed. They're easy, but someone has to keep them up to date, and a shared file that isn't listed lets a breaking change through.
Monorepo tools like nx affected and turbo --filter use the project graph you declare. They're precise between projects and blind to anything the graph doesn't say, such as a test reading a fixture file.
Recording reads watches every file each test command actually opens, so nothing has to be declared and a test reruns whenever anything it read changed. That's what land and land-action do.
# paths filter on: { pull_request: { paths: ["api/**"] } } # project graph: npx nx affected -t test --base=origin/main # npx turbo run test --filter="...[origin/main]" # recorded reads - uses: Ataraxy-Labs/land-action@v1 with: { run: "tests: pytest -q tests" }
05 Split the suite finely
land-action does thisAny selection method can only skip what it can tell apart. If the whole suite is one command, one changed file means the whole suite runs. Splitting by test file lets an edit to one module rerun just the files that use it.
- uses: Ataraxy-Labs/land-action@v1 with: split: files run: "tests: pytest -q tests"
06 Use every core, then shard
land-action does halfGitHub's standard Linux runners have 4 cores, and a test runner left on its defaults often uses one. Turn on parallel workers first, since it's free. Only after that is it worth splitting across machines with a matrix, because each extra machine pays for checkout and install again.
pytest -n auto # pytest-xdist npx vitest run --shard=1/3 npx jest --shard=1/3
Balance shards by how long tests take rather than by how many files each gets, or one shard keeps running long after the others finish.
07 Don't reprove what a PR already proved
land-action does thisA pull request often gets pushed many times, and each push usually touches one or two files. If CI only remembers main, every push reruns everything the previous push already passed. land-action keeps a record for each pull request, so the fifth push reruns only what the fifth push changed.
08 Rerun a failure before trusting it
land-action does thisFlaky tests cost more than their runtime, because each one sends someone digging into a change that didn't break anything. Rerunning a failed test on its own tells the two cases apart. A test that passes alone is either flaky or depends on another test's leftovers, and both are worth quarantining and fixing.
pytest --lf # rerun only the last run's failures pytest -p no:randomly tests/test_x.py::test_y
09 Pin the toolchain
apt-get install, a latest image tag or a floating Node version can turn main red on a day nobody changed anything. Pin versions in the workflow and commit the lockfile, so a red run always means a real change.runs-on: ubuntu-24.04 # not ubuntu-latest steps: - uses: actions/setup-node@v4 with: { node-version-file: .nvmrc }
10 Test what actually merges
merge_group event so your checks run there too.on: pull_request: merge_group: push: { branches: [main] }
This is where land goes further. It merges several finished branches with main, tests them together in one run, and names the branch that broke the tests.
11 Make failures quick to read
fail-fast in a matrix when you want every failure from one run.pytest --junitxml=results.xml npx vitest run --reporter=junit --outputFile=results.xml
12 Faster machines, last
Bigger runners from GitHub, or providers like Blacksmith, Depot and Namespace, make every step faster without changing your workflow. They're worth it once the steps above are done, because they speed up the work you kept, and the steps above remove the work you didn't need.
Where to start
- Measure queue time and run time for one slow run (01).
- Add the concurrency group and dependency caching (02, 03). Both take minutes.
- Add land-action with
split: filesto cover 04, 05, 07, 08 and the parallel half of 06 in one step. - Pin versions and add the merge queue when red builds start costing more than slow ones (09, 10).
Going further
When CI is still the bottleneck, or several people and agents are merging into main at once, these come next.
land
The same recorder as land-action, running on land's own machines and taking over merging too.
- Merges several finished branches with main and tests them together in one run
- Names the branch that broke the tests, with the failing test and a one-command repro
- Environments built once and kept warm, so installs don't rerun
- Move a workflow over by changing
runs-on: ubuntu-latesttoruns-on: land - 500 free minutes a month with no card, then from $0.0008 a minute, billed per second