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 'local://' will source the 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 local://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 committed May 24, 2023
1 parent e5f7013 commit 99761bf
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion 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/containerd/platforms"
"github.com/docker/buildx/bake"
Expand Down Expand Up @@ -134,7 +135,18 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
}()

if url != "" {
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer)
if containsLocalFiles(in.files) {
var remoteFiles []bake.File
remoteFiles, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, getRemoteFiles(in.files), printer)
if err != nil {
return err
}
files, err = bake.ReadLocalFiles(getLocalFiles(in.files))
files = append(files, remoteFiles...)

} else {
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer)
}
} else {
files, err = bake.ReadLocalFiles(in.files)
}
Expand Down Expand Up @@ -247,3 +259,32 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {

return cmd
}

func containsLocalFiles(s []string) bool {
for _, v := range s {
if strings.Contains(v, "local://") {
return true
}
}
return false
}

func getLocalFiles(s []string) []string {
var files []string
for _, v := range s {
if strings.Contains(v, "local://") {
files = append(files, strings.TrimPrefix(v, "local://"))
}
}
return files
}

func getRemoteFiles(s []string) []string {
var files []string
for _, v := range s {
if !strings.Contains(v, "local://") {
files = append(files, v)
}
}
return files
}

0 comments on commit 99761bf

Please sign in to comment.