diff --git a/dev-tools/mage/clean.go b/dev-tools/mage/clean.go index 3a0ef7288c9..b291a703053 100644 --- a/dev-tools/mage/clean.go +++ b/dev-tools/mage/clean.go @@ -25,7 +25,7 @@ var DefaultCleanPaths = []string{ "_meta/kibana/7/index-pattern/{{.BeatName}}.json", } -// Clean clean generated build artifacts. +// Clean clean generated build artifacts and caches. func Clean(pathLists ...[]string) error { if len(pathLists) == 0 { pathLists = [][]string{DefaultCleanPaths} @@ -38,5 +38,8 @@ func Clean(pathLists ...[]string) error { } } } + if CrossBuildMountBuildCache { + return sh.Run("docker", "volume", "rm", "-f", CrossBuildBuildCacheVolumeName) + } return nil } diff --git a/dev-tools/mage/crossbuild.go b/dev-tools/mage/crossbuild.go index 94a580f3393..c54cfb0f76b 100644 --- a/dev-tools/mage/crossbuild.go +++ b/dev-tools/mage/crossbuild.go @@ -274,6 +274,9 @@ func (b GolangCrossBuilder) Build() error { return fmt.Errorf("failed to determine repo root and package sub dir: %w", err) } + uid := os.Getuid() + gid := os.Getgid() + mountPoint := filepath.ToSlash(filepath.Join("/go", "src", repoInfo.CanonicalRootImportPath)) // use custom dir for build if given, subdir if not: cwd := repoInfo.SubDir @@ -315,8 +318,8 @@ func (b GolangCrossBuilder) Build() error { if runtime.GOOS != "windows" { args = append(args, - "--env", "EXEC_UID="+strconv.Itoa(os.Getuid()), - "--env", "EXEC_GID="+strconv.Itoa(os.Getgid()), + "--env", fmt.Sprintf("EXEC_UID=%d", uid), + "--env", fmt.Sprintf("EXEC_GID=%d", gid), ) } if versionQualified { @@ -328,6 +331,14 @@ func (b GolangCrossBuilder) Build() error { args = append(args, "-v", hostDir+":/go/pkg/mod:ro") } + buildCacheLocation := "/tmp/.cache/go-build" + if CrossBuildMountBuildCache { + // Mount the go build cache volume into the container. + args = append(args, + "-v", fmt.Sprintf("%s:%s", CrossBuildBuildCacheVolumeName, buildCacheLocation), + ) + } + // Mount /opt/git-mirrors (if present) to resolve git alternates in CI if _, err := os.Stat("/opt/git-mirrors"); err == nil { args = append(args, "-v", "/opt/git-mirrors:/opt/git-mirrors:ro") @@ -344,6 +355,7 @@ func (b GolangCrossBuilder) Build() error { args = append(args, "--rm", "--env", "GOFLAGS=-mod=readonly", + "--env", fmt.Sprintf("GOCACHE=%s", buildCacheLocation), // ensure this is writable by the user "--env", "MAGEFILE_VERBOSE="+verbose, "--env", "MAGEFILE_TIMEOUT="+EnvOr("MAGEFILE_TIMEOUT", ""), "--env", fmt.Sprintf("SNAPSHOT=%v", Snapshot), diff --git a/dev-tools/mage/settings.go b/dev-tools/mage/settings.go index 17bf18e225f..69761565315 100644 --- a/dev-tools/mage/settings.go +++ b/dev-tools/mage/settings.go @@ -72,7 +72,11 @@ var ( // CrossBuildMountModcache mounts $GOPATH/pkg/mod into // the crossbuild images at /go/pkg/mod, read-only, when set to true. - CrossBuildMountModcache = true + CrossBuildMountModcache = EnvOr("CROSSBUILD_MOUNT_MODCACHE", "true") == "true" + + // CrossBuildMountBuildCache mounts the Go build cache into golang-crossbuild containers + CrossBuildMountBuildCache = EnvOr("CROSSBUILD_MOUNT_GOCACHE", "true") == "true" + CrossBuildBuildCacheVolumeName = "elastic-agent-crossbuild-build-cache" BeatName = EnvOr("BEAT_NAME", defaultName) BeatServiceName = EnvOr("BEAT_SERVICE_NAME", BeatName)