Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dev-tools/mage/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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}
Expand All @@ -37,5 +37,8 @@ func Clean(pathLists ...[]string) error {
}
}
}
if CrossBuildMountBuildCache {
return sh.Run("docker", "volume", "rm", "-f", CrossBuildBuildCacheVolumeName)
}
return nil
}
16 changes: 14 additions & 2 deletions dev-tools/mage/crossbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,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
Expand Down Expand Up @@ -311,8 +314,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 {
Expand All @@ -324,6 +327,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")
Expand All @@ -340,6 +351,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),
Expand Down
6 changes: 5 additions & 1 deletion dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading