Skip to content

Commit 5638919

Browse files
committed
command(bake): Specify local and remote bake files
This adds the ability to source additional local build definition files when sourcing Bake files via a remote url. Prefixing a file with 'cwd://' will source a bake file on the local machine, instead of the remote location. Local files will be read/have precedence before remote files. Usage: ``` docker buildx bake https://github.com/example/upstream.git --file cwd://docker-bake.override.hcl --print ``` This will source a default file from the example/upstream repository, and also source a build definition from the local machine. Signed-off-by: Cameron Adams <[email protected]>
1 parent 86ae8ea commit 5638919

2 files changed

Lines changed: 81 additions & 3 deletions

File tree

commands/bake.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"strings"
89

910
"github.com/containerd/console"
1011
"github.com/containerd/containerd/platforms"
@@ -142,11 +143,17 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
142143
}
143144
}
144145
}()
145-
146146
if url != "" {
147-
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer)
147+
localFilesArg, remoteFilesArg := getFilesArgs(in.files, true)
148+
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, remoteFilesArg, printer)
149+
if len(localFilesArg) > 0 {
150+
var localFiles []bake.File
151+
localFiles, err = bake.ReadLocalFiles(localFilesArg, dockerCli.In())
152+
files = append(files, localFiles...)
153+
}
148154
} else {
149-
files, err = bake.ReadLocalFiles(in.files, dockerCli.In())
155+
localFilesArg, _ := getFilesArgs(in.files, false)
156+
files, err = bake.ReadLocalFiles(localFilesArg, dockerCli.In())
150157
}
151158
if err != nil {
152159
return err
@@ -257,3 +264,26 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
257264

258265
return cmd
259266
}
267+
268+
func getFilesArgs(s []string, isRemote bool) ([]string, []string) {
269+
var localFiles []string
270+
var remoteFiles []string
271+
if isRemote {
272+
for _, v := range s {
273+
if strings.HasPrefix(v, "cwd://") {
274+
localFiles = append(localFiles, strings.TrimPrefix(v, "cwd://"))
275+
} else {
276+
remoteFiles = append(remoteFiles, v)
277+
}
278+
}
279+
} else {
280+
for _, v := range s {
281+
if strings.HasPrefix(v, "cwd://") {
282+
localFiles = append(localFiles, strings.TrimPrefix(v, "cwd://"))
283+
} else {
284+
remoteFiles = append(localFiles, v)
285+
}
286+
}
287+
}
288+
return localFiles, remoteFiles
289+
}

tests/bake.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
2121
testBakeLocal,
2222
testBakeRemote,
2323
testBakeRemoteCmdContext,
24+
testBakeRemoteLocalOverride,
2425
testBakeRemoteCmdContextOverride,
2526
testBakeRemoteContextSubdir,
2627
testBakeRemoteCmdContextEscapeRoot,
@@ -48,6 +49,11 @@ target "default" {
4849
require.NoError(t, err, out)
4950

5051
require.FileExists(t, filepath.Join(dirDest, "foo"))
52+
53+
out, err = bakeCmd(sb, withDir(dir), withArgs("--file", "cwd://docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest))
54+
require.NoError(t, err, out)
55+
56+
require.FileExists(t, filepath.Join(dirDest, "foo"))
5157
}
5258

5359
func testBakeRemote(t *testing.T, sb integration.Sandbox) {
@@ -80,6 +86,48 @@ EOT
8086
require.FileExists(t, filepath.Join(dirDest, "foo"))
8187
}
8288

89+
func testBakeRemoteLocalOverride(t *testing.T, sb integration.Sandbox) {
90+
remoteBakefile := []byte(`
91+
target "default" {
92+
dockerfile-inline = <<EOT
93+
FROM scratch
94+
COPY foo /foo
95+
EOT
96+
}
97+
`)
98+
localBakefile := []byte(`
99+
target "default" {
100+
dockerfile-inline = <<EOT
101+
FROM scratch
102+
COPY bar /bar
103+
EOT
104+
}
105+
`)
106+
dirSpec := tmpdir(
107+
t,
108+
fstest.CreateFile("docker-bake.hcl", remoteBakefile, 0600),
109+
fstest.CreateFile("bar", []byte("bar"), 0600),
110+
)
111+
dirSrc := tmpdir(
112+
t,
113+
fstest.CreateFile("local-docker-bake.hcl", localBakefile, 0600),
114+
)
115+
dirDest := t.TempDir()
116+
117+
git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec))
118+
require.NoError(t, err)
119+
120+
gitutil.GitInit(git, t)
121+
gitutil.GitAdd(git, t, "docker-bake.hcl", "bar")
122+
gitutil.GitCommit(git, t, "initial commit")
123+
addr := gitutil.GitServeHTTP(git, t)
124+
125+
out, err := bakeCmd(sb, withDir(dirSrc), withArgs(addr, "--file", "cwd://local-docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest))
126+
require.NoError(t, err, out)
127+
128+
require.FileExists(t, filepath.Join(dirDest, "bar"))
129+
}
130+
83131
func testBakeRemoteCmdContext(t *testing.T, sb integration.Sandbox) {
84132
bakefile := []byte(`
85133
target "default" {

0 commit comments

Comments
 (0)