pull/1992/merge
CrazyMax 2 years ago committed by GitHub
commit 0cc80365e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,7 +4,11 @@ import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"context" "context"
"encoding/json"
"os"
"github.com/compose-spec/compose-go/loader"
compose "github.com/compose-spec/compose-go/types"
"github.com/docker/buildx/builder" "github.com/docker/buildx/builder"
controllerapi "github.com/docker/buildx/controller/pb" controllerapi "github.com/docker/buildx/controller/pb"
"github.com/docker/buildx/driver" "github.com/docker/buildx/driver"
@ -182,10 +186,11 @@ func filesFromRef(ctx context.Context, ref gwclient.Reference, names []string) (
} }
for _, name := range names { for _, name := range names {
f, err := readRefFile(name, func(name string) (*File, error) {
_, err := ref.StatFile(ctx, gwclient.StatRequest{Path: name}) _, err := ref.StatFile(ctx, gwclient.StatRequest{Path: name})
if err != nil { if err != nil {
if isDefault { if isDefault {
continue return nil, nil
} }
return nil, err return nil, err
} }
@ -193,8 +198,79 @@ func filesFromRef(ctx context.Context, ref gwclient.Reference, names []string) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
files = append(files, File{Name: name, Data: dt}) return &File{Name: name, Data: dt}, nil
}, nil)
if err != nil {
return nil, err
} else if len(f) == 0 {
continue
}
files = append(files, f...)
}
return files, nil
} }
func readRefFile(name string, readFunc func(name string) (*File, error), files []File) ([]File, error) {
f, err := readFunc(name)
if err != nil {
return nil, err
} else if f == nil {
return files, nil
}
// if we have a compose file, we need to read it and extract the
// include files from it
_, err = loader.Load(compose.ConfigDetails{
ConfigFiles: []compose.ConfigFile{
{
Content: f.Data,
},
},
Environment: sliceToMap(os.Environ()),
}, func(options *loader.Options) {
options.SetProjectName("bake", false)
options.SkipNormalization = true
options.SkipConsistencyCheck = true
options.SkipInclude = true
})
if err == nil {
yml, err := loader.ParseYAML(f.Data)
if err != nil {
return nil, err
}
if v, ok := yml["include"]; ok && v != "" {
// include files will be read later and added to the list of files
// to be processed, so we can remote "include" from the yaml file
delete(yml, "include")
// marshal the yaml file again without the include key and add it
// to the list of files to be processed
newData, err := json.Marshal(yml)
if err != nil {
return nil, err
}
files = append(files, File{
Name: f.Name,
Data: newData,
})
// read include files
if idt, ok := v.([]interface{}); ok && len(idt) > 0 {
var includes []string
for _, i := range idt {
includes = append(includes, i.(string))
}
for _, include := range includes {
incf, err := readRefFile(include, readFunc, files)
if err != nil {
return nil, err
} else if incf == nil {
continue
}
files = append(files, incf...)
}
}
}
} else {
files = append(files, *f)
}
return files, nil return files, nil
} }

@ -25,6 +25,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeRemoteContextSubdir, testBakeRemoteContextSubdir,
testBakeRemoteCmdContextEscapeRoot, testBakeRemoteCmdContextEscapeRoot,
testBakeRemoteCmdContextEscapeRelative, testBakeRemoteCmdContextEscapeRelative,
testBakeRemoteComposeInclude,
} }
func testBakeLocal(t *testing.T, sb integration.Sandbox) { func testBakeLocal(t *testing.T, sb integration.Sandbox) {
@ -287,3 +288,47 @@ EOT
require.NoError(t, err, out) require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "foo")) require.FileExists(t, filepath.Join(dirDest, "foo"))
} }
func testBakeRemoteComposeInclude(t *testing.T, sb integration.Sandbox) {
composeFile := []byte(`
include:
- compose-inc.yml
services:
foo:
build:
context: .
dockerfile_inline: |
FROM busybox
RUN echo foo
`)
composeIncFile := []byte(`
services:
inc:
build:
context: .
dockerfile_inline: |
FROM busybox
RUN echo inc
`)
dirSpec := tmpdir(
t,
fstest.CreateFile("compose.yml", composeFile, 0600),
fstest.CreateFile("compose-inc.yml", composeIncFile, 0600),
)
git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec))
require.NoError(t, err)
gitutil.GitInit(git, t)
gitutil.GitAdd(git, t, "compose.yml", "compose-inc.yml")
gitutil.GitCommit(git, t, "initial commit")
addr := gitutil.GitServeHTTP(git, t)
out, err := bakeCmd(
sb,
withArgs(addr, "--set", "*.output=type=cacheonly"),
)
require.NoError(t, err, out)
}

Loading…
Cancel
Save