Skip to content

Commit 3374450

Browse files
committed
Control mounting build cache with env variable
1 parent febe8fe commit 3374450

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

dev-tools/mage/crossbuild.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,19 @@ func (b GolangCrossBuilder) Build() error {
331331
hostDir := filepath.Join(build.Default.GOPATH, "pkg", "mod")
332332
args = append(args, "-v", hostDir+":/go/pkg/mod:ro")
333333
}
334-
// Mount the go build cache into the container.
335-
out, err := exec.Command("go", "env", "GOCACHE").Output()
336-
if err != nil {
337-
return fmt.Errorf("failed to get GOCACHE: %w", err)
334+
335+
buildCacheLocation := "/tmp/.cache/go-build"
336+
if CrossBuildMountBuildCache {
337+
// Mount the go build cache into the container.
338+
out, err := exec.Command("go", "env", "GOCACHE").Output()
339+
if err != nil {
340+
return fmt.Errorf("failed to get GOCACHE: %w", err)
341+
}
342+
cacheDir := strings.TrimSpace(string(out))
343+
args = append(args,
344+
"-v", fmt.Sprintf("%s:%s", cacheDir, buildCacheLocation),
345+
)
338346
}
339-
cacheDir := strings.TrimSpace(string(out))
340-
buildCacheMountPoint := "/tmp/.cache/go-build"
341-
args = append(args, "-v", fmt.Sprintf("%s:%s", cacheDir, buildCacheMountPoint))
342347

343348
// Mount /opt/git-mirrors (if present) to resolve git alternates in CI
344349
if _, err := os.Stat("/opt/git-mirrors"); err == nil {
@@ -356,7 +361,7 @@ func (b GolangCrossBuilder) Build() error {
356361
args = append(args,
357362
"--rm",
358363
"--env", "GOFLAGS=-mod=readonly",
359-
"--env", fmt.Sprintf("GOCACHE=%s", buildCacheMountPoint),
364+
"--env", fmt.Sprintf("GOCACHE=%s", buildCacheLocation), // ensure this is writable by the user
360365
"--env", "MAGEFILE_VERBOSE="+verbose,
361366
"--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""),
362367
"--env", fmt.Sprintf("SNAPSHOT=%v", Snapshot),

dev-tools/mage/settings.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ var (
7474
// the crossbuild images at /go/pkg/mod, read-only, when set to true.
7575
CrossBuildMountModcache = EnvOr("CROSSBUILD_MOUNT_MODCACHE", "true") == "true"
7676

77+
// CrossBuildMountBuildCache mounts the Go build cache into golang-crossbuild containers
78+
CrossBuildMountBuildCache = EnvOr("CROSSBUILD_MOUNT_GOCACHE", "true") == "true"
79+
7780
BeatName = EnvOr("BEAT_NAME", defaultName)
7881
BeatServiceName = EnvOr("BEAT_SERVICE_NAME", BeatName)
7982
BeatIndexPrefix = EnvOr("BEAT_INDEX_PREFIX", BeatName)

0 commit comments

Comments
 (0)