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.

Also moves remote and local files reading logic to a func

Signed-off-by: CrazyMax <[email protected]>
Signed-off-by: Cameron Adams <[email protected]>
Co-authored-by: CrazyMax <[email protected]>
  • Loading branch information
c-ameron and crazy-max committed Oct 24, 2023
1 parent ee19ce5 commit 208b042
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
50 changes: 40 additions & 10 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/containerd/console"
"github.com/containerd/containerd/platforms"
Expand Down Expand Up @@ -98,8 +100,6 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
defer cancel()

var nodes []builder.Node
var files []bake.File
var inp *bake.Input
var progressConsoleDesc, progressTextDesc string

// instance only needed for reading remote bake files or building
Expand Down Expand Up @@ -147,14 +147,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
}
}()

if url != "" {
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer)
} else {
progress.Wrap("[internal] load local bake definitions", printer.Write, func(sub progress.SubLogger) error {
files, err = bake.ReadLocalFiles(in.files, dockerCli.In(), sub)
return nil
})
}
files, inp, err := readBakeFiles(ctx, nodes, url, in.files, dockerCli.In(), printer)
if err != nil {
return err
}
Expand Down Expand Up @@ -296,3 +289,40 @@ func saveLocalStateGroup(dockerCli command.Cli, ref string, lsg localstate.State
}
return l.SaveGroup(ref, lsg)
}

func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names []string, stdin io.Reader, pw progress.Writer) (files []bake.File, inp *bake.Input, err error) {
var lnames []string
var rnames []string
for _, v := range names {
if strings.HasPrefix(v, "cwd://") {
lnames = append(lnames, strings.TrimPrefix(v, "cwd://"))
} else {
rnames = append(rnames, v)
}
}

if len(lnames) > 0 || url == "" {
progress.Wrap("[internal] load local bake definitions", pw.Write, func(sub progress.SubLogger) error {
if url != "" {
files, err = bake.ReadLocalFiles(lnames, stdin, sub)
} else {
files, err = bake.ReadLocalFiles(append(lnames, rnames...), stdin, sub)
}
return nil
})
if err != nil {
return nil, nil, err
}
}

if url != "" {
var rfiles []bake.File
rfiles, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, rnames, pw)
if err != nil {
return nil, nil, err
}
files = append(files, rfiles...)
}

return
}
56 changes: 51 additions & 5 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeLocalMulti,
testBakeRemote,
testBakeRemoteCmdContext,
testBakeRemoteLocalOverride,
testBakeRemoteCmdContextOverride,
testBakeRemoteContextSubdir,
testBakeRemoteCmdContextEscapeRoot,
Expand Down Expand Up @@ -82,12 +83,15 @@ services:
dirDest := t.TempDir()

cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--progress=plain", "--set", "*.output=type=local,dest="+dirDest))
out, err := cmd.CombinedOutput()
require.NoError(t, err, out)
require.Contains(t, string(out), `#1 [internal] load local bake definitions`)
require.Contains(t, string(out), `#1 reading compose.yaml`)
require.Contains(t, string(out), `#1 reading docker-bake.hcl`)
dt, err := cmd.CombinedOutput()
require.NoError(t, err, string(dt))
require.Contains(t, string(dt), `#1 [internal] load local bake definitions`)
require.Contains(t, string(dt), `#1 reading compose.yaml`)
require.Contains(t, string(dt), `#1 reading docker-bake.hcl`)
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"))
}

Expand Down Expand Up @@ -121,6 +125,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)

Check failure on line 165 in tests/bake.go

View workflow job for this annotation

GitHub Actions / test (docker, ./tests)

Failed: tests/TestIntegration/TestBakeRemoteLocalOverride/worker=docker

=== RUN TestIntegration/TestBakeRemoteLocalOverride/worker=docker === PAUSE TestIntegration/TestBakeRemoteLocalOverride/worker=docker === CONT TestIntegration/TestBakeRemoteLocalOverride/worker=docker bake.go:165: Error Trace: /src/tests/bake.go:165 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:91 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:205 Error: Received unexpected error: exit status 1 Test: TestIntegration/TestBakeRemoteLocalOverride/worker=docker Messages: Dockerfile:2 -------------------- 1 | FROM scratch 2 | >>> COPY foo /foo 3 | -------------------- ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 985e9454-96d6-4314-8a2d-a8cdf4c4b4ef::xjtqubv4clg3uh10sccrn0t8j: "/foo": not found sandbox.go:128: stdout: /usr/bin/dockerd sandbox.go:128: stderr: /usr/bin/dockerd sandbox.go:131: > startCmd 2023-10-24 15:55:19.947903881 +0000 UTC m=+37.163731319 /usr/bin/dockerd --data-root /tmp/integration2227563497/deqbrqwavwwlt/root --exec-root /tmp/dxr/deqbrqwavwwlt --pidfile /tmp/integration2227563497/deqbrqwavwwlt/docker.pid --containerd-namespace deqbrqwavwwlt --containerd-plugins-namespace deqbrqwavwwltp --host unix:///tmp/docker-integration/deqbrqwavwwlt.sock --config-file /tmp/integration2227563497/daemon.json --userland-proxy=false --tls=false --debug sandbox.go:131: time="2023-10-24T15:55:19.997956818Z" level=info msg="Starting up" sandbox.go:131: time="2023-10-24T15:55:19.999785634Z" level=warning msg="could not change group /tmp/docker-integration/deqbrqwavwwlt.sock to docker: group docker not found" sandbox.go:131: time="2023-10-24T15:55:20.000052537Z" level=debug msg="Listener created for HTTP on unix (/tmp/docker-integration/deqbrqwavwwlt.sock)" sandbox.go:131: time="2023-10-24T15:55:20.000076337Z" level=info msg="containerd not running, starting managed containerd" sandbox.go:131: time="2023-10-24T15:55:20.001021345Z" level=info msg="started new containerd process" address=/tmp/dxr/deqbrqwavwwlt/containerd/containerd.sock module=libcontainerd pid=8330 sandbox.go:131: time="2023-10-24T15:55:20.026849671Z" level=info msg="starting containerd" revision=7880925980b188f4c97b462f709d0db8e8962aff version=v1.7.3 sandbox.go:131: time="2023-10-24T15:55:20.048498460Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.aufs\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:20.049737570Z" level=info msg="skip loading plugin \"io.containerd.snapshotter.v1.aufs\"..." error="aufs is not supported (modprobe aufs failed: exit status 1 \"modprobe: can't change directory to '/lib/modules': No such file or directory\\n\"): skip plugin" type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:20.049766171Z" level=info msg="loading plugin \"io.containerd.content.v1.content\"..." type=io.containerd.content.v1 sandbox.go:131: time="2023-10-24T15:55:20.049912972Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.native\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:20.050003173Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.overlayfs\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:20.050221775Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.devmapper\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:20.050246175Z" level=warning msg="failed to load plugin io.containerd.snapshotter.v1.devmapper" error="devmapper not configured" sandbox.go:131: time="2023-10-24T15:55:20.050257575Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.zfs\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-2

Check failure on line 165 in tests/bake.go

View workflow job for this annotation

GitHub Actions / test (docker\+containerd, ./tests)

Failed: tests/TestIntegration/TestBakeRemoteLocalOverride/worker=docker+containerd

=== RUN TestIntegration/TestBakeRemoteLocalOverride/worker=docker+containerd === PAUSE TestIntegration/TestBakeRemoteLocalOverride/worker=docker+containerd === CONT TestIntegration/TestBakeRemoteLocalOverride/worker=docker+containerd bake.go:165: Error Trace: /src/tests/bake.go:165 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:91 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:205 Error: Received unexpected error: exit status 1 Test: TestIntegration/TestBakeRemoteLocalOverride/worker=docker+containerd Messages: Dockerfile:2 -------------------- 1 | FROM scratch 2 | >>> COPY foo /foo 3 | -------------------- ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref z7c5ewx8mdnrrp4bn7yn5yl36::5rkgi7tkok7xl1dgczna3s0jn: "/foo": not found sandbox.go:128: stdout: /usr/bin/dockerd sandbox.go:128: stderr: /usr/bin/dockerd sandbox.go:131: > startCmd 2023-10-24 15:55:43.52161016 +0000 UTC m=+32.861013338 /usr/bin/dockerd --data-root /tmp/integration1900188841/dz55v7qe00lyy/root --exec-root /tmp/dxr/dz55v7qe00lyy --pidfile /tmp/integration1900188841/dz55v7qe00lyy/docker.pid --containerd-namespace dz55v7qe00lyy --containerd-plugins-namespace dz55v7qe00lyyp --host unix:///tmp/docker-integration/dz55v7qe00lyy.sock --config-file /tmp/integration1900188841/daemon.json --userland-proxy=false --tls=false --debug sandbox.go:131: time="2023-10-24T15:55:43.581964397Z" level=info msg="Starting up" sandbox.go:131: time="2023-10-24T15:55:43.585055350Z" level=warning msg="could not change group /tmp/docker-integration/dz55v7qe00lyy.sock to docker: group docker not found" sandbox.go:131: time="2023-10-24T15:55:43.585312771Z" level=debug msg="Listener created for HTTP on unix (/tmp/docker-integration/dz55v7qe00lyy.sock)" sandbox.go:131: time="2023-10-24T15:55:43.585353875Z" level=info msg="containerd not running, starting managed containerd" sandbox.go:131: time="2023-10-24T15:55:43.586114437Z" level=info msg="started new containerd process" address=/tmp/dxr/dz55v7qe00lyy/containerd/containerd.sock module=libcontainerd pid=8139 sandbox.go:131: time="2023-10-24T15:55:43.609127119Z" level=info msg="starting containerd" revision=7880925980b188f4c97b462f709d0db8e8962aff version=v1.7.3 sandbox.go:131: time="2023-10-24T15:55:43.630896200Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.aufs\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:43.631941385Z" level=info msg="skip loading plugin \"io.containerd.snapshotter.v1.aufs\"..." error="aufs is not supported (modprobe aufs failed: exit status 1 \"modprobe: can't change directory to '/lib/modules': No such file or directory\\n\"): skip plugin" type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:43.632013291Z" level=info msg="loading plugin \"io.containerd.content.v1.content\"..." type=io.containerd.content.v1 sandbox.go:131: time="2023-10-24T15:55:43.632189506Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.native\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:43.632326717Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.overlayfs\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:43.632629742Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.devmapper\"..." type=io.containerd.snapshotter.v1 sandbox.go:131: time="2023-10-24T15:55:43.632675245Z" level=warning msg="failed to load plugin io.containerd.snapshotter.v1.devmapper" error="devmapper not configured" sandbox.go:131: time="2023-10-24T15:55:43.632716249Z" level=info msg="loading plugin \"io.containerd.snapshotter.v1.zfs\"..." type=io.containerd.snapshotter.v1

Check failure on line 165 in tests/bake.go

View workflow job for this annotation

GitHub Actions / test (docker-container, ./tests)

Failed: tests/TestIntegration/TestBakeRemoteLocalOverride/worker=docker-container

=== RUN TestIntegration/TestBakeRemoteLocalOverride/worker=docker-container === PAUSE TestIntegration/TestBakeRemoteLocalOverride/worker=docker-container === CONT TestIntegration/TestBakeRemoteLocalOverride/worker=docker-container bake.go:165: Error Trace: /src/tests/bake.go:165 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:91 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:205 Error: Received unexpected error: exit status 1 Test: TestIntegration/TestBakeRemoteLocalOverride/worker=docker-container Messages: Dockerfile:2 -------------------- 1 | FROM scratch 2 | >>> COPY foo /foo 3 | -------------------- ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 0ybhs78r2dbwp7mctix6v0uah::dd7or0w17rgp4s40d4p06g3f8: "/foo": not found --- FAIL: TestIntegration/TestBakeRemoteLocalOverride/worker=docker-container (4.26s)

Check failure on line 165 in tests/bake.go

View workflow job for this annotation

GitHub Actions / test (remote, ./tests)

Failed: tests/TestIntegration/TestBakeRemoteLocalOverride/worker=remote

=== RUN TestIntegration/TestBakeRemoteLocalOverride/worker=remote === PAUSE TestIntegration/TestBakeRemoteLocalOverride/worker=remote === CONT TestIntegration/TestBakeRemoteLocalOverride/worker=remote bake.go:165: Error Trace: /src/tests/bake.go:165 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:91 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:205 Error: Received unexpected error: exit status 1 Test: TestIntegration/TestBakeRemoteLocalOverride/worker=remote Messages: Dockerfile:2 -------------------- 1 | FROM scratch 2 | >>> COPY foo /foo 3 | -------------------- ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref rjibpae7zx87n36svovpblbfo::mlc9kvz7a4xssnt9zu9gyhusn: "/foo": not found sandbox.go:128: stdout: /usr/bin/buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktest_config585760607/buildkitd.toml --root /tmp/bktest_buildkitd2831569426 --addr unix:///tmp/bktest_buildkitd2831569426/buildkitd.sock --debug sandbox.go:128: stderr: /usr/bin/buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktest_config585760607/buildkitd.toml --root /tmp/bktest_buildkitd2831569426 --addr unix:///tmp/bktest_buildkitd2831569426/buildkitd.sock --debug sandbox.go:131: > StartCmd 2023-10-24 15:54:01.194386657 +0000 UTC m=+15.538907577 /usr/bin/buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktest_config585760607/buildkitd.toml --root /tmp/bktest_buildkitd2831569426 --addr unix:///tmp/bktest_buildkitd2831569426/buildkitd.sock --debug sandbox.go:131: time="2023-10-24T15:54:01Z" level=info msg="auto snapshotter: using overlayfs" sandbox.go:131: time="2023-10-24T15:54:01Z" level=warning msg="using host network as the default" sandbox.go:131: time="2023-10-24T15:54:01Z" level=info msg="found worker \"rjibpae7zx87n36svovpblbfo\", labels=map[org.mobyproject.buildkit.worker.executor:oci org.mobyproject.buildkit.worker.hostname:a834fb092661 org.mobyproject.buildkit.worker.network:host org.mobyproject.buildkit.worker.oci.process-mode:sandbox org.mobyproject.buildkit.worker.sandbox:true org.mobyproject.buildkit.worker.selinux.enabled:false org.mobyproject.buildkit.worker.snapshotter:overlayfs], platforms=[linux/amd64 linux/amd64/v2 linux/amd64/v3 linux/amd64/v4 linux/arm64 linux/riscv64 linux/ppc64le linux/s390x linux/386 linux/mips64le linux/mips64 linux/arm/v7 linux/arm/v6]" sandbox.go:131: time="2023-10-24T15:54:01Z" level=info msg="found 1 workers, default=\"rjibpae7zx87n36svovpblbfo\"" sandbox.go:131: time="2023-10-24T15:54:01Z" level=warning msg="currently, only the default worker can be used." sandbox.go:131: time="2023-10-24T15:54:01Z" level=info msg="running server on /tmp/bktest_buildkitd2831569426/buildkitd.sock" sandbox.go:131: time="2023-10-24T15:54:01Z" level=debug msg="session started" spanID=4451a03e5d6c5f07 traceID=94b15f5ec6ed1c81c445f67d05718adb sandbox.go:131: time="2023-10-24T15:54:01Z" level=error msg="/moby.buildkit.v1.frontend.LLBBridge/StatFile returned error: rpc error: code = Unknown desc = lstat /tmp/bktest_buildkitd2831569426/tmp/buildkit-mount1460531283/compose.yaml: no such file or directory" sandbox.go:131: lstat /tmp/bktest_buildkitd2831569426/tmp/buildkit-mount1460531283/compose.yaml: no such file or directory sandbox.go:131: 9944 v0.11.6 buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktes

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 208b042

Please sign in to comment.