mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-01 00:23:56 +08:00 
			
		
		
		
	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. Also moves remote and local files reading logic to a func Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com> Signed-off-by: Cameron Adams <pnzreba@gmail.com> Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
		| @@ -23,6 +23,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ | ||||
| 	testBakeLocalMulti, | ||||
| 	testBakeRemote, | ||||
| 	testBakeRemoteCmdContext, | ||||
| 	testBakeRemoteLocalOverride, | ||||
| 	testBakeRemoteCmdContextOverride, | ||||
| 	testBakeRemoteContextSubdir, | ||||
| 	testBakeRemoteCmdContextEscapeRoot, | ||||
| @@ -46,6 +47,7 @@ target "default" { | ||||
| 		fstest.CreateFile("Dockerfile", dockerfile, 0600), | ||||
| 		fstest.CreateFile("foo", []byte("foo"), 0600), | ||||
| 	) | ||||
|  | ||||
| 	dirDest := t.TempDir() | ||||
|  | ||||
| 	cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--progress=plain", "--set", "*.output=type=local,dest="+dirDest)) | ||||
| @@ -79,16 +81,23 @@ services: | ||||
| 		fstest.CreateFile("Dockerfile", dockerfile, 0600), | ||||
| 		fstest.CreateFile("foo", []byte("foo"), 0600), | ||||
| 	) | ||||
|  | ||||
| 	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")) | ||||
|  | ||||
| 	dirDest2 := t.TempDir() | ||||
|  | ||||
| 	out, err := bakeCmd(sb, withDir(dir), withArgs("--file", "cwd://docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest2)) | ||||
| 	require.NoError(t, err, out) | ||||
|  | ||||
| 	require.FileExists(t, filepath.Join(dirDest2, "foo")) | ||||
| } | ||||
|  | ||||
| func testBakeRemote(t *testing.T, sb integration.Sandbox) { | ||||
| @@ -121,6 +130,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" { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Cameron Adams
					Cameron Adams