go build, go test and go vet. A later job reads these results.
Then Go compiles only the packages that changed.
Setup
The Go cache is off by default, because it changes howgo runs. An admin
sets it to on in the Shared cache tab on the Runners page of the Trunk
web app. The admin can set it for the organization or for one repository. See
Cache settings.
You must use Go 1.24 or later. An older Go does not use the shared cache.
It uses only its local build cache.
Environment variables and log lines
When the Go cache is on, the runner starts with these environment variables:
You can also set this variable:
At the start of the job, the job log shows one of these lines:
trunk-ci: the Go cache is set up for steps on the runner host; a container step or a Docker action builds without it.trunk-ci: the Go cache helper is not on this runner; this job builds without the Go cache.
go command, the helper writes one summary line to stderr:
- A
local hitis an entry from the local directory. - A
remote hitis an entry from the shared cache. - A
putis an entry that Go wrote. - An
uploadsends aputto the shared cache.
Steps that use the cache
Trunk sets the variables in the environment of the runner, not in
$GITHUB_ENV. Thus a container step does not get a GOCACHEPROG that it
cannot start.
To run one step without the cache, set an empty value for that step:
Failure behavior
A cache problem does not stop the build.- When a read fails, is too slow or finds no entry, it is a miss. Go then compiles the package.
- When an upload fails, the helper drops it.
- After three failures in sequence, the helper stops reads or uploads for the
remaining time of that
gocommand. - A read waits for a maximum of 10 seconds.
- The helper does not upload an entry that is larger than 2 GiB.
Docker steps
A Go build in your own container can also use the cache. Put the helper in the container, and give the container the cache address:- The helper is a static binary. It runs in all Linux images, Alpine too.
- Put the real file in the container (
readlink -f).$GOCACHEPROGcan be a symbolic link. - The cache address has no credential. The runner adds the credential. Thus the container gets no secret.
- When
TRUNK_GOCACHEPROG_URLstarts withhttps://, do not change it.
Helper output on stderr
The helper writes itstrunk-go-cache: lines to stderr, and go sends them on.
Some tools read each stderr line of go test as an error. For example,
gotestsum does this. For these tools, use a small wrapper script. The wrapper
writes the lines to a file. Set the wrapper as GOCACHEPROG for that step only:
golangci-lint
golangci-lint can also keep its analysis cache in the shared cache. It readsGOLANGCI_LINT_CACHEPROG. This variable uses the same protocol as
GOCACHEPROG.
Key prefixes
The cache uses all the text after/cache/v1/go/ in the address as the key of
the entry. Thus, when you add a path segment to TRUNK_GOCACHEPROG_URL, you
get a different set of keys. Use this when the key of the tool does not
include an input that changes the result.
For example, the key of golangci-lint includes the path of the
ruleguard rules file. It does not
include the contents of that file. Thus, after you change a rule,
golangci-lint can use a result from before the change. This result can come
from a different branch. Put a hash of the rules in the address to prevent
this:
Behavior and limits
- Some misses occur in each job. Go keeps an index of each package directory of your module in the build cache. The key of the index includes the time when each file changed. A new checkout gives each file a new time. Thus these entries miss in each job, and the helper uploads them again. Go makes them again in a few milliseconds. When a job changes little, these entries are frequently most of the misses.
- A change to an exported API makes Go compile the importers again. A change can alter the exported types, the functions or the inlinable code of a package. Then all the packages that import it get new keys. The first job that builds that version compiles them. A change in the body of a function does not have an effect on the importers.
- The Go cache does not keep modules.
GOMODCACHEstays on the runner. To keep the downloaded modules for later jobs, put them in a template, or useactions/cache. - Branches. By default, a job reads entries from its own branch and from the default branch. It writes only to its own branch. An admin can let a job also read entries from other branches. See Cache settings.
- Storage. The Go cache follows the cache storage policy. When a repository is more than its limit, Trunk removes the entries that jobs used least recently.