bake: compose parser should only error if there are neither build nor image fields

Signed-off-by: Tibor Vass <tibor@docker.com>
pull/87/head
Tibor Vass 6 years ago
parent 4b2666b9d6
commit b741350afd

@ -2,6 +2,7 @@ package bake
import ( import (
"fmt" "fmt"
"reflect"
"github.com/docker/cli/cli/compose/loader" "github.com/docker/cli/cli/compose/loader"
composetypes "github.com/docker/cli/cli/compose/types" composetypes "github.com/docker/cli/cli/compose/types"
@ -28,6 +29,7 @@ func ParseCompose(dt []byte) (*Config, error) {
} }
var c Config var c Config
var zeroBuildConfig composetypes.BuildConfig
if len(cfg.Services) > 0 { if len(cfg.Services) > 0 {
c.Group = map[string]Group{} c.Group = map[string]Group{}
c.Target = map[string]Target{} c.Target = map[string]Target{}
@ -36,6 +38,14 @@ func ParseCompose(dt []byte) (*Config, error) {
for _, s := range cfg.Services { for _, s := range cfg.Services {
if reflect.DeepEqual(s.Build, zeroBuildConfig) {
// if not make sure they're setting an image or it's invalid d-c.yml
if s.Image == "" {
return nil, fmt.Errorf("compose file invalid: service %s has neither an image nor a build context specified. At least one must be provided.", s.Name)
}
continue
}
var contextPathP *string var contextPathP *string
if s.Build.Context != "" { if s.Build.Context != "" {
contextPath := s.Build.Context contextPath := s.Build.Context
@ -46,14 +56,6 @@ func ParseCompose(dt []byte) (*Config, error) {
dockerfilePath := s.Build.Dockerfile dockerfilePath := s.Build.Dockerfile
dockerfilePathP = &dockerfilePath dockerfilePathP = &dockerfilePath
} }
// Check if there's actually a dockerfile mentioned
if dockerfilePathP == nil && contextPathP == nil {
// if not make sure they're setting an image or it's invalid d-c.yml
if s.Image == "" {
return nil, fmt.Errorf("Compose file invalid: Service %s has neither an image nor a build context specified. At least one must be provided.", s.Name)
}
break
}
g.Targets = append(g.Targets, s.Name) g.Targets = append(g.Targets, s.Name)
t := Target{ t := Target{
Context: contextPathP, Context: contextPathP,

@ -53,7 +53,6 @@ services:
c, err := ParseCompose(dt) c, err := ParseCompose(dt)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(c.Group)) require.Equal(t, 1, len(c.Group))
} }
func TestParseComposeTarget(t *testing.T) { func TestParseComposeTarget(t *testing.T) {
@ -78,7 +77,7 @@ services:
require.Equal(t, "webapp", *c.Target["webapp"].Target) require.Equal(t, "webapp", *c.Target["webapp"].Target)
} }
func TestBogusCompose(t *testing.T) { func TestComposeBuildWithoutContext(t *testing.T) {
var dt = []byte(` var dt = []byte(`
version: "3.7" version: "3.7"
@ -92,6 +91,27 @@ services:
target: webapp target: webapp
`) `)
c, err := ParseCompose(dt)
require.NoError(t, err)
require.Equal(t, "db", *c.Target["db"].Target)
require.Equal(t, "webapp", *c.Target["webapp"].Target)
}
func TestBogusCompose(t *testing.T) {
var dt = []byte(`
version: "3.7"
services:
db:
labels:
- "foo"
webapp:
build:
context: .
target: webapp
`)
_, err := ParseCompose(dt) _, err := ParseCompose(dt)
require.Error(t, err) require.Error(t, err)
require.Contains(t, err.Error(), "has neither an image nor a build context specified. At least one must be provided")
} }

Loading…
Cancel
Save