diff --git a/forge/cli/pkg/earthly/earthly.go b/forge/cli/pkg/earthly/earthly.go index bf4aad31..e4cb02a3 100644 --- a/forge/cli/pkg/earthly/earthly.go +++ b/forge/cli/pkg/earthly/earthly.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" "os" + "path" "regexp" "runtime" "strconv" @@ -118,7 +119,10 @@ func (e *EarthlyExecutor) buildArguments(platform string) []string { earthlyArgs = append(earthlyArgs, e.earthlyArgs...) - if e.opts.artifact != "" { + // If we have an artifact path and multiple platforms, we need to append the platform to the artifact path to avoid conflicts. + if e.opts.artifact != "" && len(e.opts.platforms) > 1 { + earthlyArgs = append(earthlyArgs, "--artifact", fmt.Sprintf("%s+%s/*", e.earthfile, e.target), path.Join(e.opts.artifact, platform)+"/") + } else if e.opts.artifact != "" && len(e.opts.platforms) <= 1 { earthlyArgs = append(earthlyArgs, "--artifact", fmt.Sprintf("%s+%s/*", e.earthfile, e.target), e.opts.artifact) } else { earthlyArgs = append(earthlyArgs, fmt.Sprintf("%s+%s", e.earthfile, e.target)) diff --git a/forge/cli/pkg/earthly/earthly_test.go b/forge/cli/pkg/earthly/earthly_test.go index 9de33b2b..69d38ea3 100644 --- a/forge/cli/pkg/earthly/earthly_test.go +++ b/forge/cli/pkg/earthly/earthly_test.go @@ -165,6 +165,16 @@ func TestEarthlyExecutor_buildArguments(t *testing.T) { platform: getNativePlatform(), expect: []string{"--artifact", "/test/dir+foo/*", "test/"}, }, + { + name: "with artifact and platforms", + e: NewEarthlyExecutor("/test/dir", "foo", nil, secrets.SecretStore{}, + testutils.NewNoopLogger(), + WithPlatforms("foo", "bar"), + WithArtifact("test"), + ), + platform: "foo", + expect: []string{"--platform", "foo", "--artifact", "/test/dir+foo/*", "test/foo/"}, + }, { name: "with ci", e: NewEarthlyExecutor("/test/dir", "foo", nil, secrets.SecretStore{},