Sub topic 14
Clone Patterns for CI Pipelines
This chapter assumes you know basic Git (clone, fetch, branch) and have seen --depth, --filter, and sparse checkout in earlier chapters. A CI job usually needs the current files and, sometimes, a little history. A full clone pays for far more than that on every run.
What a CI job needs
Start by asking what the job reads from Git. The answer picks the pattern.
| Job type | Needs | Pattern |
|---|---|---|
| Build or unit tests | Current snapshot only | Shallow, single branch, no tags |
| Version stamping with `git describe` | Tags and history back to the last tag | Deepen or fetch tags |
| Changed-files detection | Merge base with the target branch | Shallow plus targeted fetch |
| Monorepo service build | One or two directories | Partial clone plus sparse checkout |
| Blame or history analysis | Full history | Blobless clone |
Pattern 1: smallest snapshot
For a job that only compiles or tests the current commit, fetch one commit from one branch and skip tags.
git clone --depth 1 --single-branch --no-tags --branch main https://example.com/org/big-repo.gitThis is the cheapest starting point. Most CI systems also let you set a fetch depth in their checkout settings, which does the same thing.
Pattern 2: snapshot plus a bit of history
Some jobs compare the current commit with the target branch. A depth-1 clone has no merge base, so the comparison fails. Deepen the clone only as far as needed.
git fetch --deepen 50
git fetch --unshallowUse --deepen 50 first and fall back to --unshallow only if the merge base is still missing. Pick a depth that covers your usual branch length, not the whole history.
git clone --depth 1 https://example.com/org/big-repo.git
cd big-repo
git fetch --deepen 50
git merge-base HEAD origin/main || git fetch --unshallowIn this sequence, the || runs the full conversion only when the cheap attempt finds no merge base. In a depth-1 clone of one branch, origin/main may not exist at all. If so, fetch it explicitly first with git fetch --depth 50 origin main.
Pattern 3: monorepo service build
When a pipeline builds one service from a large monorepo, avoid downloading files for every other service.
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docs
git sparse-checkout listThe clone downloads commits and trees. The sparse-checkout set command then limits the working tree to two directories, so Git fetches only the blobs for those paths.
Pattern 4: snapshot build with a treeless filter
git clone --filter=tree:0 --depth 1 https://example.com/org/big-repo.gitThis omits trees and blobs until needed and keeps only one commit. It suits jobs that read the current snapshot and never run history commands.
Caching and fresh runners
Fresh runners start with an empty disk, so every job repeats the clone. Two habits reduce the cost:
- Keep a persistent checkout on long-lived runners and run
git fetchinstead of cloning again. - Cache the clone directory between runs when your CI system supports it, then fetch the new commits.
On ephemeral runners, prefer the smallest pattern that works, because nothing is reused.
Checking what the runner got
Log the state at the start of a job so failures are easy to diagnose.
git rev-parse --is-shallow-repository
git config --get remote.origin.partialclonefilter
git sparse-checkout listIf a step fails with a missing commit, tag, or merge base, the clone is usually too shallow. Deepen it before changing anything else.
Exercise
Your CI job runs git describe --tags after git clone --depth 1 --no-tags and fails. Name two ways to fix it while keeping the clone small.
Show answer
Fetch tags and enough history to reach the last tag. For example, run git fetch --deepen 100 --tags, or drop --no-tags and fetch a deeper history with git fetch --deepen 100. Use git fetch --unshallow --tags only if a smaller depth still cannot reach a tag.