Skip to content

Commit 4528491

Browse files
mergify[bot]swiatekmv1v
authored
Mount Go build cache into crossbuild container (#9094) (#9959)
* Mount Go build cache into crossbuild container * Run crossbuild container as host user * Update dev-tools/mage/crossbuild.go * Fix formatting * Control mounting mod cache with env variable * Control mounting build cache with env variable * Use a volume for Go build cache --------- (cherry picked from commit fd4de6b) Co-authored-by: Mikołaj Świątek <mail@mikolajswiatek.com> Co-authored-by: Victor Martinez <victormartinezrubio@gmail.com>
1 parent 6c47528 commit 4528491

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

dev-tools/mage/clean.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var DefaultCleanPaths = []string{
2424
"_meta/kibana/7/index-pattern/{{.BeatName}}.json",
2525
}
2626

27-
// Clean clean generated build artifacts.
27+
// Clean clean generated build artifacts and caches.
2828
func Clean(pathLists ...[]string) error {
2929
if len(pathLists) == 0 {
3030
pathLists = [][]string{DefaultCleanPaths}
@@ -37,5 +37,8 @@ func Clean(pathLists ...[]string) error {
3737
}
3838
}
3939
}
40+
if CrossBuildMountBuildCache {
41+
return sh.Run("docker", "volume", "rm", "-f", CrossBuildBuildCacheVolumeName)
42+
}
4043
return nil
4144
}

dev-tools/mage/crossbuild.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ func (b GolangCrossBuilder) Build() error {
270270
return fmt.Errorf("failed to determine repo root and package sub dir: %w", err)
271271
}
272272

273+
uid := os.Getuid()
274+
gid := os.Getgid()
275+
273276
mountPoint := filepath.ToSlash(filepath.Join("/go", "src", repoInfo.CanonicalRootImportPath))
274277
// use custom dir for build if given, subdir if not:
275278
cwd := repoInfo.SubDir
@@ -311,8 +314,8 @@ func (b GolangCrossBuilder) Build() error {
311314

312315
if runtime.GOOS != "windows" {
313316
args = append(args,
314-
"--env", "EXEC_UID="+strconv.Itoa(os.Getuid()),
315-
"--env", "EXEC_GID="+strconv.Itoa(os.Getgid()),
317+
"--env", fmt.Sprintf("EXEC_UID=%d", uid),
318+
"--env", fmt.Sprintf("EXEC_GID=%d", gid),
316319
)
317320
}
318321
if versionQualified {
@@ -324,6 +327,14 @@ func (b GolangCrossBuilder) Build() error {
324327
args = append(args, "-v", hostDir+":/go/pkg/mod:ro")
325328
}
326329

330+
buildCacheLocation := "/tmp/.cache/go-build"
331+
if CrossBuildMountBuildCache {
332+
// Mount the go build cache volume into the container.
333+
args = append(args,
334+
"-v", fmt.Sprintf("%s:%s", CrossBuildBuildCacheVolumeName, buildCacheLocation),
335+
)
336+
}
337+
327338
// Mount /opt/git-mirrors (if present) to resolve git alternates in CI
328339
if _, err := os.Stat("/opt/git-mirrors"); err == nil {
329340
args = append(args, "-v", "/opt/git-mirrors:/opt/git-mirrors:ro")
@@ -340,6 +351,7 @@ func (b GolangCrossBuilder) Build() error {
340351
args = append(args,
341352
"--rm",
342353
"--env", "GOFLAGS=-mod=readonly",
354+
"--env", fmt.Sprintf("GOCACHE=%s", buildCacheLocation), // ensure this is writable by the user
343355
"--env", "MAGEFILE_VERBOSE="+verbose,
344356
"--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""),
345357
"--env", fmt.Sprintf("SNAPSHOT=%v", Snapshot),

dev-tools/mage/settings.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ var (
7272

7373
// CrossBuildMountModcache mounts $GOPATH/pkg/mod into
7474
// the crossbuild images at /go/pkg/mod, read-only, when set to true.
75-
CrossBuildMountModcache = true
75+
CrossBuildMountModcache = EnvOr("CROSSBUILD_MOUNT_MODCACHE", "true") == "true"
76+
77+
// CrossBuildMountBuildCache mounts the Go build cache into golang-crossbuild containers
78+
CrossBuildMountBuildCache = EnvOr("CROSSBUILD_MOUNT_GOCACHE", "true") == "true"
79+
CrossBuildBuildCacheVolumeName = "elastic-agent-crossbuild-build-cache"
7680

7781
BeatName = EnvOr("BEAT_NAME", defaultName)
7882
BeatServiceName = EnvOr("BEAT_SERVICE_NAME", BeatName)

0 commit comments

Comments
 (0)