@@ -360,16 +360,31 @@ func (Build) Clean() error {
360360 if err := os .RemoveAll (absBuildDir ); err != nil {
361361 return fmt .Errorf ("cannot remove build dir '%s': %w" , absBuildDir , err )
362362 }
363+ return nil
364+ }
363365
364- testBinariesPath , err := getTestBinariesPath ()
366+ // CleanKeepDistributions deletes the build directory but keeps the distributions directory.
367+ func (Build ) CleanKeepDistributions () error {
368+ absBuildDir , err := filepath .Abs (buildDir )
365369 if err != nil {
366- return fmt .Errorf ("cannot remove test binaries : %w" , err )
370+ return fmt .Errorf ("cannot get absolute path of build dir : %w" , err )
367371 }
368372
369- if mg .Verbose () {
370- fmt .Println ("removed" , absBuildDir )
371- for _ , b := range testBinariesPath {
372- fmt .Println ("removed" , b )
373+ entries , err := os .ReadDir (absBuildDir )
374+ if err != nil {
375+ if os .IsNotExist (err ) {
376+ return nil // Directory doesn't exist, nothing to clean
377+ }
378+ return fmt .Errorf ("cannot read build dir '%s': %w" , absBuildDir , err )
379+ }
380+
381+ for _ , entry := range entries {
382+ if entry .Name () == "distributions" {
383+ continue
384+ }
385+ entryPath := filepath .Join (absBuildDir , entry .Name ())
386+ if err := os .RemoveAll (entryPath ); err != nil {
387+ return fmt .Errorf ("cannot remove '%s': %w" , entryPath , err )
373388 }
374389 }
375390
@@ -580,8 +595,9 @@ func Package(ctx context.Context) error {
580595 panic ("elastic-agent package is expected to build at least one platform package" )
581596 }
582597
583- // needs elastic-agent-core built first
584- mg .Deps (PackageAgentCore )
598+ // needs elastic-agent-core built first (removes any extra files in build to ensure the following steps don't
599+ // rely on any files that elastic-agent-core placed during the build process)
600+ mg .SerialDeps (PackageAgentCore , Build .CleanKeepDistributions )
585601
586602 // switch to the main package target
587603 mage .UseElasticAgentPackaging ()
@@ -1175,14 +1191,16 @@ func runAgent(ctx context.Context, env map[string]string) error {
11751191func packageAgent (ctx context.Context , dependenciesVersion string , manifestResponse * manifest.Build ) error {
11761192 fmt .Println ("--- Package elastic-agent" )
11771193
1194+ var coreVersion string
1195+ if beatVersion , found := os .LookupEnv ("BEAT_VERSION" ); ! found {
1196+ coreVersion = bversion .GetDefaultVersion ()
1197+ } else {
1198+ coreVersion = beatVersion
1199+ }
1200+ // add the snapshot suffix if needed
1201+ coreVersion += devtools .SnapshotSuffix ()
11781202 if dependenciesVersion == "" {
1179- if beatVersion , found := os .LookupEnv ("BEAT_VERSION" ); ! found {
1180- dependenciesVersion = bversion .GetDefaultVersion ()
1181- } else {
1182- dependenciesVersion = beatVersion
1183- }
1184- // add the snapshot suffix if needed
1185- dependenciesVersion += devtools .SnapshotSuffix ()
1203+ dependenciesVersion = coreVersion
11861204 }
11871205 log .Printf ("Packaging with dependenciesVersion: %s" , dependenciesVersion )
11881206
@@ -1219,7 +1237,7 @@ func packageAgent(ctx context.Context, dependenciesVersion string, manifestRespo
12191237 flattenDependencies (mage .Platforms .Names (), dependenciesVersion , archivePath , dropPath , flatPath , manifestResponse , dependencies )
12201238
12211239 // extract elastic-agent-core to be used for packaging
1222- err = extractAgentCoreForPackage (ctx , manifestResponse , dependenciesVersion )
1240+ err = extractAgentCoreForPackage (ctx , manifestResponse , coreVersion )
12231241 if err != nil {
12241242 return err
12251243 }
@@ -1526,6 +1544,10 @@ func PackageUsingDRA(ctx context.Context) error {
15261544 manifestURL := os .Getenv (mage .ManifestUrlEnvVar )
15271545 if manifestURL == "" {
15281546 fmt .Println ("NOTICE: No MANIFEST_URL was provided, using elastic-agent-core packages from build/distributions." )
1547+ dependenciesVersion = os .Getenv ("DRA_VERSION" )
1548+ if mage .Snapshot && ! strings .HasSuffix (dependenciesVersion , devtools .SnapshotSuffix ()) {
1549+ dependenciesVersion += devtools .SnapshotSuffix ()
1550+ }
15291551 } else {
15301552 var parsedVersion * version.ParsedSemVer
15311553 manifestResponse , parsedVersion , err = downloadManifestAndSetVersion (ctx , devtools .ManifestURL )
0 commit comments