Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 0 additions & 11 deletions dev-tools/mage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,6 @@ func GolangCrossBuild(params BuildArgs) error {
"only be executed within the golang-crossbuild docker environment")
}

defer DockerChown(filepath.Join(params.OutputDir, params.Name+binaryExtension(GOOS)))
defer DockerChown(filepath.Join(params.OutputDir))

mountPoint, err := ElasticBeatsDir()
if err != nil {
return err
}
if err := sh.Run("git", "config", "--global", "--add", "safe.directory", mountPoint); err != nil {
return err
}

Comment thread
swiatekm marked this conversation as resolved.
return Build(params)
}

Expand Down
23 changes: 21 additions & 2 deletions dev-tools/mage/crossbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go/build"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -274,6 +275,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 @@ -315,8 +319,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 @@ -328,6 +332,19 @@ 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 into the container.
out, err := exec.Command("go", "env", "GOCACHE").Output()
if err != nil {
return fmt.Errorf("failed to get GOCACHE: %w", err)
}
cacheDir := strings.TrimSpace(string(out))
args = append(args,
"-v", fmt.Sprintf("%s:%s", cacheDir, 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 @@ -344,12 +361,14 @@ 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),
"--env", fmt.Sprintf("DEV=%v", DevBuild),
"--env", fmt.Sprintf("EXTERNAL=%v", ExternalBuild),
"--env", fmt.Sprintf("FIPS=%v", FIPSBuild),
"--user", fmt.Sprintf("%d:%d", uid, gid),
Comment thread
swiatekm marked this conversation as resolved.
Outdated
"-v", repoInfo.RootDir+":"+mountPoint,
"-w", workDir,
image,
Expand Down
5 changes: 4 additions & 1 deletion dev-tools/mage/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ 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"

BeatName = EnvOr("BEAT_NAME", defaultName)
BeatServiceName = EnvOr("BEAT_SERVICE_NAME", BeatName)
Expand Down
Loading