mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-31 18:05:46 +08:00
Merge pull request #2560 from crazy-max/fix-localstate-remote
build: fix localstate for remote context and stdin
This commit is contained in:
commit
63eb73d9cf
@ -15,18 +15,20 @@ func saveLocalState(so *client.SolveOpt, target string, opts Options, node build
|
|||||||
}
|
}
|
||||||
lp := opts.Inputs.ContextPath
|
lp := opts.Inputs.ContextPath
|
||||||
dp := opts.Inputs.DockerfilePath
|
dp := opts.Inputs.DockerfilePath
|
||||||
if lp != "" || dp != "" {
|
if dp != "" && !IsRemoteURL(lp) && lp != "-" && dp != "-" {
|
||||||
if lp != "" {
|
dp, err = filepath.Abs(dp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lp != "" && !IsRemoteURL(lp) && lp != "-" {
|
||||||
lp, err = filepath.Abs(lp)
|
lp, err = filepath.Abs(lp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if dp != "" {
|
if lp == "" && dp == "" {
|
||||||
dp, err = filepath.Abs(dp)
|
return nil
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
l, err := localstate.New(configDir)
|
l, err := localstate.New(configDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -39,5 +41,3 @@ func saveLocalState(so *client.SolveOpt, target string, opts Options, node build
|
|||||||
GroupRef: opts.GroupRef,
|
GroupRef: opts.GroupRef,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -21,9 +21,10 @@ const (
|
|||||||
type State struct {
|
type State struct {
|
||||||
// Target is the name of the invoked target (default if empty)
|
// Target is the name of the invoked target (default if empty)
|
||||||
Target string
|
Target string
|
||||||
// LocalPath is the absolute path to the context
|
// LocalPath is the absolute path to the context or remote context
|
||||||
LocalPath string
|
LocalPath string
|
||||||
// DockerfilePath is the absolute path to the Dockerfile
|
// DockerfilePath is the absolute path to the Dockerfile or relative if
|
||||||
|
// context is remote
|
||||||
DockerfilePath string
|
DockerfilePath string
|
||||||
// GroupRef is the ref of the state group that this ref belongs to
|
// GroupRef is the ref of the state group that this ref belongs to
|
||||||
GroupRef string `json:",omitempty"`
|
GroupRef string `json:",omitempty"`
|
||||||
|
170
tests/build.go
170
tests/build.go
@ -16,6 +16,8 @@ import (
|
|||||||
"github.com/containerd/containerd/platforms"
|
"github.com/containerd/containerd/platforms"
|
||||||
"github.com/containerd/continuity/fs/fstest"
|
"github.com/containerd/continuity/fs/fstest"
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
|
"github.com/docker/buildx/localstate"
|
||||||
|
"github.com/docker/buildx/util/gitutil"
|
||||||
"github.com/moby/buildkit/client"
|
"github.com/moby/buildkit/client"
|
||||||
"github.com/moby/buildkit/frontend/subrequests/lint"
|
"github.com/moby/buildkit/frontend/subrequests/lint"
|
||||||
"github.com/moby/buildkit/frontend/subrequests/outline"
|
"github.com/moby/buildkit/frontend/subrequests/outline"
|
||||||
@ -42,6 +44,10 @@ func buildCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
|
|||||||
var buildTests = []func(t *testing.T, sb integration.Sandbox){
|
var buildTests = []func(t *testing.T, sb integration.Sandbox){
|
||||||
testBuild,
|
testBuild,
|
||||||
testBuildStdin,
|
testBuildStdin,
|
||||||
|
testBuildRemote,
|
||||||
|
testBuildLocalState,
|
||||||
|
testBuildLocalStateStdin,
|
||||||
|
testBuildLocalStateRemote,
|
||||||
testImageIDOutput,
|
testImageIDOutput,
|
||||||
testBuildLocalExport,
|
testBuildLocalExport,
|
||||||
testBuildRegistryExport,
|
testBuildRegistryExport,
|
||||||
@ -95,6 +101,170 @@ COPY --from=base /etc/bar /bar
|
|||||||
require.NoError(t, err, string(out))
|
require.NoError(t, err, string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testBuildRemote(t *testing.T, sb integration.Sandbox) {
|
||||||
|
dockerfile := []byte(`
|
||||||
|
FROM busybox:latest
|
||||||
|
COPY foo /foo
|
||||||
|
`)
|
||||||
|
dir := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("Dockerfile", dockerfile, 0600),
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
dirDest := t.TempDir()
|
||||||
|
|
||||||
|
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
gitutil.GitInit(git, t)
|
||||||
|
gitutil.GitAdd(git, t, "Dockerfile", "foo")
|
||||||
|
gitutil.GitCommit(git, t, "initial commit")
|
||||||
|
addr := gitutil.GitServeHTTP(git, t)
|
||||||
|
|
||||||
|
out, err := buildCmd(sb, withDir(dir), withArgs("--output=type=local,dest="+dirDest, addr))
|
||||||
|
require.NoError(t, err, out)
|
||||||
|
require.FileExists(t, filepath.Join(dirDest, "foo"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBuildLocalState(t *testing.T, sb integration.Sandbox) {
|
||||||
|
dockerfile := []byte(`
|
||||||
|
FROM busybox:latest AS base
|
||||||
|
COPY foo /etc/foo
|
||||||
|
RUN cp /etc/foo /etc/bar
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
COPY --from=base /etc/bar /bar
|
||||||
|
`)
|
||||||
|
dir := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("build.Dockerfile", dockerfile, 0600),
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
|
||||||
|
out, err := buildCmd(sb, withDir(dir), withArgs(
|
||||||
|
"-f", "build.Dockerfile",
|
||||||
|
"--metadata-file", filepath.Join(dir, "md.json"),
|
||||||
|
".",
|
||||||
|
))
|
||||||
|
require.NoError(t, err, out)
|
||||||
|
|
||||||
|
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
type mdT struct {
|
||||||
|
BuildRef string `json:"buildx.build.ref"`
|
||||||
|
}
|
||||||
|
var md mdT
|
||||||
|
err = json.Unmarshal(dt, &md)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ls, err := localstate.New(buildxConfig(sb))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
refParts := strings.Split(md.BuildRef, "/")
|
||||||
|
require.Len(t, refParts, 3)
|
||||||
|
|
||||||
|
ref, err := ls.ReadRef(refParts[0], refParts[1], refParts[2])
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, ref)
|
||||||
|
require.DirExists(t, ref.LocalPath)
|
||||||
|
require.FileExists(t, ref.DockerfilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBuildLocalStateStdin(t *testing.T, sb integration.Sandbox) {
|
||||||
|
dockerfile := []byte(`
|
||||||
|
FROM busybox:latest AS base
|
||||||
|
COPY foo /etc/foo
|
||||||
|
RUN cp /etc/foo /etc/bar
|
||||||
|
|
||||||
|
FROM scratch
|
||||||
|
COPY --from=base /etc/bar /bar
|
||||||
|
`)
|
||||||
|
dir := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd := buildxCmd(sb, withDir(dir), withArgs("build", "--progress=quiet", "--metadata-file", filepath.Join(dir, "md.json"), "-f-", dir))
|
||||||
|
cmd.Stdin = bytes.NewReader(dockerfile)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, string(out))
|
||||||
|
|
||||||
|
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
type mdT struct {
|
||||||
|
BuildRef string `json:"buildx.build.ref"`
|
||||||
|
}
|
||||||
|
var md mdT
|
||||||
|
err = json.Unmarshal(dt, &md)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ls, err := localstate.New(buildxConfig(sb))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
refParts := strings.Split(md.BuildRef, "/")
|
||||||
|
require.Len(t, refParts, 3)
|
||||||
|
|
||||||
|
ref, err := ls.ReadRef(refParts[0], refParts[1], refParts[2])
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, ref)
|
||||||
|
require.DirExists(t, ref.LocalPath)
|
||||||
|
require.Equal(t, "-", ref.DockerfilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBuildLocalStateRemote(t *testing.T, sb integration.Sandbox) {
|
||||||
|
dockerfile := []byte(`
|
||||||
|
FROM busybox:latest
|
||||||
|
COPY foo /foo
|
||||||
|
`)
|
||||||
|
dir := tmpdir(
|
||||||
|
t,
|
||||||
|
fstest.CreateFile("build.Dockerfile", dockerfile, 0600),
|
||||||
|
fstest.CreateFile("foo", []byte("foo"), 0600),
|
||||||
|
)
|
||||||
|
dirDest := t.TempDir()
|
||||||
|
|
||||||
|
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
gitutil.GitInit(git, t)
|
||||||
|
gitutil.GitAdd(git, t, "build.Dockerfile", "foo")
|
||||||
|
gitutil.GitCommit(git, t, "initial commit")
|
||||||
|
addr := gitutil.GitServeHTTP(git, t)
|
||||||
|
|
||||||
|
out, err := buildCmd(sb, withDir(dir), withArgs(
|
||||||
|
"-f", "build.Dockerfile",
|
||||||
|
"--metadata-file", filepath.Join(dirDest, "md.json"),
|
||||||
|
"--output", "type=local,dest="+dirDest,
|
||||||
|
addr,
|
||||||
|
))
|
||||||
|
require.NoError(t, err, out)
|
||||||
|
require.FileExists(t, filepath.Join(dirDest, "foo"))
|
||||||
|
|
||||||
|
dt, err := os.ReadFile(filepath.Join(dirDest, "md.json"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
type mdT struct {
|
||||||
|
BuildRef string `json:"buildx.build.ref"`
|
||||||
|
}
|
||||||
|
var md mdT
|
||||||
|
err = json.Unmarshal(dt, &md)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ls, err := localstate.New(buildxConfig(sb))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
refParts := strings.Split(md.BuildRef, "/")
|
||||||
|
require.Len(t, refParts, 3)
|
||||||
|
|
||||||
|
ref, err := ls.ReadRef(refParts[0], refParts[1], refParts[2])
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, ref)
|
||||||
|
require.Equal(t, addr, ref.LocalPath)
|
||||||
|
require.Equal(t, "build.Dockerfile", ref.DockerfilePath)
|
||||||
|
}
|
||||||
|
|
||||||
func testBuildLocalExport(t *testing.T, sb integration.Sandbox) {
|
func testBuildLocalExport(t *testing.T, sb integration.Sandbox) {
|
||||||
dir := createTestProject(t)
|
dir := createTestProject(t)
|
||||||
out, err := buildCmd(sb, withArgs(fmt.Sprintf("--output=type=local,dest=%s/result", dir), dir))
|
out, err := buildCmd(sb, withArgs(fmt.Sprintf("--output=type=local,dest=%s/result", dir), dir))
|
||||||
|
@ -56,7 +56,7 @@ func buildxCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd {
|
|||||||
|
|
||||||
if builder := sb.Address(); builder != "" {
|
if builder := sb.Address(); builder != "" {
|
||||||
cmd.Env = append(cmd.Env,
|
cmd.Env = append(cmd.Env,
|
||||||
"BUILDX_CONFIG=/tmp/buildx-"+builder,
|
"BUILDX_CONFIG="+buildxConfig(sb),
|
||||||
"BUILDX_BUILDER="+builder,
|
"BUILDX_BUILDER="+builder,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -86,6 +86,13 @@ func dockerCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func buildxConfig(sb integration.Sandbox) string {
|
||||||
|
if builder := sb.Address(); builder != "" {
|
||||||
|
return "/tmp/buildx-" + builder
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func isMobyWorker(sb integration.Sandbox) bool {
|
func isMobyWorker(sb integration.Sandbox) bool {
|
||||||
name, hasFeature := driverName(sb.Name())
|
name, hasFeature := driverName(sb.Name())
|
||||||
return name == "docker" && !hasFeature
|
return name == "docker" && !hasFeature
|
||||||
|
Loading…
x
Reference in New Issue
Block a user