2019-04-05 15:04:19 +08:00
|
|
|
package bake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-21 07:22:27 +08:00
|
|
|
"encoding/csv"
|
2021-06-24 10:03:17 +08:00
|
|
|
"fmt"
|
2022-03-10 08:46:06 +08:00
|
|
|
"io"
|
2019-05-15 08:37:34 +08:00
|
|
|
"os"
|
2019-05-25 06:23:16 +08:00
|
|
|
"path"
|
2022-02-24 16:05:42 +08:00
|
|
|
"path/filepath"
|
2020-09-17 13:52:40 +08:00
|
|
|
"regexp"
|
2022-01-25 08:38:33 +08:00
|
|
|
"sort"
|
2019-10-17 06:10:17 +08:00
|
|
|
"strconv"
|
2019-04-05 15:04:19 +08:00
|
|
|
"strings"
|
|
|
|
|
2021-06-24 10:03:17 +08:00
|
|
|
"github.com/docker/buildx/bake/hclparser"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/build"
|
2021-04-09 14:20:26 +08:00
|
|
|
"github.com/docker/buildx/util/buildflags"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/util/platformutil"
|
2022-07-06 16:47:29 +08:00
|
|
|
"github.com/docker/cli/cli/config"
|
2022-07-22 16:53:15 +08:00
|
|
|
"github.com/docker/docker/builder/remotecontext/urlutil"
|
2020-04-16 09:00:21 +08:00
|
|
|
hcl "github.com/hashicorp/hcl/v2"
|
2020-09-17 13:52:40 +08:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2019-04-05 15:04:19 +08:00
|
|
|
"github.com/moby/buildkit/session/auth/authprovider"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-01-26 17:29:17 +08:00
|
|
|
var (
|
|
|
|
httpPrefix = regexp.MustCompile(`^https?://`)
|
|
|
|
gitURLPathWithFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`)
|
|
|
|
|
|
|
|
validTargetNameChars = `[a-zA-Z0-9_-]+`
|
|
|
|
targetNamePattern = regexp.MustCompile(`^` + validTargetNameChars + `$`)
|
|
|
|
)
|
2020-09-17 13:52:40 +08:00
|
|
|
|
|
|
|
type File struct {
|
|
|
|
Name string
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2021-09-21 07:22:27 +08:00
|
|
|
type Override struct {
|
2021-09-29 14:55:19 +08:00
|
|
|
Value string
|
|
|
|
ArrValue []string
|
2021-09-21 07:22:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
func defaultFilenames() []string {
|
|
|
|
return []string{
|
|
|
|
"docker-compose.yml", // support app
|
|
|
|
"docker-compose.yaml", // support app
|
|
|
|
"docker-bake.json",
|
|
|
|
"docker-bake.override.json",
|
|
|
|
"docker-bake.hcl",
|
|
|
|
"docker-bake.override.hcl",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadLocalFiles(names []string) ([]File, error) {
|
|
|
|
isDefault := false
|
|
|
|
if len(names) == 0 {
|
|
|
|
isDefault = true
|
|
|
|
names = defaultFilenames()
|
|
|
|
}
|
|
|
|
out := make([]File, 0, len(names))
|
|
|
|
|
|
|
|
for _, n := range names {
|
2021-11-26 01:41:55 +08:00
|
|
|
var dt []byte
|
|
|
|
var err error
|
|
|
|
if n == "-" {
|
2022-03-10 08:46:06 +08:00
|
|
|
dt, err = io.ReadAll(os.Stdin)
|
2021-11-26 01:41:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-10 08:46:06 +08:00
|
|
|
dt, err = os.ReadFile(n)
|
2021-11-26 01:41:55 +08:00
|
|
|
if err != nil {
|
|
|
|
if isDefault && errors.Is(err, os.ErrNotExist) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
2020-09-17 13:52:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out = append(out, File{Name: n, Data: dt})
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:46:48 +08:00
|
|
|
func ReadTargets(ctx context.Context, files []File, targets, overrides []string, defaults map[string]string) (map[string]*Target, map[string]*Group, error) {
|
2021-07-13 08:53:04 +08:00
|
|
|
c, err := ParseFiles(files, defaults)
|
2021-02-09 15:17:10 +08:00
|
|
|
if err != nil {
|
2021-08-12 15:02:28 +08:00
|
|
|
return nil, nil, err
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2021-02-09 15:17:10 +08:00
|
|
|
|
2022-07-31 22:53:39 +08:00
|
|
|
for i, t := range targets {
|
|
|
|
targets[i] = sanitizeTargetName(t)
|
|
|
|
}
|
|
|
|
|
2020-04-22 03:53:19 +08:00
|
|
|
o, err := c.newOverrides(overrides)
|
|
|
|
if err != nil {
|
2021-08-12 15:02:28 +08:00
|
|
|
return nil, nil, err
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
m := map[string]*Target{}
|
2022-09-12 20:46:48 +08:00
|
|
|
n := map[string]*Group{}
|
|
|
|
for _, target := range targets {
|
|
|
|
ts, gs := c.ResolveGroup(target)
|
|
|
|
for _, tname := range ts {
|
|
|
|
t, err := c.ResolveTarget(tname, o)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
2021-08-12 15:02:28 +08:00
|
|
|
return nil, nil, err
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
if t != nil {
|
2022-09-12 20:46:48 +08:00
|
|
|
m[tname] = t
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-12 20:46:48 +08:00
|
|
|
for _, gname := range gs {
|
2021-12-14 02:21:14 +08:00
|
|
|
for _, group := range c.Groups {
|
2022-09-12 20:46:48 +08:00
|
|
|
if group.Name == gname {
|
|
|
|
n[gname] = group
|
2021-12-14 02:21:14 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 20:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, target := range targets {
|
|
|
|
if target == "default" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := n["default"]; !ok {
|
|
|
|
n["default"] = &Group{Name: "default"}
|
|
|
|
}
|
|
|
|
n["default"].Targets = append(n["default"].Targets, target)
|
|
|
|
}
|
|
|
|
if g, ok := n["default"]; ok {
|
|
|
|
g.Targets = dedupSlice(g.Targets)
|
2021-11-25 04:54:51 +08:00
|
|
|
}
|
|
|
|
|
2022-01-25 08:38:33 +08:00
|
|
|
for name, t := range m {
|
|
|
|
if err := c.loadLinks(name, t, m, o, nil); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 20:46:48 +08:00
|
|
|
return m, n, nil
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2022-08-05 21:21:21 +08:00
|
|
|
func dedupSlice(s []string) []string {
|
2021-12-14 02:21:14 +08:00
|
|
|
if len(s) == 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
var res []string
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for _, val := range s {
|
|
|
|
if _, ok := seen[val]; !ok {
|
|
|
|
res = append(res, val)
|
|
|
|
seen[val] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:21:21 +08:00
|
|
|
func dedupMap(ms ...map[string]string) map[string]string {
|
|
|
|
if len(ms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
res := map[string]string{}
|
|
|
|
for _, m := range ms {
|
|
|
|
if len(m) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
|
|
|
if _, ok := res[k]; !ok {
|
|
|
|
res[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-08-10 09:24:33 +08:00
|
|
|
func sliceToMap(env []string) (res map[string]string) {
|
|
|
|
res = make(map[string]string)
|
|
|
|
for _, s := range env {
|
|
|
|
kv := strings.SplitN(s, "=", 2)
|
|
|
|
key := kv[0]
|
|
|
|
switch {
|
|
|
|
case len(kv) == 1:
|
|
|
|
res[key] = ""
|
|
|
|
default:
|
|
|
|
res[key] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:53:04 +08:00
|
|
|
func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error) {
|
2021-06-24 10:03:17 +08:00
|
|
|
defer func() {
|
|
|
|
err = formatHCLError(err, files)
|
|
|
|
}()
|
|
|
|
|
2021-02-09 15:17:10 +08:00
|
|
|
var c Config
|
2022-08-18 17:34:08 +08:00
|
|
|
var composeFiles []File
|
|
|
|
var hclFiles []*hcl.File
|
2021-02-09 15:17:10 +08:00
|
|
|
for _, f := range files {
|
2022-08-18 17:34:08 +08:00
|
|
|
isCompose, composeErr := validateComposeFile(f.Data, f.Name)
|
2021-06-24 10:03:17 +08:00
|
|
|
if isCompose {
|
|
|
|
if composeErr != nil {
|
|
|
|
return nil, composeErr
|
|
|
|
}
|
2022-08-18 17:34:08 +08:00
|
|
|
composeFiles = append(composeFiles, f)
|
2021-06-24 10:03:17 +08:00
|
|
|
}
|
|
|
|
if !isCompose {
|
|
|
|
hf, isHCL, err := ParseHCLFile(f.Data, f.Name)
|
|
|
|
if isHCL {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-18 17:34:08 +08:00
|
|
|
hclFiles = append(hclFiles, hf)
|
2021-06-24 10:03:17 +08:00
|
|
|
} else if composeErr != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse %s: parsing yaml: %v, parsing hcl: %w", f.Name, composeErr, err)
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-09 15:17:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 17:34:08 +08:00
|
|
|
if len(composeFiles) > 0 {
|
|
|
|
cfg, cmperr := ParseComposeFiles(composeFiles)
|
|
|
|
if cmperr != nil {
|
|
|
|
return nil, errors.Wrap(cmperr, "failed to parse compose file")
|
|
|
|
}
|
|
|
|
c = mergeConfig(c, *cfg)
|
|
|
|
c = dedupeConfig(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hclFiles) > 0 {
|
|
|
|
if err := hclparser.Parse(hcl.MergeFiles(hclFiles), hclparser.Opt{
|
2022-01-26 17:29:17 +08:00
|
|
|
LookupVar: os.LookupEnv,
|
|
|
|
Vars: defaults,
|
|
|
|
ValidateLabel: validateTargetName,
|
2021-06-24 10:03:17 +08:00
|
|
|
}, &c); err.HasErrors() {
|
2021-02-09 15:17:10 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 17:34:08 +08:00
|
|
|
|
2021-02-09 15:17:10 +08:00
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dedupeConfig(c Config) Config {
|
|
|
|
c2 := c
|
2022-08-18 17:34:08 +08:00
|
|
|
c2.Groups = make([]*Group, 0, len(c2.Groups))
|
|
|
|
for _, g := range c.Groups {
|
|
|
|
g1 := *g
|
|
|
|
g1.Targets = dedupSlice(g1.Targets)
|
|
|
|
c2.Groups = append(c2.Groups, &g1)
|
|
|
|
}
|
2021-02-09 15:17:10 +08:00
|
|
|
c2.Targets = make([]*Target, 0, len(c2.Targets))
|
2022-08-18 17:34:08 +08:00
|
|
|
mt := map[string]*Target{}
|
2021-02-09 15:17:10 +08:00
|
|
|
for _, t := range c.Targets {
|
2022-08-18 17:34:08 +08:00
|
|
|
if t2, ok := mt[t.Name]; ok {
|
2021-06-24 10:03:17 +08:00
|
|
|
t2.Merge(t)
|
2021-02-09 15:17:10 +08:00
|
|
|
} else {
|
2022-08-18 17:34:08 +08:00
|
|
|
mt[t.Name] = t
|
2021-02-09 15:17:10 +08:00
|
|
|
c2.Targets = append(c2.Targets, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c2
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
func ParseFile(dt []byte, fn string) (*Config, error) {
|
2021-07-13 08:53:04 +08:00
|
|
|
return ParseFiles([]File{{Data: dt, Name: fn}}, nil)
|
2021-02-09 15:17:10 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 15:04:19 +08:00
|
|
|
type Config struct {
|
2021-06-24 10:03:17 +08:00
|
|
|
Groups []*Group `json:"group" hcl:"group,block"`
|
|
|
|
Targets []*Target `json:"target" hcl:"target,block"`
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func mergeConfig(c1, c2 Config) Config {
|
2020-04-16 09:00:18 +08:00
|
|
|
if c1.Groups == nil {
|
|
|
|
c1.Groups = []*Group{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g2 := range c2.Groups {
|
|
|
|
var g1 *Group
|
|
|
|
for _, g := range c1.Groups {
|
|
|
|
if g2.Name == g.Name {
|
|
|
|
g1 = g
|
|
|
|
break
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
if g1 == nil {
|
|
|
|
c1.Groups = append(c1.Groups, g2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nextTarget:
|
|
|
|
for _, t2 := range g2.Targets {
|
|
|
|
for _, t1 := range g1.Targets {
|
|
|
|
if t1 == t2 {
|
|
|
|
continue nextTarget
|
2019-08-20 06:48:42 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
g1.Targets = append(g1.Targets, t2)
|
2019-08-20 06:48:42 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
c1.Groups = append(c1.Groups, g1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c1.Targets == nil {
|
|
|
|
c1.Targets = []*Target{}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2020-04-16 09:00:18 +08:00
|
|
|
for _, t2 := range c2.Targets {
|
|
|
|
var t1 *Target
|
|
|
|
for _, t := range c1.Targets {
|
|
|
|
if t2.Name == t.Name {
|
|
|
|
t1 = t
|
|
|
|
break
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
if t1 != nil {
|
2021-06-24 10:03:17 +08:00
|
|
|
t1.Merge(t2)
|
|
|
|
t2 = t1
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
c1.Targets = append(c1.Targets, t2)
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
|
2019-04-05 15:04:19 +08:00
|
|
|
return c1
|
|
|
|
}
|
|
|
|
|
2020-04-22 05:47:40 +08:00
|
|
|
func (c Config) expandTargets(pattern string) ([]string, error) {
|
2020-04-16 09:00:18 +08:00
|
|
|
for _, target := range c.Targets {
|
|
|
|
if target.Name == pattern {
|
|
|
|
return []string{pattern}, nil
|
|
|
|
}
|
2020-04-22 05:47:40 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
|
2020-04-22 05:47:40 +08:00
|
|
|
var names []string
|
2020-04-16 09:00:18 +08:00
|
|
|
for _, target := range c.Targets {
|
|
|
|
ok, err := path.Match(pattern, target.Name)
|
2020-04-22 05:47:40 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not match targets with '%s'", pattern)
|
|
|
|
}
|
|
|
|
if ok {
|
2020-04-16 09:00:18 +08:00
|
|
|
names = append(names, target.Name)
|
2020-04-22 05:47:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
|
|
return nil, errors.Errorf("could not find any target matching '%s'", pattern)
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 08:38:33 +08:00
|
|
|
func (c Config) loadLinks(name string, t *Target, m map[string]*Target, o map[string]map[string]Override, visited []string) error {
|
|
|
|
visited = append(visited, name)
|
|
|
|
for _, v := range t.Contexts {
|
|
|
|
if strings.HasPrefix(v, "target:") {
|
|
|
|
target := strings.TrimPrefix(v, "target:")
|
|
|
|
if target == t.Name {
|
|
|
|
return errors.Errorf("target %s cannot link to itself", target)
|
|
|
|
}
|
|
|
|
for _, v := range visited {
|
|
|
|
if v == target {
|
|
|
|
return errors.Errorf("infinite loop from %s to %s", name, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t2, ok := m[target]
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
t2, err = c.ResolveTarget(target, o)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t2.Outputs = nil
|
2022-06-04 01:26:16 +08:00
|
|
|
t2.linked = true
|
2022-01-25 08:38:33 +08:00
|
|
|
m[target] = t2
|
|
|
|
}
|
|
|
|
if err := c.loadLinks(target, t2, m, o, visited); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(t.Platforms) > 1 && len(t2.Platforms) > 1 {
|
|
|
|
if !sliceEqual(t.Platforms, t2.Platforms) {
|
|
|
|
return errors.Errorf("target %s can't be used by %s because it is defined for different platforms %v and %v", target, name, t2.Platforms, t.Platforms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:55:19 +08:00
|
|
|
func (c Config) newOverrides(v []string) (map[string]map[string]Override, error) {
|
|
|
|
m := map[string]map[string]Override{}
|
2019-04-05 15:04:19 +08:00
|
|
|
for _, v := range v {
|
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
keys := strings.SplitN(parts[0], ".", 3)
|
|
|
|
if len(keys) < 2 {
|
2020-04-22 03:53:19 +08:00
|
|
|
return nil, errors.Errorf("invalid override key %s, expected target.name", parts[0])
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 05:47:40 +08:00
|
|
|
pattern := keys[0]
|
2019-10-31 06:15:10 +08:00
|
|
|
if len(parts) != 2 && keys[1] != "args" {
|
2020-04-22 03:53:19 +08:00
|
|
|
return nil, errors.Errorf("invalid override %s, expected target.name=value", v)
|
2019-10-31 06:15:10 +08:00
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
|
2020-04-22 05:47:40 +08:00
|
|
|
names, err := c.expandTargets(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2021-09-21 07:22:27 +08:00
|
|
|
kk := strings.SplitN(parts[0], ".", 2)
|
|
|
|
|
2020-04-22 05:47:40 +08:00
|
|
|
for _, name := range names {
|
2021-09-29 14:55:19 +08:00
|
|
|
t, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
t = map[string]Override{}
|
|
|
|
m[name] = t
|
2020-04-16 09:00:18 +08:00
|
|
|
}
|
2020-04-22 05:47:40 +08:00
|
|
|
|
2021-09-29 14:55:19 +08:00
|
|
|
o := t[kk[1]]
|
|
|
|
|
2020-04-22 05:47:40 +08:00
|
|
|
switch keys[1] {
|
2021-09-29 14:55:19 +08:00
|
|
|
case "output", "cache-to", "cache-from", "tags", "platform", "secrets", "ssh":
|
|
|
|
if len(parts) == 2 {
|
|
|
|
o.ArrValue = append(o.ArrValue, parts[1])
|
|
|
|
}
|
2020-04-22 05:47:40 +08:00
|
|
|
case "args":
|
|
|
|
if len(keys) != 3 {
|
|
|
|
return nil, errors.Errorf("invalid key %s, args requires name", parts[0])
|
2019-10-31 06:15:10 +08:00
|
|
|
}
|
2020-04-22 05:47:40 +08:00
|
|
|
if len(parts) < 2 {
|
|
|
|
v, ok := os.LookupEnv(keys[2])
|
2021-09-21 07:22:27 +08:00
|
|
|
if !ok {
|
|
|
|
continue
|
2020-04-22 05:47:40 +08:00
|
|
|
}
|
2021-09-21 07:22:27 +08:00
|
|
|
o.Value = v
|
2019-10-17 06:10:17 +08:00
|
|
|
}
|
2021-09-29 14:55:19 +08:00
|
|
|
fallthrough
|
2022-01-25 08:38:33 +08:00
|
|
|
case "contexts":
|
|
|
|
if len(keys) != 3 {
|
|
|
|
return nil, errors.Errorf("invalid key %s, contexts requires name", parts[0])
|
|
|
|
}
|
|
|
|
fallthrough
|
2021-09-29 14:55:19 +08:00
|
|
|
default:
|
|
|
|
if len(parts) == 2 {
|
|
|
|
o.Value = parts[1]
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2021-09-21 07:22:27 +08:00
|
|
|
|
2021-09-29 14:55:19 +08:00
|
|
|
t[kk[1]] = o
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 03:53:19 +08:00
|
|
|
return m, nil
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:46:48 +08:00
|
|
|
func (c Config) ResolveGroup(name string) ([]string, []string) {
|
|
|
|
targets, groups := c.group(name, map[string]visit{})
|
|
|
|
return dedupSlice(targets), dedupSlice(groups)
|
|
|
|
}
|
|
|
|
|
|
|
|
type visit struct {
|
|
|
|
target []string
|
|
|
|
group []string
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 20:46:48 +08:00
|
|
|
func (c Config) group(name string, visited map[string]visit) ([]string, []string) {
|
|
|
|
if v, ok := visited[name]; ok {
|
|
|
|
return v.target, v.group
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
var g *Group
|
|
|
|
for _, group := range c.Groups {
|
|
|
|
if group.Name == name {
|
|
|
|
g = group
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if g == nil {
|
2022-09-12 20:46:48 +08:00
|
|
|
return []string{name}, nil
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2022-09-12 20:46:48 +08:00
|
|
|
visited[name] = visit{}
|
2019-04-05 15:04:19 +08:00
|
|
|
targets := make([]string, 0, len(g.Targets))
|
2022-09-12 20:46:48 +08:00
|
|
|
groups := []string{name}
|
2019-04-05 15:04:19 +08:00
|
|
|
for _, t := range g.Targets {
|
2022-09-12 20:46:48 +08:00
|
|
|
ttarget, tgroup := c.group(t, visited)
|
|
|
|
if len(ttarget) > 0 {
|
|
|
|
targets = append(targets, ttarget...)
|
2021-12-14 18:17:37 +08:00
|
|
|
} else {
|
|
|
|
targets = append(targets, t)
|
|
|
|
}
|
2022-09-12 20:46:48 +08:00
|
|
|
if len(tgroup) > 0 {
|
|
|
|
groups = append(groups, tgroup...)
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2022-09-12 20:46:48 +08:00
|
|
|
visited[name] = visit{target: targets, group: groups}
|
|
|
|
return targets, groups
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2021-09-29 14:55:19 +08:00
|
|
|
func (c Config) ResolveTarget(name string, overrides map[string]map[string]Override) (*Target, error) {
|
2022-01-14 05:22:23 +08:00
|
|
|
t, err := c.target(name, map[string]*Target{}, overrides)
|
2019-04-20 13:54:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-14 05:22:23 +08:00
|
|
|
t.Inherits = nil
|
2019-04-20 13:54:34 +08:00
|
|
|
if t.Context == nil {
|
|
|
|
s := "."
|
|
|
|
t.Context = &s
|
|
|
|
}
|
|
|
|
if t.Dockerfile == nil {
|
|
|
|
s := "Dockerfile"
|
|
|
|
t.Dockerfile = &s
|
|
|
|
}
|
|
|
|
return t, nil
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2022-01-14 05:22:23 +08:00
|
|
|
func (c Config) target(name string, visited map[string]*Target, overrides map[string]map[string]Override) (*Target, error) {
|
|
|
|
if t, ok := visited[name]; ok {
|
|
|
|
return t, nil
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
2022-01-14 05:22:23 +08:00
|
|
|
visited[name] = nil
|
2020-04-16 09:00:18 +08:00
|
|
|
var t *Target
|
|
|
|
for _, target := range c.Targets {
|
|
|
|
if target.Name == name {
|
|
|
|
t = target
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t == nil {
|
2019-04-05 15:04:19 +08:00
|
|
|
return nil, errors.Errorf("failed to find target %s", name)
|
|
|
|
}
|
2020-04-16 09:00:18 +08:00
|
|
|
tt := &Target{}
|
2019-04-05 15:04:19 +08:00
|
|
|
for _, name := range t.Inherits {
|
2020-04-22 03:53:19 +08:00
|
|
|
t, err := c.target(name, visited, overrides)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if t != nil {
|
2021-06-24 10:03:17 +08:00
|
|
|
tt.Merge(t)
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 10:03:17 +08:00
|
|
|
m := defaultTarget()
|
|
|
|
m.Merge(tt)
|
|
|
|
m.Merge(t)
|
|
|
|
tt = m
|
2021-09-21 07:22:27 +08:00
|
|
|
if err := tt.AddOverrides(overrides[name]); err != nil {
|
|
|
|
return nil, err
|
2020-04-16 09:00:18 +08:00
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
tt.normalize()
|
2022-01-14 05:22:23 +08:00
|
|
|
visited[name] = tt
|
2020-04-16 09:00:18 +08:00
|
|
|
return tt, nil
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Group struct {
|
2020-04-16 09:00:18 +08:00
|
|
|
Name string `json:"-" hcl:"name,label"`
|
|
|
|
Targets []string `json:"targets" hcl:"targets"`
|
2019-04-05 15:04:19 +08:00
|
|
|
// Target // TODO?
|
|
|
|
}
|
|
|
|
|
|
|
|
type Target struct {
|
2020-04-16 09:00:18 +08:00
|
|
|
Name string `json:"-" hcl:"name,label"`
|
|
|
|
|
2019-10-17 06:10:17 +08:00
|
|
|
// Inherits is the only field that cannot be overridden with --set
|
2020-04-16 09:00:18 +08:00
|
|
|
Inherits []string `json:"inherits,omitempty" hcl:"inherits,optional"`
|
|
|
|
|
2020-09-28 12:34:26 +08:00
|
|
|
Context *string `json:"context,omitempty" hcl:"context,optional"`
|
2022-01-25 08:38:33 +08:00
|
|
|
Contexts map[string]string `json:"contexts,omitempty" hcl:"contexts,optional"`
|
2020-09-28 12:34:26 +08:00
|
|
|
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional"`
|
|
|
|
DockerfileInline *string `json:"dockerfile-inline,omitempty" hcl:"dockerfile-inline,optional"`
|
|
|
|
Args map[string]string `json:"args,omitempty" hcl:"args,optional"`
|
|
|
|
Labels map[string]string `json:"labels,omitempty" hcl:"labels,optional"`
|
|
|
|
Tags []string `json:"tags,omitempty" hcl:"tags,optional"`
|
|
|
|
CacheFrom []string `json:"cache-from,omitempty" hcl:"cache-from,optional"`
|
|
|
|
CacheTo []string `json:"cache-to,omitempty" hcl:"cache-to,optional"`
|
|
|
|
Target *string `json:"target,omitempty" hcl:"target,optional"`
|
|
|
|
Secrets []string `json:"secret,omitempty" hcl:"secret,optional"`
|
|
|
|
SSH []string `json:"ssh,omitempty" hcl:"ssh,optional"`
|
|
|
|
Platforms []string `json:"platforms,omitempty" hcl:"platforms,optional"`
|
|
|
|
Outputs []string `json:"output,omitempty" hcl:"output,optional"`
|
|
|
|
Pull *bool `json:"pull,omitempty" hcl:"pull,optional"`
|
|
|
|
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional"`
|
2021-12-11 02:32:40 +08:00
|
|
|
NetworkMode *string `json:"-" hcl:"-"`
|
2021-11-23 12:56:29 +08:00
|
|
|
NoCacheFilter []string `json:"no-cache-filter,omitempty" hcl:"no-cache-filter,optional"`
|
2022-11-05 02:47:00 +08:00
|
|
|
// IMPORTANT: if you add more fields here, do not forget to update newOverrides and docs/manuals/bake/file-definition.md.
|
2022-06-04 01:26:16 +08:00
|
|
|
|
|
|
|
// linked is a private field to mark a target used as a linked one
|
|
|
|
linked bool
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Target) normalize() {
|
|
|
|
t.Tags = removeDupes(t.Tags)
|
|
|
|
t.Secrets = removeDupes(t.Secrets)
|
|
|
|
t.SSH = removeDupes(t.SSH)
|
|
|
|
t.Platforms = removeDupes(t.Platforms)
|
2019-04-24 05:18:54 +08:00
|
|
|
t.CacheFrom = removeDupes(t.CacheFrom)
|
|
|
|
t.CacheTo = removeDupes(t.CacheTo)
|
|
|
|
t.Outputs = removeDupes(t.Outputs)
|
2021-11-23 12:56:29 +08:00
|
|
|
t.NoCacheFilter = removeDupes(t.NoCacheFilter)
|
2022-01-25 08:38:33 +08:00
|
|
|
|
|
|
|
for k, v := range t.Contexts {
|
|
|
|
if v == "" {
|
|
|
|
delete(t.Contexts, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(t.Contexts) == 0 {
|
|
|
|
t.Contexts = nil
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 10:03:17 +08:00
|
|
|
func (t *Target) Merge(t2 *Target) {
|
|
|
|
if t2.Context != nil {
|
|
|
|
t.Context = t2.Context
|
|
|
|
}
|
|
|
|
if t2.Dockerfile != nil {
|
|
|
|
t.Dockerfile = t2.Dockerfile
|
|
|
|
}
|
|
|
|
if t2.DockerfileInline != nil {
|
|
|
|
t.DockerfileInline = t2.DockerfileInline
|
|
|
|
}
|
|
|
|
for k, v := range t2.Args {
|
|
|
|
if t.Args == nil {
|
|
|
|
t.Args = map[string]string{}
|
|
|
|
}
|
|
|
|
t.Args[k] = v
|
|
|
|
}
|
2022-01-25 08:38:33 +08:00
|
|
|
for k, v := range t2.Contexts {
|
|
|
|
if t.Contexts == nil {
|
|
|
|
t.Contexts = map[string]string{}
|
|
|
|
}
|
|
|
|
t.Contexts[k] = v
|
|
|
|
}
|
2021-06-24 10:03:17 +08:00
|
|
|
for k, v := range t2.Labels {
|
|
|
|
if t.Labels == nil {
|
|
|
|
t.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
t.Labels[k] = v
|
|
|
|
}
|
|
|
|
if t2.Tags != nil { // no merge
|
|
|
|
t.Tags = t2.Tags
|
|
|
|
}
|
|
|
|
if t2.Target != nil {
|
|
|
|
t.Target = t2.Target
|
|
|
|
}
|
|
|
|
if t2.Secrets != nil { // merge
|
|
|
|
t.Secrets = append(t.Secrets, t2.Secrets...)
|
|
|
|
}
|
|
|
|
if t2.SSH != nil { // merge
|
|
|
|
t.SSH = append(t.SSH, t2.SSH...)
|
|
|
|
}
|
|
|
|
if t2.Platforms != nil { // no merge
|
|
|
|
t.Platforms = t2.Platforms
|
|
|
|
}
|
|
|
|
if t2.CacheFrom != nil { // merge
|
|
|
|
t.CacheFrom = append(t.CacheFrom, t2.CacheFrom...)
|
|
|
|
}
|
|
|
|
if t2.CacheTo != nil { // no merge
|
|
|
|
t.CacheTo = t2.CacheTo
|
|
|
|
}
|
|
|
|
if t2.Outputs != nil { // no merge
|
|
|
|
t.Outputs = t2.Outputs
|
|
|
|
}
|
|
|
|
if t2.Pull != nil {
|
|
|
|
t.Pull = t2.Pull
|
|
|
|
}
|
|
|
|
if t2.NoCache != nil {
|
|
|
|
t.NoCache = t2.NoCache
|
|
|
|
}
|
2021-11-25 19:35:10 +08:00
|
|
|
if t2.NetworkMode != nil {
|
|
|
|
t.NetworkMode = t2.NetworkMode
|
|
|
|
}
|
2021-11-23 12:56:29 +08:00
|
|
|
if t2.NoCacheFilter != nil { // merge
|
|
|
|
t.NoCacheFilter = append(t.NoCacheFilter, t2.NoCacheFilter...)
|
|
|
|
}
|
2021-06-24 10:03:17 +08:00
|
|
|
t.Inherits = append(t.Inherits, t2.Inherits...)
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:55:19 +08:00
|
|
|
func (t *Target) AddOverrides(overrides map[string]Override) error {
|
|
|
|
for key, o := range overrides {
|
2021-09-21 07:22:27 +08:00
|
|
|
value := o.Value
|
2021-09-29 14:55:19 +08:00
|
|
|
keys := strings.SplitN(key, ".", 2)
|
2021-09-21 07:22:27 +08:00
|
|
|
switch keys[0] {
|
|
|
|
case "context":
|
|
|
|
t.Context = &value
|
|
|
|
case "dockerfile":
|
|
|
|
t.Dockerfile = &value
|
|
|
|
case "args":
|
|
|
|
if len(keys) != 2 {
|
|
|
|
return errors.Errorf("args require name")
|
|
|
|
}
|
|
|
|
if t.Args == nil {
|
|
|
|
t.Args = map[string]string{}
|
|
|
|
}
|
|
|
|
t.Args[keys[1]] = value
|
2022-01-25 08:38:33 +08:00
|
|
|
case "contexts":
|
|
|
|
if len(keys) != 2 {
|
|
|
|
return errors.Errorf("contexts require name")
|
|
|
|
}
|
|
|
|
if t.Contexts == nil {
|
|
|
|
t.Contexts = map[string]string{}
|
|
|
|
}
|
|
|
|
t.Contexts[keys[1]] = value
|
2021-09-21 07:22:27 +08:00
|
|
|
case "labels":
|
|
|
|
if len(keys) != 2 {
|
|
|
|
return errors.Errorf("labels require name")
|
|
|
|
}
|
|
|
|
if t.Labels == nil {
|
|
|
|
t.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
t.Labels[keys[1]] = value
|
|
|
|
case "tags":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.Tags = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "cache-from":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.CacheFrom = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "cache-to":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.CacheTo = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "target":
|
|
|
|
t.Target = &value
|
|
|
|
case "secrets":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.Secrets = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "ssh":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.SSH = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "platform":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.Platforms = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "output":
|
2021-09-29 14:55:19 +08:00
|
|
|
t.Outputs = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "no-cache":
|
|
|
|
noCache, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("invalid value %s for boolean key no-cache", value)
|
|
|
|
}
|
|
|
|
t.NoCache = &noCache
|
2021-11-23 12:56:29 +08:00
|
|
|
case "no-cache-filter":
|
|
|
|
t.NoCacheFilter = o.ArrValue
|
2021-09-21 07:22:27 +08:00
|
|
|
case "pull":
|
|
|
|
pull, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("invalid value %s for boolean key pull", value)
|
|
|
|
}
|
|
|
|
t.Pull = &pull
|
|
|
|
case "push":
|
|
|
|
_, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Errorf("invalid value %s for boolean key push", value)
|
|
|
|
}
|
|
|
|
if len(t.Outputs) == 0 {
|
|
|
|
t.Outputs = append(t.Outputs, "type=image,push=true")
|
|
|
|
} else {
|
|
|
|
for i, output := range t.Outputs {
|
|
|
|
if typ := parseOutputType(output); typ == "image" || typ == "registry" {
|
|
|
|
t.Outputs[i] = t.Outputs[i] + ",push=" + value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown key: %s", keys[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Options, error) {
|
2019-04-05 15:04:19 +08:00
|
|
|
m2 := make(map[string]build.Options, len(m))
|
|
|
|
for k, v := range m {
|
2020-09-17 13:52:40 +08:00
|
|
|
bo, err := toBuildOpt(v, inp)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m2[k] = *bo
|
|
|
|
}
|
|
|
|
return m2, nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
func updateContext(t *build.Inputs, inp *Input) {
|
|
|
|
if inp == nil || inp.State == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-02-24 15:09:46 +08:00
|
|
|
|
|
|
|
for k, v := range t.NamedContexts {
|
|
|
|
if v.Path == "." {
|
|
|
|
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if IsRemoteURL(v.Path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
|
|
|
|
t.NamedContexts[k] = build.NamedContext{State: &st}
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
if t.ContextPath == "." {
|
|
|
|
t.ContextPath = inp.URL
|
|
|
|
return
|
|
|
|
}
|
2021-07-13 08:53:04 +08:00
|
|
|
if strings.HasPrefix(t.ContextPath, "cwd://") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if IsRemoteURL(t.ContextPath) {
|
|
|
|
return
|
|
|
|
}
|
2020-09-17 13:52:40 +08:00
|
|
|
st := llb.Scratch().File(llb.Copy(*inp.State, t.ContextPath, "/"), llb.WithCustomNamef("set context to %s", t.ContextPath))
|
|
|
|
t.ContextState = &st
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:05:42 +08:00
|
|
|
// validateContextsEntitlements is a basic check to ensure contexts do not
|
|
|
|
// escape local directories when loaded from remote sources. This is to be
|
|
|
|
// replaced with proper entitlements support in the future.
|
|
|
|
func validateContextsEntitlements(t build.Inputs, inp *Input) error {
|
|
|
|
if inp == nil || inp.State == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if v, ok := os.LookupEnv("BAKE_ALLOW_REMOTE_FS_ACCESS"); ok {
|
|
|
|
if vv, _ := strconv.ParseBool(v); vv {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.ContextState == nil {
|
|
|
|
if err := checkPath(t.ContextPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range t.NamedContexts {
|
|
|
|
if v.State != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := checkPath(v.Path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPath(p string) error {
|
|
|
|
if IsRemoteURL(p) || strings.HasPrefix(p, "target:") || strings.HasPrefix(p, "docker-image:") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p, err := filepath.EvalSymlinks(p)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rel, err := filepath.Rel(wd, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
|
|
|
return errors.Errorf("path %s is outside of the working directory, please set BAKE_ALLOW_REMOTE_FS_ACCESS=1", p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
|
2019-04-20 13:54:34 +08:00
|
|
|
if v := t.Context; v != nil && *v == "-" {
|
2019-04-05 15:04:19 +08:00
|
|
|
return nil, errors.Errorf("context from stdin not allowed in bake")
|
|
|
|
}
|
2019-04-20 13:54:34 +08:00
|
|
|
if v := t.Dockerfile; v != nil && *v == "-" {
|
2019-04-05 15:04:19 +08:00
|
|
|
return nil, errors.Errorf("dockerfile from stdin not allowed in bake")
|
|
|
|
}
|
|
|
|
|
2019-04-20 13:54:34 +08:00
|
|
|
contextPath := "."
|
|
|
|
if t.Context != nil {
|
|
|
|
contextPath = *t.Context
|
|
|
|
}
|
2021-07-13 08:53:04 +08:00
|
|
|
if !strings.HasPrefix(contextPath, "cwd://") && !IsRemoteURL(contextPath) {
|
|
|
|
contextPath = path.Clean(contextPath)
|
|
|
|
}
|
2019-04-20 13:54:34 +08:00
|
|
|
dockerfilePath := "Dockerfile"
|
|
|
|
if t.Dockerfile != nil {
|
|
|
|
dockerfilePath = *t.Dockerfile
|
|
|
|
}
|
|
|
|
|
2019-05-25 06:23:16 +08:00
|
|
|
if !isRemoteResource(contextPath) && !path.IsAbs(dockerfilePath) {
|
|
|
|
dockerfilePath = path.Join(contextPath, dockerfilePath)
|
|
|
|
}
|
|
|
|
|
2020-05-01 04:44:32 +08:00
|
|
|
noCache := false
|
|
|
|
if t.NoCache != nil {
|
|
|
|
noCache = *t.NoCache
|
|
|
|
}
|
|
|
|
pull := false
|
|
|
|
if t.Pull != nil {
|
|
|
|
pull = *t.Pull
|
|
|
|
}
|
2021-11-25 19:35:10 +08:00
|
|
|
networkMode := ""
|
|
|
|
if t.NetworkMode != nil {
|
|
|
|
networkMode = *t.NetworkMode
|
|
|
|
}
|
2020-05-01 04:44:32 +08:00
|
|
|
|
2020-09-17 13:52:40 +08:00
|
|
|
bi := build.Inputs{
|
|
|
|
ContextPath: contextPath,
|
|
|
|
DockerfilePath: dockerfilePath,
|
2022-02-24 15:09:46 +08:00
|
|
|
NamedContexts: toNamedContexts(t.Contexts),
|
2020-09-17 13:52:40 +08:00
|
|
|
}
|
2020-09-28 12:34:26 +08:00
|
|
|
if t.DockerfileInline != nil {
|
|
|
|
bi.DockerfileInline = *t.DockerfileInline
|
|
|
|
}
|
2020-09-17 13:52:40 +08:00
|
|
|
updateContext(&bi, inp)
|
2021-07-13 08:53:04 +08:00
|
|
|
if strings.HasPrefix(bi.ContextPath, "cwd://") {
|
|
|
|
bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://"))
|
|
|
|
}
|
2022-02-24 15:09:46 +08:00
|
|
|
for k, v := range bi.NamedContexts {
|
|
|
|
if strings.HasPrefix(v.Path, "cwd://") {
|
|
|
|
bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))}
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 08:53:04 +08:00
|
|
|
|
2022-02-24 16:05:42 +08:00
|
|
|
if err := validateContextsEntitlements(bi, inp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:53:04 +08:00
|
|
|
t.Context = &bi.ContextPath
|
2020-09-17 13:52:40 +08:00
|
|
|
|
2019-04-05 15:04:19 +08:00
|
|
|
bo := &build.Options{
|
2021-11-23 12:56:29 +08:00
|
|
|
Inputs: bi,
|
|
|
|
Tags: t.Tags,
|
|
|
|
BuildArgs: t.Args,
|
|
|
|
Labels: t.Labels,
|
|
|
|
NoCache: noCache,
|
|
|
|
NoCacheFilter: t.NoCacheFilter,
|
|
|
|
Pull: pull,
|
|
|
|
NetworkMode: networkMode,
|
2022-06-04 01:26:16 +08:00
|
|
|
Linked: t.linked,
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
2019-04-18 07:48:52 +08:00
|
|
|
platforms, err := platformutil.Parse(t.Platforms)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bo.Platforms = platforms
|
|
|
|
|
2022-07-06 16:47:29 +08:00
|
|
|
dockerConfig := config.LoadDefaultConfigFile(os.Stderr)
|
|
|
|
bo.Session = append(bo.Session, authprovider.NewDockerAuthProvider(dockerConfig))
|
2019-04-05 15:04:19 +08:00
|
|
|
|
2021-04-09 14:20:26 +08:00
|
|
|
secrets, err := buildflags.ParseSecretSpecs(t.Secrets)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bo.Session = append(bo.Session, secrets)
|
|
|
|
|
2021-04-02 02:08:56 +08:00
|
|
|
sshSpecs := t.SSH
|
2021-04-09 14:20:26 +08:00
|
|
|
if len(sshSpecs) == 0 && buildflags.IsGitSSH(contextPath) {
|
2021-04-02 02:08:56 +08:00
|
|
|
sshSpecs = []string{"default"}
|
|
|
|
}
|
2021-04-09 14:20:26 +08:00
|
|
|
ssh, err := buildflags.ParseSSHSpecs(sshSpecs)
|
2019-04-05 15:04:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bo.Session = append(bo.Session, ssh)
|
|
|
|
|
|
|
|
if t.Target != nil {
|
|
|
|
bo.Target = *t.Target
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:20:26 +08:00
|
|
|
cacheImports, err := buildflags.ParseCacheEntry(t.CacheFrom)
|
2019-04-18 14:07:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bo.CacheFrom = cacheImports
|
|
|
|
|
2021-04-09 14:20:26 +08:00
|
|
|
cacheExports, err := buildflags.ParseCacheEntry(t.CacheTo)
|
2019-04-18 14:07:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bo.CacheTo = cacheExports
|
|
|
|
|
2021-04-09 14:20:26 +08:00
|
|
|
outputs, err := buildflags.ParseOutputs(t.Outputs)
|
2019-04-24 05:18:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bo.Exports = outputs
|
|
|
|
|
2019-04-05 15:04:19 +08:00
|
|
|
return bo, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 09:00:18 +08:00
|
|
|
func defaultTarget() *Target {
|
|
|
|
return &Target{}
|
2019-04-05 15:04:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeDupes(s []string) []string {
|
|
|
|
i := 0
|
|
|
|
seen := make(map[string]struct{}, len(s))
|
|
|
|
for _, v := range s {
|
|
|
|
if _, ok := seen[v]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-26 12:56:18 +08:00
|
|
|
if v == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-05 15:04:19 +08:00
|
|
|
seen[v] = struct{}{}
|
|
|
|
s[i] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return s[:i]
|
|
|
|
}
|
2019-05-25 06:23:16 +08:00
|
|
|
|
|
|
|
func isRemoteResource(str string) bool {
|
|
|
|
return urlutil.IsGitURL(str) || urlutil.IsURL(str)
|
|
|
|
}
|
2021-09-21 07:22:27 +08:00
|
|
|
|
|
|
|
func parseOutputType(str string) string {
|
|
|
|
csvReader := csv.NewReader(strings.NewReader(str))
|
|
|
|
fields, err := csvReader.Read()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, field := range fields {
|
|
|
|
parts := strings.SplitN(field, "=", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
if parts[0] == "type" {
|
|
|
|
return parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2022-01-26 17:29:17 +08:00
|
|
|
|
|
|
|
func validateTargetName(name string) error {
|
|
|
|
if !targetNamePattern.MatchString(name) {
|
|
|
|
return errors.Errorf("only %q are allowed", validTargetNameChars)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-25 08:38:33 +08:00
|
|
|
|
2022-07-31 22:53:39 +08:00
|
|
|
func sanitizeTargetName(target string) string {
|
|
|
|
// as stipulated in compose spec, service name can contain a dot so as
|
|
|
|
// best-effort and to avoid any potential ambiguity, we replace the dot
|
|
|
|
// with an underscore.
|
|
|
|
return strings.ReplaceAll(target, ".", "_")
|
|
|
|
}
|
|
|
|
|
2022-01-25 08:38:33 +08:00
|
|
|
func sliceEqual(s1, s2 []string) bool {
|
|
|
|
if len(s1) != len(s2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sort.Strings(s1)
|
|
|
|
sort.Strings(s2)
|
|
|
|
for i := range s1 {
|
|
|
|
if s1[i] != s2[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-02-24 15:09:46 +08:00
|
|
|
|
|
|
|
func toNamedContexts(m map[string]string) map[string]build.NamedContext {
|
|
|
|
m2 := make(map[string]build.NamedContext, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
m2[k] = build.NamedContext{Path: v}
|
|
|
|
}
|
|
|
|
return m2
|
|
|
|
}
|