Skip to content

Commit

Permalink
feat: add cache volume for GOMODCACHE location
Browse files Browse the repository at this point in the history
In Go 1.15 there's separate environment variable to control the cache
locaiton: golang/go#34527, but as we're on Go
1.14 yet, we use compatible approach with `GOPATH/pkg` being cached
across all Go-related build steps.

This speeds up `go mod download` a lot when only some module
dependencies got changed.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira authored and talos-bot committed Aug 20, 2020
1 parent 73f402f commit 197b72f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2020-08-19T15:36:34Z by kres 29f29d8-dirty.
# Generated on 2020-08-20T18:05:09Z by kres 73f402f-dirty.

ARG TOOLCHAIN

Expand All @@ -27,6 +27,7 @@ RUN apk --update --no-cache add bash curl build-base
FROM toolchain AS tools
ENV GO111MODULE on
ENV CGO_ENABLED 0
ENV GOPATH /go
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /bin v1.30.0
ARG GOFUMPT_VERSION
RUN cd $(mktemp -d) \
Expand All @@ -39,8 +40,8 @@ FROM tools AS base
WORKDIR /src
COPY ./go.mod .
COPY ./go.sum .
RUN go mod download
RUN go mod verify
RUN --mount=type=cache,target=/go/pkg go mod download
RUN --mount=type=cache,target=/go/pkg go mod verify
COPY ./internal ./internal
COPY ./cmd ./cmd
RUN go list -mod=readonly all >/dev/null
Expand All @@ -51,7 +52,7 @@ WORKDIR /src/cmd/kres
ARG VERSION_PKG="github.com/talos-systems/kres/internal/version"
ARG SHA
ARG TAG
RUN --mount=type=cache,target=/root/.cache/go-build go build -ldflags "-s -w -X ${VERSION_PKG}.Name=kres -X ${VERSION_PKG}.SHA=${SHA} -X ${VERSION_PKG}.Tag=${TAG}" -o /kres
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go build -ldflags "-s -w -X ${VERSION_PKG}.Name=kres -X ${VERSION_PKG}.SHA=${SHA} -X ${VERSION_PKG}.Tag=${TAG}" -o /kres

# runs gofumpt
FROM base AS lint-gofumpt
Expand All @@ -62,17 +63,17 @@ RUN FILES="$(gofumports -l -local github.com/talos-systems/kres .)" && test -z "
FROM base AS lint-golangci-lint
COPY .golangci.yml .
ENV GOGC 50
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.cache/golangci-lint golangci-lint run --config .golangci.yml
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.cache/golangci-lint --mount=type=cache,target=/go/pkg golangci-lint run --config .golangci.yml

# runs unit-tests with race detector
FROM base AS unit-tests-race
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS}
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS}

# runs unit-tests
FROM base AS unit-tests-run
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -count 1 ${TESTPKGS}
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -count 1 ${TESTPKGS}

FROM scratch AS kres
COPY --from=kres-build /kres /kres
Expand Down
3 changes: 2 additions & 1 deletion internal/project/golang/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (build *Build) CompileDockerfile(output *dockerfile.Output) error {
}

stage.Step(step.Script(fmt.Sprintf(`go build -ldflags "%s" -o /%s`, ldflags, build.Name())).
MountCache(filepath.Join(build.meta.CachePath, "go-build")))
MountCache(filepath.Join(build.meta.CachePath, "go-build")).
MountCache(filepath.Join(build.meta.GoPath, "pkg")))

output.Stage(build.Name()).
From("scratch").
Expand Down
3 changes: 2 additions & 1 deletion internal/project/golang/golangcilint.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func (lint *GolangciLint) CompileDockerfile(output *dockerfile.Output) error {
Step(step.Env("GOGC", "50")).
Step(step.Run("golangci-lint", "run", "--config", ".golangci.yml").
MountCache(filepath.Join(lint.meta.CachePath, "go-build")).
MountCache(filepath.Join(lint.meta.CachePath, "golangci-lint")),
MountCache(filepath.Join(lint.meta.CachePath, "golangci-lint")).
MountCache(filepath.Join(lint.meta.GoPath, "pkg")),
)

return nil
Expand Down
9 changes: 6 additions & 3 deletions internal/project/golang/toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package golang

import (
"fmt"
"path/filepath"

"github.com/talos-systems/kres/internal/dag"
"github.com/talos-systems/kres/internal/output/dockerfile"
Expand Down Expand Up @@ -50,6 +51,7 @@ func NewToolchain(meta *meta.Options) *Toolchain {
meta.BuildArgs = append(meta.BuildArgs, "TOOLCHAIN")
meta.BinPath = toolchain.binPath()
meta.CachePath = toolchain.cachePath()
meta.GoPath = "/go"

return toolchain
}
Expand Down Expand Up @@ -130,7 +132,8 @@ func (toolchain *Toolchain) CompileDockerfile(output *dockerfile.Output) error {
Description("build tools").
From("toolchain").
Step(step.Env("GO111MODULE", "on")).
Step(step.Env("CGO_ENABLED", "0"))
Step(step.Env("CGO_ENABLED", "0")).
Step(step.Env("GOPATH", toolchain.meta.GoPath))

if err := dag.WalkNode(toolchain, func(node dag.Node) error {
if builder, ok := node.(common.ToolchainBuilder); ok {
Expand All @@ -148,8 +151,8 @@ func (toolchain *Toolchain) CompileDockerfile(output *dockerfile.Output) error {
Step(step.WorkDir("/src")).
Step(step.Copy("./go.mod", ".")).
Step(step.Copy("./go.sum", ".")).
Step(step.Run("go", "mod", "download")).
Step(step.Run("go", "mod", "verify"))
Step(step.Run("go", "mod", "download").MountCache(filepath.Join(toolchain.meta.GoPath, "pkg"))).
Step(step.Run("go", "mod", "verify").MountCache(filepath.Join(toolchain.meta.GoPath, "pkg")))

for _, directory := range toolchain.meta.GoDirectories {
base.Step(step.Copy("./"+directory, "./"+directory))
Expand Down
2 changes: 2 additions & 0 deletions internal/project/golang/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error {
Step(step.Arg("TESTPKGS")).
Step(step.Script(`go test -v -covermode=atomic -coverprofile=coverage.txt -count 1 ${TESTPKGS}`).
MountCache(filepath.Join(tests.meta.CachePath, "go-build")).
MountCache(filepath.Join(tests.meta.GoPath, "pkg")).
MountCache("/tmp"))

output.Stage("unit-tests").
Expand All @@ -52,6 +53,7 @@ func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error {
Step(step.Arg("TESTPKGS")).
Step(step.Script(`go test -v -race -count 1 ${TESTPKGS}`).
MountCache(filepath.Join(tests.meta.CachePath, "go-build")).
MountCache(filepath.Join(tests.meta.GoPath, "pkg")).
MountCache("/tmp").
Env("CGO_ENABLED", "1"))

Expand Down
3 changes: 3 additions & 0 deletions internal/project/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type Options struct {
// Path to /bin.
BinPath string

// Go's GOPATH.
GoPath string

// Path to ~/.cache.
CachePath string
}

0 comments on commit 197b72f

Please sign in to comment.