keep the platform defined at service level during build if no build platforms provided

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2022-09-14 23:20:31 +02:00
parent c4d79e60b6
commit 0e975262da
No known key found for this signature in database
3 changed files with 20 additions and 1 deletions

View File

@ -363,8 +363,13 @@ func addPlatforms(project *types.Project, service types.ServiceConfig) ([]specs.
}
if service.Platform != "" && !utils.StringContains(service.Build.Platforms, 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)
return append(plats, p), err
}
for _, buildPlatform := range service.Build.Platforms {
p, err := platforms.Parse(buildPlatform)

View File

@ -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) {

View File

@ -0,0 +1,6 @@
services:
platforms:
image: build-test-platform:test
platform: linux/386
build:
context: .