Skip to content

Commit

Permalink
keep the platform defined at service level during build if no build p…
Browse files Browse the repository at this point in the history
…latforms provided

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
  • Loading branch information
glours committed Sep 14, 2022
1 parent c4d79e6 commit be2a37a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,15 @@ func addPlatforms(project *types.Project, service types.ServiceConfig) ([]specs.
}

if service.Platform != "" && !utils.StringContains(service.Build.Platforms, service.Platform) {
return nil, fmt.Errorf("service.platform should be part of the service.build.platforms: %q", service.Platform)
if len(service.Build.Platforms) > 0 {
return nil, fmt.Errorf("service.platform should be part of the service.build.platforms: %q", service.Platform)
}
// User defined a service platform and no build platforms, so we should keep the one define on the service level
p, err := platforms.Parse(service.Platform)
if err != nil {
return nil, err
}
plats = append(plats, p)
}

for _, buildPlatform := range service.Build.Platforms {
Expand Down
8 changes: 8 additions & 0 deletions pkg/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
res.Assert(t, icmd.Expected{Out: "I am building for linux/amd64"})
assert.Assert(t, !strings.Contains(res.Stdout(), "I am building for linux/arm64"))
})

t.Run("use service platform value when no build platforms defined ", func(t *testing.T) {
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms",
"-f", "fixtures/build-test/platforms/compose-service-platform-and-no-build-platforms.yaml", "build")
assert.NilError(t, res.Error, res.Stderr())
res.Assert(t, icmd.Expected{Out: "I am building for linux/386"})
})

}

func TestBuildPlatformsStandardErrors(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
platforms:
image: build-test-platform:test
platform: linux/386
build:
context: .

0 comments on commit be2a37a

Please sign in to comment.