Skip to content

Commit

Permalink
command(bake): Specify local and remote bake files
Browse files Browse the repository at this point in the history
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 <pnzreba@gmail.com>
  • Loading branch information
c-ameron authored and jedevc committed Aug 13, 2023
1 parent 86ae8ea commit 78616f2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
28 changes: 25 additions & 3 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/containerd/console"
"github.com/containerd/containerd/platforms"
Expand Down Expand Up @@ -142,11 +143,19 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
}
}
}()

localFilesArg, remoteFilesArg := getFilesArgs(in.files)
if url != "" {
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer)
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, remoteFilesArg, printer)
if len(localFilesArg) > 0 {
var localFiles []bake.File
localFiles, err = bake.ReadLocalFiles(localFilesArg, dockerCli.In())
files = append(files, localFiles...)
}
} else {
files, err = bake.ReadLocalFiles(in.files, dockerCli.In())
var filesArg []string
filesArg = append(filesArg, localFilesArg...)
filesArg = append(filesArg, remoteFilesArg...)
files, err = bake.ReadLocalFiles(filesArg, dockerCli.In())
}
if err != nil {
return err
Expand Down Expand Up @@ -257,3 +266,16 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {

return cmd
}

func getFilesArgs(s []string) ([]string, []string) {
var localFiles []string
var remoteFiles []string
for _, v := range s {
if strings.HasPrefix(v, "cwd://") {
localFiles = append(localFiles, strings.TrimPrefix(v, "cwd://"))
} else {
remoteFiles = append(remoteFiles, v)
}
}
return localFiles, remoteFiles
}
48 changes: 48 additions & 0 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeLocal,
testBakeRemote,
testBakeRemoteCmdContext,
testBakeRemoteLocalOverride,
testBakeRemoteCmdContextOverride,
testBakeRemoteContextSubdir,
testBakeRemoteCmdContextEscapeRoot,
Expand Down Expand Up @@ -48,6 +49,11 @@ target "default" {
require.NoError(t, err, out)

require.FileExists(t, filepath.Join(dirDest, "foo"))

out, err = bakeCmd(sb, withDir(dir), withArgs("--file", "cwd://docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest))
require.NoError(t, err, out)

require.FileExists(t, filepath.Join(dirDest, "foo"))
}

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

func testBakeRemoteLocalOverride(t *testing.T, sb integration.Sandbox) {
remoteBakefile := []byte(`
target "default" {
dockerfile-inline = <<EOT
FROM scratch
COPY foo /foo
EOT
}
`)
localBakefile := []byte(`
target "default" {
dockerfile-inline = <<EOT
FROM scratch
COPY bar /bar
EOT
}
`)
dirSpec := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", remoteBakefile, 0600),
fstest.CreateFile("bar", []byte("bar"), 0600),
)
dirSrc := tmpdir(
t,
fstest.CreateFile("local-docker-bake.hcl", localBakefile, 0600),
)
dirDest := t.TempDir()

git, err := gitutil.New(gitutil.WithWorkingDir(dirSpec))
require.NoError(t, err)

gitutil.GitInit(git, t)
gitutil.GitAdd(git, t, "docker-bake.hcl", "bar")
gitutil.GitCommit(git, t, "initial commit")
addr := gitutil.GitServeHTTP(git, t)

out, err := bakeCmd(sb, withDir(dirSrc), withArgs(addr, "--file", "cwd://local-docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest))
require.NoError(t, err, out)

require.FileExists(t, filepath.Join(dirDest, "bar"))
}

func testBakeRemoteCmdContext(t *testing.T, sb integration.Sandbox) {
bakefile := []byte(`
target "default" {
Expand Down

0 comments on commit 78616f2

Please sign in to comment.