go / test-cache

go test caches passing test results and compiled packages. To reuse that cache across CI runs, I persist the cache directories and make each test binary depend only on its source.

Caches

Go keeps two on-disk caches:

On a CI VM I put both on the local disk and reuse them across runs:

export GOCACHE=/var/cache/ci/go-cache
export GOMODCACHE=/var/cache/ci/go-mod

Stable inputs

The result cache keys on the inputs to each test binary. Two defaults change those inputs on every CI run:

Two flags remove both:

go test -buildvcs=false -trimpath ./...

Identical source then produces an identical binary, and go test reuses cached results across commits and across boxes that share a GOCACHE.

Result

The first run populates the cache. The next run on the same source reports (cached) for unchanged packages and finishes in seconds. cmd / cibot runs its CI cache this way.

← All articles