Merge pull request #38 from tonistiigi/bake-defaults

bake: fix dockerfile and context defaults
pull/40/head
Tibor Vass 6 years ago committed by GitHub
commit 02d0474e04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -109,9 +109,9 @@ func (c Config) setOverrides(v []string) error {
switch keys[1] { switch keys[1] {
case "context": case "context":
t.Context = parts[1] t.Context = &parts[1]
case "dockerfile": case "dockerfile":
t.Dockerfile = parts[1] t.Dockerfile = &parts[1]
case "args": case "args":
if len(keys) != 3 { if len(keys) != 3 {
return errors.Errorf("invalid key %s, args requires name", parts[0]) return errors.Errorf("invalid key %s, args requires name", parts[0])
@ -170,7 +170,19 @@ func (c Config) group(name string, visited map[string]struct{}) []string {
} }
func (c Config) ResolveTarget(name string) (*Target, error) { func (c Config) ResolveTarget(name string) (*Target, error) {
return c.target(name, map[string]struct{}{}) t, err := c.target(name, map[string]struct{}{})
if err != nil {
return nil, err
}
if t.Context == nil {
s := "."
t.Context = &s
}
if t.Dockerfile == nil {
s := "Dockerfile"
t.Dockerfile = &s
}
return t, nil
} }
func (c Config) target(name string, visited map[string]struct{}) (*Target, error) { func (c Config) target(name string, visited map[string]struct{}) (*Target, error) {
@ -205,8 +217,8 @@ type Group struct {
type Target struct { type Target struct {
Inherits []string `json:"inherits,omitempty"` Inherits []string `json:"inherits,omitempty"`
Context string `json:"context,omitempty"` Context *string `json:"context,omitempty"`
Dockerfile string `json:"dockerfile,omitempty"` Dockerfile *string `json:"dockerfile,omitempty"`
Args map[string]string `json:"args,omitempty"` Args map[string]string `json:"args,omitempty"`
Labels map[string]string `json:"labels,omitempty"` Labels map[string]string `json:"labels,omitempty"`
Tags []string `json:"tags,omitempty"` Tags []string `json:"tags,omitempty"`
@ -238,17 +250,26 @@ func TargetsToBuildOpt(m map[string]Target) (map[string]build.Options, error) {
} }
func toBuildOpt(t Target) (*build.Options, error) { func toBuildOpt(t Target) (*build.Options, error) {
if t.Context == "-" { if v := t.Context; v != nil && *v == "-" {
return nil, errors.Errorf("context from stdin not allowed in bake") return nil, errors.Errorf("context from stdin not allowed in bake")
} }
if t.Dockerfile == "-" { if v := t.Dockerfile; v != nil && *v == "-" {
return nil, errors.Errorf("dockerfile from stdin not allowed in bake") return nil, errors.Errorf("dockerfile from stdin not allowed in bake")
} }
contextPath := "."
if t.Context != nil {
contextPath = *t.Context
}
dockerfilePath := "Dockerfile"
if t.Dockerfile != nil {
dockerfilePath = *t.Dockerfile
}
bo := &build.Options{ bo := &build.Options{
Inputs: build.Inputs{ Inputs: build.Inputs{
ContextPath: t.Context, ContextPath: contextPath,
DockerfilePath: t.Dockerfile, DockerfilePath: dockerfilePath,
}, },
Tags: t.Tags, Tags: t.Tags,
BuildArgs: t.Args, BuildArgs: t.Args,
@ -295,17 +316,14 @@ func toBuildOpt(t Target) (*build.Options, error) {
} }
func defaultTarget() Target { func defaultTarget() Target {
return Target{ return Target{}
Context: ".",
Dockerfile: "Dockerfile",
}
} }
func merge(t1, t2 Target) Target { func merge(t1, t2 Target) Target {
if t2.Context != "" { if t2.Context != nil {
t1.Context = t2.Context t1.Context = t2.Context
} }
if t2.Dockerfile != "" { if t2.Dockerfile != nil {
t1.Dockerfile = t2.Dockerfile t1.Dockerfile = t2.Dockerfile
} }
for k, v := range t2.Args { for k, v := range t2.Args {

@ -0,0 +1,69 @@
package bake
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadTargets(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "bake")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
fp := filepath.Join(tmpdir, "config.hcl")
err = ioutil.WriteFile(fp, []byte(`
target "dep" {
}
target "webapp" {
dockerfile = "Dockerfile.webapp"
inherits = ["dep"]
}`), 0600)
require.NoError(t, err)
ctx := context.TODO()
m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, nil)
require.NoError(t, err)
require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile)
require.Equal(t, ".", *m["webapp"].Context)
}
func TestReadTargetsCompose(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "bake")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
fp := filepath.Join(tmpdir, "docker-compose.yml")
err = ioutil.WriteFile(fp, []byte(`
version: "3"
services:
db:
build: .
command: ./entrypoint.sh
image: docker.io/tonistiigi/db
webapp:
build:
dockerfile: Dockerfile.webapp
args:
buildno: 1
`), 0600)
require.NoError(t, err)
ctx := context.TODO()
m, err := ReadTargets(ctx, []string{fp}, []string{"default"}, nil)
require.NoError(t, err)
require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile)
require.Equal(t, ".", *m["webapp"].Context)
}

@ -34,9 +34,19 @@ func ParseCompose(dt []byte) (*Config, error) {
for _, s := range cfg.Services { for _, s := range cfg.Services {
g.Targets = append(g.Targets, s.Name) g.Targets = append(g.Targets, s.Name)
var contextPathP *string
if s.Build.Context != "" {
contextPath := s.Build.Context
contextPathP = &contextPath
}
var dockerfilePathP *string
if s.Build.Dockerfile != "" {
dockerfilePath := s.Build.Dockerfile
dockerfilePathP = &dockerfilePath
}
t := Target{ t := Target{
Context: s.Build.Context, Context: contextPathP,
Dockerfile: s.Build.Dockerfile, Dockerfile: dockerfilePathP,
Labels: s.Build.Labels, Labels: s.Build.Labels,
Args: toMap(s.Build.Args), Args: toMap(s.Build.Args),
CacheFrom: s.Build.CacheFrom, CacheFrom: s.Build.CacheFrom,

@ -32,10 +32,10 @@ services:
require.Equal(t, []string{"db", "webapp"}, c.Group["default"].Targets) require.Equal(t, []string{"db", "webapp"}, c.Group["default"].Targets)
require.Equal(t, 2, len(c.Target)) require.Equal(t, 2, len(c.Target))
require.Equal(t, "./db", c.Target["db"].Context) require.Equal(t, "./db", *c.Target["db"].Context)
require.Equal(t, "./dir", c.Target["webapp"].Context) require.Equal(t, "./dir", *c.Target["webapp"].Context)
require.Equal(t, "Dockerfile-alternate", c.Target["webapp"].Dockerfile) require.Equal(t, "Dockerfile-alternate", *c.Target["webapp"].Dockerfile)
require.Equal(t, 1, len(c.Target["webapp"].Args)) require.Equal(t, 1, len(c.Target["webapp"].Args))
require.Equal(t, "123", c.Target["webapp"].Args["buildno"]) require.Equal(t, "123", c.Target["webapp"].Args["buildno"])
} }

@ -47,7 +47,7 @@ func TestParseHCL(t *testing.T) {
require.Equal(t, []string{"db", "webapp"}, c.Group["default"].Targets) require.Equal(t, []string{"db", "webapp"}, c.Group["default"].Targets)
require.Equal(t, 4, len(c.Target)) require.Equal(t, 4, len(c.Target))
require.Equal(t, "./db", c.Target["db"].Context) require.Equal(t, "./db", *c.Target["db"].Context)
require.Equal(t, 1, len(c.Target["webapp"].Args)) require.Equal(t, 1, len(c.Target["webapp"].Args))
require.Equal(t, "123", c.Target["webapp"].Args["buildno"]) require.Equal(t, "123", c.Target["webapp"].Args["buildno"])

@ -420,7 +420,7 @@ func LoadInputs(inp Inputs, target *client.SolveOpt) (func(), error) {
} }
func notSupported(d driver.Driver, f driver.Feature) error { func notSupported(d driver.Driver, f driver.Feature) error {
return errors.Errorf("%s feature is currently not supported for %s driver. Please switch to a different driver (eg. \"docker buildx create\")", f, d.Factory().Name()) return errors.Errorf("%s feature is currently not supported for %s driver. Please switch to a different driver (eg. \"docker buildx create --use\")", f, d.Factory().Name())
} }
func newDockerLoader(ctx context.Context, d DockerAPI, name string, mw *progress.MultiWriter) (io.WriteCloser, func(), error) { func newDockerLoader(ctx context.Context, d DockerAPI, name string, mw *progress.MultiWriter) (io.WriteCloser, func(), error) {

Loading…
Cancel
Save