Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bake: support setting the context to null in bake #2906

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 58 additions & 32 deletions bake/bake.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bake

import (
"bytes"
"context"
"encoding"
"io"
Expand Down Expand Up @@ -638,10 +639,6 @@ func (c Config) ResolveTarget(name string, overrides map[string]map[string]Overr
return nil, err
}
t.Inherits = nil
if t.Context == nil {
s := "."
t.Context = &s
}
if t.Dockerfile == nil {
s := "Dockerfile"
t.Dockerfile = &s
Expand Down Expand Up @@ -702,7 +699,7 @@ type Target struct {

Annotations []string `json:"annotations,omitempty" hcl:"annotations,optional" cty:"annotations"`
Attest buildflags.Attests `json:"attest,omitempty" hcl:"attest,optional" cty:"attest"`
Context *string `json:"context,omitempty" hcl:"context,optional" cty:"context"`
Context **string `json:"context" hcl:"context,optional" cty:"context"`
Contexts map[string]string `json:"contexts,omitempty" hcl:"contexts,optional" cty:"contexts"`
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional" cty:"dockerfile"`
DockerfileInline *string `json:"dockerfile-inline,omitempty" hcl:"dockerfile-inline,optional" cty:"dockerfile-inline"`
Expand Down Expand Up @@ -761,11 +758,23 @@ func (t *Target) normalize() {
delete(t.Contexts, k)
}
}
if t.Context == nil {
s := t.ContextPath()
t.Context = &s
}
if len(t.Contexts) == 0 {
t.Contexts = nil
}
}

func (t *Target) ContextPath() *string {
if t.Context == nil {
p := "."
return &p
}
return (*string)(*t.Context)
}

func (t *Target) Merge(t2 *Target) {
if t2.Context != nil {
t.Context = t2.Context
Expand Down Expand Up @@ -866,7 +875,8 @@ func (t *Target) AddOverrides(overrides map[string]Override, ent *EntitlementCon
keys := strings.SplitN(key, ".", 2)
switch keys[0] {
case "context":
t.Context = &value
ptr := &value
t.Context = &ptr
case "dockerfile":
t.Dockerfile = &value
case "args":
Expand Down Expand Up @@ -1210,37 +1220,18 @@ func isLocalPath(p string) (string, bool) {
}

func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
if v := t.Context; v != nil && *v == "-" {
if v := t.ContextPath(); v != nil && *v == "-" {
return nil, errors.Errorf("context from stdin not allowed in bake")
}
if v := t.Dockerfile; v != nil && *v == "-" {
return nil, errors.Errorf("dockerfile from stdin not allowed in bake")
}

contextPath := "."
if t.Context != nil {
contextPath = *t.Context
}
if !strings.HasPrefix(contextPath, "cwd://") && !build.IsRemoteURL(contextPath) {
contextPath = path.Clean(contextPath)
}
dockerfilePath := "Dockerfile"
if t.Dockerfile != nil {
dockerfilePath = *t.Dockerfile
}
if !strings.HasPrefix(dockerfilePath, "cwd://") {
dockerfilePath = path.Clean(dockerfilePath)
}
bi := build.Inputs{}

bi := build.Inputs{
ContextPath: contextPath,
DockerfilePath: dockerfilePath,
NamedContexts: toNamedContexts(t.Contexts),
}
if t.DockerfileInline != nil {
bi.DockerfileInline = *t.DockerfileInline
}
setBuildOptContexts(&bi, t)
updateContext(&bi, inp)

if strings.HasPrefix(bi.DockerfilePath, "cwd://") {
// If Dockerfile is local for a remote invocation, we first check if
// it's not outside the working directory and then resolve it to an
Expand Down Expand Up @@ -1273,7 +1264,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
if strings.HasPrefix(bi.ContextPath, "cwd://") {
bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://"))
}
if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) {
if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && bi.DockerfilePath != "" && !path.IsAbs(bi.DockerfilePath) {
bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath)
}
for k, v := range bi.NamedContexts {
Expand All @@ -1282,8 +1273,6 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
}
}

t.Context = &bi.ContextPath

args := map[string]string{}
for k, v := range t.Args {
if v == nil {
Expand Down Expand Up @@ -1410,6 +1399,43 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
return bo, nil
}

func setBuildOptContexts(bi *build.Inputs, t *Target) error {
dockerfilePath := "Dockerfile"
if t.Dockerfile != nil {
dockerfilePath = *t.Dockerfile
}

if !strings.HasPrefix(dockerfilePath, "cwd://") {
dockerfilePath = path.Clean(dockerfilePath)
}
bi.DockerfilePath = dockerfilePath

if contextPath := t.ContextPath(); contextPath != nil {
bi.ContextPath = *contextPath
if !strings.HasPrefix(bi.ContextPath, "cwd://") && !build.IsRemoteURL(bi.ContextPath) {
bi.ContextPath = path.Clean(bi.ContextPath)
}

if t.DockerfileInline != nil {
bi.DockerfileInline = *t.DockerfileInline
}
} else {
bi.ContextPath = "-"
if t.DockerfileInline != nil {
bi.InStream = build.NewSyncMultiReader(strings.NewReader(*t.DockerfileInline))
} else {
dockerfile, err := os.ReadFile(bi.DockerfilePath)
if err != nil {
return err
}
bi.DockerfilePath = ""
bi.InStream = build.NewSyncMultiReader(bytes.NewReader(dockerfile))
}
}
bi.NamedContexts = toNamedContexts(t.Contexts)
return nil
}

func defaultTarget() *Target {
return &Target{}
}
Expand Down
30 changes: 15 additions & 15 deletions bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ target "webapp" {
require.Equal(t, 1, len(m))

require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile)
require.Equal(t, ".", *m["webapp"].Context)
require.Equal(t, ".", *m["webapp"].ContextPath())
require.Equal(t, ptrstr("webDEP"), m["webapp"].Args["VAR_INHERITED"])
require.Equal(t, true, *m["webapp"].NoCache)
require.Equal(t, "128m", *m["webapp"].ShmSize)
Expand Down Expand Up @@ -79,7 +79,7 @@ target "webapp" {
require.NoError(t, err)

require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile)
require.Equal(t, ".", *m["webapp"].Context)
require.Equal(t, ".", *m["webapp"].ContextPath())

_, isSet := m["webapp"].Args["VAR_UNSET"]
require.False(t, isSet, m["webapp"].Args["VAR_UNSET"])
Expand Down Expand Up @@ -121,7 +121,7 @@ target "webapp" {

m, g, err := ReadTargets(ctx, []File{fp}, []string{"webapp"}, []string{"webapp.context=foo"}, nil, &EntitlementConf{})
require.NoError(t, err)
require.Equal(t, "foo", *m["webapp"].Context)
require.Equal(t, "foo", *m["webapp"].ContextPath())
require.Equal(t, 1, len(g))
require.Equal(t, []string{"webapp"}, g["default"].Targets)
})
Expand Down Expand Up @@ -518,7 +518,7 @@ services:

require.True(t, ok)
require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile)
require.Equal(t, ".", *m["webapp"].Context)
require.Equal(t, ".", *m["webapp"].ContextPath())
require.Equal(t, ptrstr("1"), m["webapp"].Args["buildno"])
require.Equal(t, ptrstr("12"), m["webapp"].Args["buildno2"])

Expand Down Expand Up @@ -579,7 +579,7 @@ services:
_, ok = m["web_app"]
require.True(t, ok)
require.Equal(t, "Dockerfile.webapp", *m["web_app"].Dockerfile)
require.Equal(t, ".", *m["web_app"].Context)
require.Equal(t, ".", *m["web_app"].ContextPath())
require.Equal(t, ptrstr("1"), m["web_app"].Args["buildno"])
require.Equal(t, ptrstr("12"), m["web_app"].Args["buildno2"])

Expand Down Expand Up @@ -610,7 +610,7 @@ func TestHCLContextCwdPrefix(t *testing.T) {
require.Equal(t, 1, len(m))
require.Contains(t, m, "app")
assert.Equal(t, "test", *m["app"].Dockerfile)
assert.Equal(t, "foo", *m["app"].Context)
assert.Equal(t, "cwd://foo", *m["app"].ContextPath())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cwd://foo

Was this a bug or is it a behavior change? I think a path.Clean(bi.ContextPath) is missing in this else cond: https://github.com/docker/buildx/pull/2906/files#diff-fbe2fa23203f2ff05f06b6c0731ea90529f90906807483db76a79f6018781465R1422

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't a bug but it was an artifact of this code that I removed: https://github.com/docker/buildx/pull/2906/files#diff-fbe2fa23203f2ff05f06b6c0731ea90529f90906807483db76a79f6018781465L1285

The function here seemed to be "take target as input and produce build inputs as output" but it was modifying the original target. So the cleaning of cwd://foo is happening but isn't being changed on the original target.

assert.Equal(t, "foo/test", bo["app"].Inputs.DockerfilePath)
assert.Equal(t, "foo", bo["app"].Inputs.ContextPath)
}
Expand Down Expand Up @@ -641,7 +641,7 @@ func TestHCLDockerfileCwdPrefix(t *testing.T) {
require.Equal(t, 1, len(m))
require.Contains(t, m, "app")
assert.Equal(t, "cwd://Dockerfile.app", *m["app"].Dockerfile)
assert.Equal(t, ".", *m["app"].Context)
assert.Equal(t, ".", *m["app"].ContextPath())
assert.Equal(t, filepath.Join(cwd, "Dockerfile.app"), bo["app"].Inputs.DockerfilePath)
assert.Equal(t, ".", bo["app"].Inputs.ContextPath)
}
Expand Down Expand Up @@ -798,9 +798,9 @@ services:
require.True(t, ok)

require.Equal(t, "Dockerfile", *m["app1"].Dockerfile)
require.Equal(t, ".", *m["app1"].Context)
require.Equal(t, ".", *m["app1"].ContextPath())
require.Equal(t, "Dockerfile", *m["app2"].Dockerfile)
require.Equal(t, ".", *m["app2"].Context)
require.Equal(t, ".", *m["app2"].ContextPath())
}

func TestReadContextFromTargetChain(t *testing.T) {
Expand Down Expand Up @@ -1130,7 +1130,7 @@ services:
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image", "image-release"}, g["default"].Targets)
require.Equal(t, 2, len(m))
require.Equal(t, ".", *m["image"].Context)
require.Equal(t, ".", *m["image"].ContextPath())
require.Equal(t, 1, len(m["image-release"].Outputs))
require.Equal(t, "type=image,push=true", m["image-release"].Outputs[0].String())

Expand All @@ -1139,14 +1139,14 @@ services:
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g["default"].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, ".", *m["image"].Context)
require.Equal(t, ".", *m["image"].ContextPath())

m, g, err = ReadTargets(ctx, []File{fjson}, []string{"default"}, nil, nil, &EntitlementConf{})
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g["default"].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, ".", *m["image"].Context)
require.Equal(t, ".", *m["image"].ContextPath())

m, g, err = ReadTargets(ctx, []File{fyml}, []string{"default"}, nil, nil, &EntitlementConf{})
require.NoError(t, err)
Expand All @@ -1172,7 +1172,7 @@ services:
sort.Strings(g["default"].Targets)
require.Equal(t, []string{"addon", "aws", "image"}, g["default"].Targets)
require.Equal(t, 3, len(m))
require.Equal(t, ".", *m["image"].Context)
require.Equal(t, ".", *m["image"].ContextPath())
require.Equal(t, "./Dockerfile", *m["addon"].Dockerfile)
require.Equal(t, "./aws.Dockerfile", *m["aws"].Dockerfile)
}
Expand Down Expand Up @@ -1537,7 +1537,7 @@ target "f" {
require.Equal(t, tt.targets, g["default"].Targets)

require.Equal(t, tt.count, len(m))
require.Equal(t, ".", *m["d"].Context)
require.Equal(t, ".", *m["d"].ContextPath())
require.Equal(t, "./testdockerfile", *m["d"].Dockerfile)
})
}
Expand Down Expand Up @@ -1571,7 +1571,7 @@ services:
require.Equal(t, "app", c.Targets[0].Name)
require.Equal(t, ptrstr("foo"), c.Targets[0].Args["v1"])
require.Equal(t, ptrstr("bar"), c.Targets[0].Args["v2"])
require.Equal(t, "dir", *c.Targets[0].Context)
require.Equal(t, "dir", *c.Targets[0].ContextPath())
require.Equal(t, "Dockerfile-alternate", *c.Targets[0].Dockerfile)
}

Expand Down
6 changes: 3 additions & 3 deletions bake/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf
return nil, errors.Wrapf(err, "invalid service name %q", targetName)
}

var contextPathP *string
var contextPathP **string
if s.Build.Context != "" {
contextPath := s.Build.Context
contextPathP = &contextPath
ptr := &s.Build.Context
contextPathP = &ptr
}
var dockerfilePathP *string
if s.Build.Dockerfile != "" {
Expand Down
6 changes: 3 additions & 3 deletions bake/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ secrets:
return c.Targets[i].Name < c.Targets[j].Name
})
require.Equal(t, "db", c.Targets[0].Name)
require.Equal(t, "db", *c.Targets[0].Context)
require.Equal(t, "db", *c.Targets[0].ContextPath())
require.Equal(t, []string{"docker.io/tonistiigi/db"}, c.Targets[0].Tags)

require.Equal(t, "webapp", c.Targets[1].Name)
require.Equal(t, "dir", *c.Targets[1].Context)
require.Equal(t, "dir", *c.Targets[1].ContextPath())
require.Equal(t, map[string]string{"foo": "bar"}, c.Targets[1].Contexts)
require.Equal(t, "Dockerfile-alternate", *c.Targets[1].Dockerfile)
require.Equal(t, 1, len(c.Targets[1].Args))
Expand All @@ -84,7 +84,7 @@ secrets:
}, stringify(c.Targets[1].Secrets))

require.Equal(t, "webapp2", c.Targets[2].Name)
require.Equal(t, "dir", *c.Targets[2].Context)
require.Equal(t, "dir", *c.Targets[2].ContextPath())
require.Equal(t, "FROM alpine\n", *c.Targets[2].DockerfileInline)
}

Expand Down
Loading
Loading