Skip to content

Commit

Permalink
bake: support setting the context to null in bake
Browse files Browse the repository at this point in the history
Setting the context to null in bake will cause the dockerfile to be sent
over stdin to the build and will prevent using the default context of
"." similar to how this would be used on the command line.

When the JSON is printed from bake, it will reproduce the null so
subsequent uses can keep the value.

The HCL parser has been expanded to have a `WithDefaults` interface that
will fill in default values for newly created structs. The
`UnmarshalJSON` function has also been added to do the same for the
`Target` struct.

Signed-off-by: Jonathan A. Sternberg <[email protected]>
  • Loading branch information
jsternberg committed Jan 10, 2025
1 parent 16edf5d commit e1f381b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
95 changes: 62 additions & 33 deletions bake/bake.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package bake

import (
"bytes"
"context"
"encoding"
"encoding/json"
"io"
"os"
"path"
Expand Down Expand Up @@ -638,10 +640,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 +700,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 @@ -733,10 +731,30 @@ type Target struct {
var (
_ hclparser.WithEvalContexts = &Target{}
_ hclparser.WithGetName = &Target{}
_ hclparser.WithDefaults = &Target{}
_ hclparser.WithEvalContexts = &Group{}
_ hclparser.WithGetName = &Group{}
)

func (t *Target) SetDefaults() {
contextPath := "."
t.Context = &contextPath
}

func (t *Target) UnmarshalJSON(data []byte) error {
v := &Target{}
v.SetDefaults()

// Prevent recursive call of our custom unmarshal function while
// reusing the existing struct with its tags.
type TargetAlias Target
if err := json.Unmarshal(data, (*TargetAlias)(v)); err != nil {
return err
}
*t = *v
return nil
}

func (t *Target) normalize() {
t.Annotations = removeDupesStr(t.Annotations)
t.Attest = t.Attest.Normalize()
Expand Down Expand Up @@ -767,9 +785,7 @@ func (t *Target) normalize() {
}

func (t *Target) Merge(t2 *Target) {
if t2.Context != nil {
t.Context = t2.Context
}
t.Context = t2.Context
if t2.Dockerfile != nil {
t.Dockerfile = t2.Dockerfile
}
Expand Down Expand Up @@ -1217,30 +1233,11 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
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 +1270,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 +1279,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 +1405,40 @@ 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 t.Context != nil {
bi.ContextPath = *t.Context
if t.DockerfileInline != nil {
bi.DockerfileInline = *t.DockerfileInline
}
return nil
} 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
7 changes: 7 additions & 0 deletions bake/hclparser/hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ type WithGetName interface {
GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(hcl.Expression) hcl.Diagnostics) (string, error)
}

type WithDefaults interface {
SetDefaults()
}

// errUndefined is returned when a variable or function is not defined.
type errUndefined struct{}

Expand Down Expand Up @@ -989,6 +993,9 @@ func key(ks ...any) uint64 {
}

func decodeBody(body hcl.Body, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics {
if val, ok := val.(WithDefaults); ok {
val.SetDefaults()
}
dec := gohcl.DecodeOptions{ImpliedType: ImpliedType}
return dec.DecodeBody(body, ctx, val)
}

0 comments on commit e1f381b

Please sign in to comment.