Skip to content

Commit c1084a0

Browse files
mergify[bot]swiatekmv1v
authored
Mount Go build cache into crossbuild container (#9094) (#9960)
* 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 2c0d4c4 commit c1084a0

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
@@ -25,7 +25,7 @@ var DefaultCleanPaths = []string{
2525
"_meta/kibana/7/index-pattern/{{.BeatName}}.json",
2626
}
2727

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

dev-tools/mage/crossbuild.go

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

277+
uid := os.Getuid()
278+
gid := os.Getgid()
279+
277280
mountPoint := filepath.ToSlash(filepath.Join("/go", "src", repoInfo.CanonicalRootImportPath))
278281
// use custom dir for build if given, subdir if not:
279282
cwd := repoInfo.SubDir
@@ -315,8 +318,8 @@ func (b GolangCrossBuilder) Build() error {
315318

316319
if runtime.GOOS != "windows" {
317320
args = append(args,
318-
"--env", "EXEC_UID="+strconv.Itoa(os.Getuid()),
319-
"--env", "EXEC_GID="+strconv.Itoa(os.Getgid()),
321+
"--env", fmt.Sprintf("EXEC_UID=%d", uid),
322+
"--env", fmt.Sprintf("EXEC_GID=%d", gid),
320323
)
321324
}
322325
if versionQualified {
@@ -328,6 +331,14 @@ func (b GolangCrossBuilder) Build() error {
328331
args = append(args, "-v", hostDir+":/go/pkg/mod:ro")
329332
}
330333

334+
buildCacheLocation := "/tmp/.cache/go-build"
335+
if CrossBuildMountBuildCache {
336+
// Mount the go build cache volume into the container.
337+
args = append(args,
338+
"-v", fmt.Sprintf("%s:%s", CrossBuildBuildCacheVolumeName, buildCacheLocation),
339+
)
340+
}
341+
331342
// Mount /opt/git-mirrors (if present) to resolve git alternates in CI
332343
if _, err := os.Stat("/opt/git-mirrors"); err == nil {
333344
args = append(args, "-v", "/opt/git-mirrors:/opt/git-mirrors:ro")
@@ -344,6 +355,7 @@ func (b GolangCrossBuilder) Build() error {
344355
args = append(args,
345356
"--rm",
346357
"--env", "GOFLAGS=-mod=readonly",
358+
"--env", fmt.Sprintf("GOCACHE=%s", buildCacheLocation), // ensure this is writable by the user
347359
"--env", "MAGEFILE_VERBOSE="+verbose,
348360
"--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""),
349361
"--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)