Files
cozystack/hack/go-mod-download.sh
T
Aleksei Sviridkin 5eee22fe28 ci(workflows): cache Go modules and fall back to direct in Go jobs
The unit and controller test job has no module cache and runs on an
ephemeral runner, so every run fetches every module from
proxy.golang.org, and one transient stream reset fails the job.

It now restores the module cache keyed on every go.sum in the tree and
prefetches the modules of every tracked go.mod through a bounded retry,
hack/go-mod-download.sh. The cache is written from main by the build
cache warmer under the same key, because a cache saved by a PR run is
visible to that PR only, while one saved on the default branch is
readable from every PR.

Every job that runs Go on the runner sets
GOPROXY=https://proxy.golang.org|direct so any proxy error falls back
to the origin. The release jobs get only that, not a cache.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-01 19:37:00 +03:00

30 lines
1.0 KiB
Bash
Executable File

#!/bin/sh
# `go mod download` with a bounded retry. proxy.golang.org now and then resets
# an HTTP/2 stream mid-download ("stream error ... INTERNAL_ERROR"), and one
# reset otherwise fails the whole job. A retry is safe: the module cache keeps
# whatever finished, and a module whose checksum does not match go.sum fails
# every attempt the same way.
#
# Runner jobs call it; Dockerfiles carry the same loop inline, as
# hack/go-module-fetch-contract.bats explains.
set -eu
attempts=3
backoff="${GO_MOD_DOWNLOAD_BACKOFF:-10}"
case "$backoff" in
'' | *[!0-9]*)
echo "go mod download: GO_MOD_DOWNLOAD_BACKOFF must be a whole number of seconds, got '$backoff'" >&2
exit 2
;;
esac
attempt=1
until go mod download "$@"; do
if [ "$attempt" -ge "$attempts" ]; then
echo "go mod download: giving up after $attempt attempts" >&2
exit 1
fi
echo "go mod download: attempt $attempt of $attempts failed, retrying in $((attempt * backoff))s" >&2
sleep $((attempt * backoff))
attempt=$((attempt + 1))
done