Keep Go's caches out of the test stage's image (closes #256)
check / check (push) Waiting to run

The test stage ran go mod download in its own layer, and go test left
Go's build cache in another. Exporting them took about 20 seconds of
every make test, and nothing reads either one afterwards.

Removing the build cache alone saved almost nothing, because the module
cache is the larger layer. So the download now runs in the same RUN as
go test, and go clean removes both caches once the tests pass. A failing
run still reruns with -v and exits 1.

Model: opus-5-5
This commit is contained in:
2026-11-03 10:32:23 +00:00
parent c12668226f
commit 6785689216
+8 -4
View File
@@ -33,14 +33,18 @@ RUN golangci-lint run --config .golangci.yml ./...
# golang:1.24.13-bookworm, 2026-10-06
FROM golang@sha256:1a6d4452c65dea36aac2e2d606b01b4a029ec90cc1ae53890540ce6173ea77ac AS test
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN mkdir -p web/dist && touch web/dist/index.html web/dist/style.css web/dist/app.js
# -p 4 because test runs on a shared build host cap their parallelism.
RUN go test -p 4 -timeout 90s -race -cover ./... || \
# The modules download in this RUN rather than a layer of their own, because
# exporting a layer that holds them is slow. Nothing reads them or the build
# cache afterwards, so go clean removes both.
RUN set -e; \
go mod download; \
go test -p 4 -timeout 90s -race -cover ./... || \
{ echo "--- Rerunning with -v for details ---"; \
go test -p 4 -timeout 90s -race -v ./...; exit 1; }
go test -p 4 -timeout 90s -race -v ./...; exit 1; }; \
go clean -cache -modcache
# Build stage. Nothing is wanted from either phase above; the copies
# are what make BuildKit build them first, so this stage cannot run