Skip to content

Commit c9c3e20

Browse files
committed
Use a volume for Go build cache
1 parent 3374450 commit c9c3e20

4 files changed

Lines changed: 19 additions & 11 deletions

File tree

dev-tools/mage/build.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ func GolangCrossBuild(params BuildArgs) error {
166166
"only be executed within the golang-crossbuild docker environment")
167167
}
168168

169+
defer DockerChown(filepath.Join(params.OutputDir, params.Name+binaryExtension(GOOS)))
170+
defer DockerChown(filepath.Join(params.OutputDir))
171+
172+
mountPoint, err := ElasticBeatsDir()
173+
if err != nil {
174+
return err
175+
}
176+
if err := sh.Run("git", "config", "--global", "--add", "safe.directory", mountPoint); err != nil {
177+
return err
178+
}
179+
169180
return Build(params)
170181
}
171182

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: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"go/build"
1010
"log"
1111
"os"
12-
"os/exec"
1312
"path/filepath"
1413
"runtime"
1514
"strconv"
@@ -334,14 +333,9 @@ func (b GolangCrossBuilder) Build() error {
334333

335334
buildCacheLocation := "/tmp/.cache/go-build"
336335
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))
336+
// Mount the go build cache volume into the container.
343337
args = append(args,
344-
"-v", fmt.Sprintf("%s:%s", cacheDir, buildCacheLocation),
338+
"-v", fmt.Sprintf("%s:%s", CrossBuildBuildCacheVolumeName, buildCacheLocation),
345339
)
346340
}
347341

@@ -368,7 +362,6 @@ func (b GolangCrossBuilder) Build() error {
368362
"--env", fmt.Sprintf("DEV=%v", DevBuild),
369363
"--env", fmt.Sprintf("EXTERNAL=%v", ExternalBuild),
370364
"--env", fmt.Sprintf("FIPS=%v", FIPSBuild),
371-
"--user", fmt.Sprintf("%d:%d", uid, gid),
372365
"-v", repoInfo.RootDir+":"+mountPoint,
373366
"-w", workDir,
374367
image,

dev-tools/mage/settings.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ var (
7575
CrossBuildMountModcache = EnvOr("CROSSBUILD_MOUNT_MODCACHE", "true") == "true"
7676

7777
// CrossBuildMountBuildCache mounts the Go build cache into golang-crossbuild containers
78-
CrossBuildMountBuildCache = EnvOr("CROSSBUILD_MOUNT_GOCACHE", "true") == "true"
78+
CrossBuildMountBuildCache = EnvOr("CROSSBUILD_MOUNT_GOCACHE", "true") == "true"
79+
CrossBuildBuildCacheVolumeName = "elastic-agent-crossbuild-build-cache"
7980

8081
BeatName = EnvOr("BEAT_NAME", defaultName)
8182
BeatServiceName = EnvOr("BEAT_SERVICE_NAME", BeatName)

0 commit comments

Comments
 (0)